Laravel Sanctum - Hitting all the rough edges

Поделиться
HTML-код
  • Опубликовано: 9 апр 2020
  • In this video I'm going to use Laravel Sanctum to authenticate requests to an API using session-based and token-based authentication. While doing so, I'm going show you all the possible errors that you might get and how to fix them.
  • НаукаНаука

Комментарии • 57

  • @pratikrane149
    @pratikrane149 4 года назад +5

    This is one of a kind of a video. Usually we don't really get to see such tutorials where one touches all the points about a technology in this details. Thank you Mohamed.

    • @themsaid
      @themsaid  4 года назад +1

      Glad you found it useful :) Your comment means a lot

  • @necrotikS
    @necrotikS Год назад

    The best video about Laravel Sanctum I've seen.

  • @daleryanaldover6545
    @daleryanaldover6545 2 года назад +1

    Finally I can send request from different domain

  • @BrianDillingham
    @BrianDillingham 4 года назад +5

    Yet another great video! looking forward to the token cookie video you hinted at 😇

  • @GAURAVMAKHECHA
    @GAURAVMAKHECHA 4 года назад +2

    I like your style of making the HTTP calls from the console.

  • @QiroLab
    @QiroLab 4 года назад +1

    Excellent explanation 👏👏👏

  • @robmellett1319
    @robmellett1319 4 года назад +2

    Amazing video Mohamed! Would love to pay for more videos with this technical walkthough

    • @themsaid
      @themsaid  4 года назад

      Glad you find my content useful :)

  • @millennia
    @millennia Год назад

    Amazing, I was so confused شكرا محمد بارك الله فيك

  • @samirboudaoud9432
    @samirboudaoud9432 4 года назад

    excellent ! one question: if im using standard laravel authentication, is the @csrf directive enough instead of getting cookie through 'sanctum/csrf-cookie' for consuming routes protected by sanctum middleware? thanks

  • @lupacescueduar
    @lupacescueduar 4 года назад +2

    Great video, thanks Mohamed! Could the session based authentication be tested in Postman or netlify somehow?

  • @briandiolun139
    @briandiolun139 4 года назад +1

    Heya, thanks for your series on multi-tenancy within Laravel - extremely useful and very well explained. I was wondering do you have any advice in using Sanctum with multiple tenants? I am aiming to use it towards two separate auth flows for both Admin and User tables, but finding it a bit confusing as to where the personal access tokens should be stored. Open to better solutions if had anyway.
    Any guidance would be awesome - thanks again!

  • @andytav89
    @andytav89 4 года назад

    Does Sanctum allow wild card subdomain for the session? I can't get it to work.

  • @ripon59
    @ripon59 2 года назад

    Awesome one

  • @krunalshrimali4471
    @krunalshrimali4471 3 года назад

    Hii
    What kind of multi auth is better? User model and role model combo or each user role as it's own model? Pls make a video if possible.

  • @rayanthonymadrona988
    @rayanthonymadrona988 4 года назад +1

    great work.will be waiting to your video on how to securely store token on a cookie.

    • @themsaid
      @themsaid  4 года назад

      If your frontend runs on the same root domain as your API you don't need to use tokens at all. Use the good old cookie-session approach.

    • @TareqHasan
      @TareqHasan 4 года назад

      Mohamed Said Normally in our apps, the JavaScript app resides on app.domain.com and the API on api.domain.com, what would be the suggested way?

  • @adamgm84
    @adamgm84 4 года назад

    I have mine setup like this, but Auth::logout() throws "Method Illuminate\Auth\RequestGuard::logout does not exist." on POST to /api/logout and also my Login unit test throws "Session store not set on request." on POST to /api/login. I am using the default Laravel AuthenticatesUsers trait. Both those routes point to the AuthenticatesUsers trait.

    • @adamgm84
      @adamgm84 4 года назад

      I can fix the unit test error by adding this middleware to the 'api' middleware group in Kernel.php: \Illuminate\Session\Middleware\StartSession::class,
      But I can't find any information on the internet about why RequestGuard::logout doesn't exist.

    • @adamgm84
      @adamgm84 4 года назад

      Bonus update: I can fix the logout error by removing the logout route (ie: Route::post('logout', 'Auth\LoginController@logout')->name('logout'); ) from the group with "auth:sanctum" middleware, and placing it by itself. I have no way to validate if this is correct. The logout route seems like something the docs should show in the minimal example code.
      Bonus update update: I just noticed everything still works in my repo if I move the login route into my guest middleware group (which has LoginController's middleware except logout), so it means that Sanctum is intended to be used with Auth::routes() in the web.php file's root closure. I am almost certain I am about to make a change of this nature and it may alleviate the session store problem because I have everything in api.php currently.

  • @gabrielhernandez8457
    @gabrielhernandez8457 3 года назад

    Hi Mohamed!... this work for me exacticly like you show us, but when I set https(certbot in nginx) for both, app and api, don't work any more, always throw cors error... please help!

  • @hassanabas186
    @hassanabas186 4 года назад

    Great tuts Mohamed, if im going to use a SPA that fetch data from API that implements Oauth2, what do u think is the best way to secure the token, should i shorten the expiration for like half an hour and force the user to login, and obtain new token ! is that secure enough!

    • @themsaid
      @themsaid  4 года назад

      You could do that but the UX will suck, no one likes to keep logging in again and again. Honestly I don't think there's a secure way to store a token when the frontend is hosted in a completely different domain than the API.

  • @aldrinmojica
    @aldrinmojica 3 года назад

    Hi Mohamed Said your videos are very informative and I'd say it's premium videos, thank you so much. I have question regarding on laravel passport client generated access token.
    It seems the access token generated once login is too 'long' and when I checked it from the database, the generated token it's seems did'nt match. Is there a possibility to find the generated access token from database(generated token after login)?. We love to hear from you or watch another video breaking down whats behind the scene of laravel passport :)

  • @thembocharleslwanga8990
    @thembocharleslwanga8990 Год назад

    Thank you

  • @julyostv1376
    @julyostv1376 3 года назад

    I followed your step. but still getting 401 in my end. if im using it in cpnael shared host. but in my localhost its working

  • @alexios4392
    @alexios4392 4 года назад

    Is it required to get a new CSRF-TOKEN from /sanctum/csrf-cookie endpoint before each POST request? Or just before trying to log in?

    • @themsaid
      @themsaid  4 года назад

      No, just before you log in.

    • @alexios4392
      @alexios4392 4 года назад

      ​@@themsaid so until I have this XSRF-TOKEN in Cookie and is valid, I don't have to send request for a new token (/sanctum/csrf-cookie)? In my project all GET requests also has this token in response.

    • @themsaid
      @themsaid  4 года назад

      @@alexios4392 Yes you don't need to have it in future request. Just this one time. Unless the session expires.

    • @alexios4392
      @alexios4392 4 года назад

      @@themsaid Is it possible to use CSRF_Token from meta tag instead and add him to Axios credentials?

  • @fidellistaz766
    @fidellistaz766 Год назад

    sactum is working on local host , but on production after successfull login it its not able to get user ,I get the user unauthenticated

  • @user-jx8uz6tb6k
    @user-jx8uz6tb6k 2 года назад

    awesome!

  • @pratikrane149
    @pratikrane149 4 года назад

    I have a question, can we use this cookie-based SPA authentication feature of Sanctum with a Nuxt SSR?
    Edit: I mean are there any caveats? Or would it be as same as Nuxt SPA?

    • @themsaid
      @themsaid  4 года назад +1

      I have no experience with Nuxt so not sure how it works :)

    • @pratikrane149
      @pratikrane149 4 года назад

      @@themsaid Alright, no issues man. :)
      BTW if you like working in Vue then you should definitely checkout Nuxt. It gives a breeze of a development experience.

    • @themsaid
      @themsaid  4 года назад +1

      @@pratikrane149 I will check it out at some point. Taking a frontend break at the moment :)

    • @pratikrane149
      @pratikrane149 4 года назад

      @@themsaid Oh yes sure. We anyways need more insights on back-end from a person like you. :)

  • @amaelftah
    @amaelftah 4 года назад +1

    this is super . keep it up (Y)

  • @bxrank
    @bxrank 3 года назад

    How to add prefix in default /login & /register api in laravel sanctum ?
    Ex. Default api are /login & /register
    I want /api/login & /api/register
    How to achieve this?
    Also in /user api i am getting many fields like id,name,email,created_date
    I just want name and email id
    How to do that?

  • @remanbala7532
    @remanbala7532 4 года назад +1

    How to do spa multi auth using Laravel Sanctum ?

    • @themsaid
      @themsaid  4 года назад

      What do you mean by multi auth?

    • @remanbala7532
      @remanbala7532 4 года назад

      @@themsaid Different user and admin dashboard

  • @ShreyanshPanchal
    @ShreyanshPanchal 4 года назад

    07:35 Anyone thinking where did createToken method on User Model come from?
    You have to add this trait on user model "Laravel\Sanctum\HasApiTokens"

    • @themsaid
      @themsaid  4 года назад

      Yes yo have to add the trait indeed. Thanks for pointing this out.

  • @riahiyassine7703
    @riahiyassine7703 Год назад

    Awesome

  • @riotsportsandentertainment9544
    @riotsportsandentertainment9544 4 года назад

    Voila. :)

  • @pradeepspace3
    @pradeepspace3 4 года назад +1

    I am first 😊

    • @ultrasamad
      @ultrasamad 4 года назад +1

      But I was first to like. :-)

  • @kirubaharanmayilvahanam8201
    @kirubaharanmayilvahanam8201 4 года назад

    Very complicated... Wish it was Jeffrey

    • @themsaid
      @themsaid  4 года назад +6

      There's a really good video on Laracasts by Andre Madrang that you can check laracasts.com/series/whats-new-in-laravel-7/episodes/6

    • @kirubaharanmayilvahanam8201
      @kirubaharanmayilvahanam8201 4 года назад

      Thank you Said. Will check it out. 🙏

    • @BrianDillingham
      @BrianDillingham 4 года назад +9

      Some of us needed the complex version not an introduction. The topic is inherently complex. I think Mohamed does a great job at making the complex more digestible

    • @kirubaharanmayilvahanam8201
      @kirubaharanmayilvahanam8201 4 года назад

      Brian Dillingham I understand, I didn’t mean to hurt him. I couldn’t follow along. May be it’s complicated to me.