Laravel API Auth with Sanctum and API Tokens

Поделиться
HTML-код
  • Опубликовано: 6 июл 2024
  • This is a free lesson from my upcoming course "How to Create Laravel API". We take a look at one of the ways to authenticate the API user - by issuing a long-term token which will be used in all the requests.
    Subscribe for -50% discount for the upcoming course:
    bit.ly/laravel-api-subscribe
    - - - - -
    Try our Laravel QuickAdminPanel: bit.ly/quickadminpanel
    Enroll in my Laravel courses: laraveldaily.teachable.com
  • ХоббиХобби

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

  • @jhonbarcasnegras
    @jhonbarcasnegras 3 года назад +17

    Two days trying to understand it and suddenly this video saves my life in a few minutes.
    Thank you.

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

      Exactly :) Love this channel. I did a live stream yesterday about Sanctum for 1 hour and struggled with it. This helped me instantly.

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

    Thankyou for your brief & clear explanation, straight to the point & solving my problem

  • @redamakarem3737
    @redamakarem3737 3 года назад +1

    Love your content
    Just bought the course
    All the best man

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

    This was killing me with pain but just a minute you made my day happy DUDE

  • @_saurabhshah
    @_saurabhshah 4 года назад +3

    Great content as always ❤️

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

    Thanks as always! Keep the hard work!

  • @Mocka-yf4ez
    @Mocka-yf4ez 2 года назад

    thank you so much your explanation so clear

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

    If I use a "role abilities" approach (for the User model), the token ability can be bypassed or should that be considered a totally different type of authorization?

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

    Thanks a lot, Povilas! You helped so many people. One question. What can be wrong with the following? I made everything according to this video, but in the very end when in the controller I try to use auth()->user()->tokenCan() it falls because auth()->user() is null. Maybe someone else here can know? My API controller cannot see the user.

  • @Shortgrauzis
    @Shortgrauzis 3 года назад +1

    Thank you. I was stuck on using a token from database which is encrypted? I guess. Did not realize I needed to use plaintext one.

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

    Hey! Can you please validate the below? When using Sanctum API Tokens tokenCan() is used instead of Gates and can(). While when using Sanctum with Vue (for instance), Gates and can() are the ones used instead of tokenCan()? From what I understand, Sanctum SPA Auth makes use of Laravel Session, hence Gates are just natural to be used. While, Laravel API Tokens, is something else relying heavily on Token + Abilities. Thanks!

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

    Sounds good !

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

    Hi
    I've protected my route with Sanctum API Token as you describe, but I've a question: when I call route internally, in my code with the 'name()' , I'd like to bypass the auth. What is the best practice?
    Thank you.

  • @JY-xv7ic
    @JY-xv7ic 4 года назад

    another new thing that i learn..

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

    In 5 minutes!!! Thou rockest !

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

    super amazing!!!

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

    What is the best way to protect Vue page with Sanctum?

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

    do you have any guide to build portal website using laravel and connect to this api?

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

    Awesome!

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

    Is there a way where we can delete all tokens which are not used anymore using a scheduler?
    Thank you in advance.

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

    I would rather watch Laravel lessons than Gaming live streams🤣

    • @PovilasKorop
      @PovilasKorop 4 года назад +7

      And my lessons are shorter! :)

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

    What's the sense of a "white list" of web tokens?

  • @diegoc3749
    @diegoc3749 3 года назад +1

    but anyone with access to the public SPA can look at the requests made vis the browser console and steal that bearer token. How is this secure?

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

    I always follow your great tutorial videos. I learned lots of things from here. I developing a laravel vue spa project with laravel sanctum but when I Deploy my project on sub domain. I am getting 401 error. Can you please make a video on laravel sanctum project deploy on live server

  • @birenjung
    @birenjung Месяц назад

    I think the show function returns 403 status because token named categories-show is not made. Am I right?

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

    Beautiful!, thanks a lot!

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

    Great Tutorial. Is it possible to combine Sanctum API Tokens with Passport/JWT instead of Sanctum SPA Authentication ?

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

      I've never heard/read of this hybrid approach, don't think it would work.

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

    sir please help me i want to apply sanctum on my custom created model instead of user.php what I have to do kindly help me?

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

    Sad that there is no refresh token functionality

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

    First of all thanks for those awesome videos. But I have question how can we secure this token exposing from the frontend site or mobile auth?

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

      use ssl (https) on the backend. That way every request is encrypted. If not, then someone could intercept the token using MITM technique.

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

      @@silvanasauzana8693 thanks for your reply. But I am talking about securely save token in Storage or cookies. So that no one can steal token from browser

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

    I found that Laravel Sanctum and Fortify documentation is very confusing. It's possible/can use Laravel Sanctum session guard to authenticate api.php route? and Forfity for api.php route?

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

      Or we must use Laravel Sanctum Token to authentication api route, and we cannot use Forfity for api route?

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

    great

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

    how to make multi auth with sanctum or passport.

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

    I think there is also another way to authenticate API which is using the Laravel Session cookies and like simple Web authentications.
    My question is which one is better or I should use?
    thanks in advance

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

      You meant this? laravel.com/docs/8.x/sanctum#spa-authentication
      Yes it works, it's your personal choice which one to use.

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

    Thank you very much for this great video; finally I understand the Sanctum!
    Could you explain what is the best practice to use 'tokenCan()' in combination with Gate and Policy?

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

    tokenCan is always giving true? Plz help

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

    Povilas, what do u prefer? JWT or sanctum?

    • @PovilasKorop
      @PovilasKorop 4 года назад +4

      JWT is soooo 2018 :) But if you got used to it and you're happy with it, stick with JWT. But for everyone new to API Auth I totally recommend Sanctum.

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

      ​@@PovilasKorop Thanks for your opinion :)

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

    I am eagerly waiting for the course to release. Can you please tell us the estimated date for that?

    • @PovilasKorop
      @PovilasKorop 4 года назад +3

      Next week. I decided to spend more time on Auth section and do a bit more research, it took more time than I expected.

  • @JY-xv7ic
    @JY-xv7ic 4 года назад +1

    how to set the expire?

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

      You can config it in sanctum.php file under config folder

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

    the course has some examples on how to consume the api with some js framework or just has the examples of how to consume the endpoints with postman?

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

      Mostly Postman, only one or two lessons is a demo of Vue.js, but just to show how it works with API, then we get back to endpoints. For JS framework usage with API, you can check out my other newer course "Vue.js + Laravel: CRUD with SPA" laraveldaily.teachable.com/p/vue-laravel-crud-spa

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

    can you help me please? I'm gettin this error "Trait 'Laravel\Sanctum\HasApitokens' not found" eventhough i have install and import sanctum in user model. please anyone help me

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

      HasApitokens - shouldn't the T be uppercase? HasApiTokens - maybe that is the reason?

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

    Can you make a detailed video on OAuth what it is and what it will do?

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

      I don't actively use OAuth, I prefer Laravel Sanctum for simplicity.

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

    Lumen OAuth2 plis

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

    I am using sanctum for authorization purpose only and handling permissions using spatie/permission. Is this the right way to do this?

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

      Yes, totally. Sanctum is for Authentication, and for Authorization you can use same Sanctum abilities or something external like your middlewares, Gates or packages.

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

      @@LaravelDaily , does you course contains videos about how to implement roles and permissions (spatie pkg or not)?
      Because i had problems to use the spatie package with sanctum and i would be interested.

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

      @@MaurizioCalarese I didn't plan that, because it depends on specific permission package/logic you want to use, but I guess it would be useful. Now, question - what kind of Sanctum you used? SPA Authentication or API Token?

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

      @@PovilasKorop , thanks for your reply.
      Yes, definitely would be useful, there are a lot of videos/tutorials about sanctum out there, but no one (at least I haven't found one) that shows how to implement with it existing solutions like spatie/permissions or that goes deeply into the argument (to setup sanctum is enough the official documentation, imho).
      For my project, I'm using Sanctum with API token mode and I had to write roles and permissions from scratch, without all the nice and ready functionalities that the spatie package gives to us.
      Anyway, the best solution would be a setup that works also with SPA mode, to speed up future projects.
      Just an idea for a course/tutorial.
      Ps. I appreciate & understand your good work.

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

      @@MaurizioCalarese Well, here's where it gets confusing. So you used API token for a page that should have been a SPA? Specifically in Sanctum docs it is written: "You should not use API tokens to authenticate your own first-party SPA. Instead, use Sanctum's built-in SPA authentication."
      So whatever tutorial/course I create, every project is almost individual for that type of project, so Sanctum/Passport, then inside of those - what type of tokens to use, and then on top - which roles/permissions logic/package.
      So I can't publish any content that would suit ALL of those possible cases. For the API token case, I would use the same abilities parameters as I used in this video, without any spatie. But it also depends on the features of your project, how big it is, what are typical permissions etc.
      The more I dive into this, the more I realize it's VERY individual, and I can only show people guidelines, or "my way" of doing it, but not help in everyone's situations.

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

    Would you mind giving us a breif about your local env? it looks clean not like xampp

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

    Please can you show us how to pass the json returned data in blade file

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

      Why would you need that? Can you give an example?

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

      @@PovilasKorop cause sometimes we need to pass data returned from Api to blade file

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

    I don't understand why you have a json response for unathorized, but I have the HTML code of the page. What am I missing? been searching on google ofr more than 30 minutes without a solution

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

      Header "accept: application/json"

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

      @@LaravelDaily thanks. I figured that after when I decided to avoid looking on google and check postman’s options