Тёмный

Securing a Laravel API in 20 minutes with JWTs 

Andrew Schmelyun
Подписаться 36 тыс.
Просмотров 79 тыс.
50% 1

Опубликовано:

 

10 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 91   
@aschmelyun
@aschmelyun 4 года назад
UPDATE FOR LARAVEL 6.0+: I've seen a few comments about the above video not working in Laravel 6. There was a discussion and back-and-forth on the GitHub repo about this, and there seems to be a stable solution out there now. Run "composer require tymon/jwt-auth ^1.0.0" from your project root or update your composer file directly to reflect using version ^1.0.0 of tymon/jwt-auth.
@aschmelyun
@aschmelyun 4 года назад
If this doesn't work for you, please let me know!
@nagorik24
@nagorik24 4 года назад
Trying to install ^1.0.0 throws "Could not find package tymon/jwt-auth in a version matching 1.0.0" But 1.0.0-rc.5.1 works fine
@ih4722
@ih4722 4 года назад
Where is the GitHub repo?
@MrAKGarg007
@MrAKGarg007 4 года назад
composer require tymon/jwt-auth ^1.0.0, This works like charm till date Laravel 6.2 . Thanks for your comment Andrew, just saved some debugging time. :)
@TJVideos
@TJVideos 3 года назад
@@ih4722 did u find it??
@blackcrow9876
@blackcrow9876 5 лет назад
Finally somebody that speaks English.
@MarceloSilva-bs7nu
@MarceloSilva-bs7nu 3 года назад
bull shit!
@Deepankarsingh1993
@Deepankarsingh1993 5 лет назад
Thank you, finally someone have explained the jwt process in a way that a beginner can understand
@bulldog2024
@bulldog2024 4 года назад
That is so much easier and quicker than trying to setup passport. Great video.
@jean-davynizigama5019
@jean-davynizigama5019 4 года назад
Finally something other than passport. You're a hero 🙏🙏🙏
@ariamustofa
@ariamustofa 4 года назад
Thank you very much for your video @Andrew.
@fadilrahadiansyah4223
@fadilrahadiansyah4223 3 года назад
this is i needed the most, need to make default guard web, but need API auth thanks !
@aminraeisi
@aminraeisi 5 лет назад
Can you please explain the process of when how do we use the refresh token? for example if I have a react native app that consumes the API then after the login, I am going to use the token that I got in first login and won't know if it is expired or no. Do I have to always check if token is expired so that I call the refresh() method or am I missing something?
@arnabjisit
@arnabjisit 2 года назад
Thank you @Andrew, this is really helpful for new learners like me..
@migfus-codes
@migfus-codes 3 года назад
If the token refresh, will it reset the expiration time?
@flayshon
@flayshon 4 года назад
9:48 How are you able to use the login route without declaring it in the routes/api.php file? I had to create one myself for the login to work Route::post('/login', 'Api\Auth\LoginController@login'); Edit: Okay, I can see the route at 11:28. Declaration must have been edited out by accident lol
@mateoborrazas27
@mateoborrazas27 4 года назад
Same happened to me broooo
@Devdojo
@Devdojo 5 лет назад
This is a great video! Thanks for creating it :)
@aseemlalfakawma5084
@aseemlalfakawma5084 5 лет назад
Great resource, thank you for this great explanation and example of JWT tokens.
@theretroman3862
@theretroman3862 4 года назад
Very complicated indeed... hopefully some day I will be able to do this shit!
@jayantrawat5961
@jayantrawat5961 Год назад
Just I out curiosity, I want to know why you didn't put the whole code, inside try, while creating post? I use to put whole things inside try and the catch if there any error. Any specific reason for that??
@mahmoudakoobah5505
@mahmoudakoobah5505 3 года назад
Thx, Very helpful tutorial, and it's working on Laravel 8
@zaqueuorlando801
@zaqueuorlando801 Год назад
I am not getting it Please can you provide the soruce code cause i can see dumplecated auth folder i am its just confusing
@akashpatel7902
@akashpatel7902 5 лет назад
Great jwt example thanks man...
@KistlakRajapakshaSevenNet
@KistlakRajapakshaSevenNet 4 года назад
Thank You Very Much Bro !! :D But, one thing.. I successfully created a token in one of the applications in cloud hosting. But, another app that is in the same cloud hosting returns true for JWT instead of a token. How can I Fix this ??
@hamaadchaudhry5393
@hamaadchaudhry5393 Год назад
can we use this package for authentication of our private channels for Laravel websockets
@arturoatencio1433
@arturoatencio1433 4 года назад
Hi, I do have a few questions, if you dont mind. At 7:30 when you just create a new login controller inside the api/auth folder, it creates a very basic scaffolded controller, so when you start building the login function, it has nothing else than the scaffolded lines. What I had before watching this video, was a copy-pasted login controller generated from the artisan make::auth and tried to customize it. But it requieres to user the `use AuthenticatesUsers;` at the very first line of the class, so I followed the rest of the tutorial and everything works just fine, But if I remove this line "use AuthenticatesUsers;" it stops working, even when on the video, you are not using this. Secondly, when you just copied the Controller.php inside the /api folder at 9:06, you forgot to fix the namespace, so Im wondering how is that it works. But bro, so many thanks. This just helped me a LOT!!
@carnalito80
@carnalito80 4 года назад
Did you get it to work? There are the issues you mentioned, which i fixed but it still doesnt work.
@awaisfiaz8186
@awaisfiaz8186 5 лет назад
Those who are facing this issue after moving the controller to /Api FatalErrorException: Class App\Http\Controllers\Api\Controller; not found in file change the name space above Controller.php namespace App\Http\Controllers; ------> namespace App\Http\Controllers\Api;
@jiromesbaril9899
@jiromesbaril9899 4 года назад
Life saver! Thanks!
@kunalsachdeva9059
@kunalsachdeva9059 4 года назад
How can I make sure the token is device specific - I just don't want user to use the token generated on website to use inside postman request? Also, I want user to have only one active token at one time, so if he tries to login on new device then I want old token to be revoked, how can we do this? Please explain this as well. Thanks in Advance
@bastiyanrodrigo9951
@bastiyanrodrigo9951 4 года назад
I got one question with this method of copying the main controller. How does this respond to core updates?
@PathOfDamn
@PathOfDamn 4 года назад
Does refreshing the token invalidates all other tokens that the user may have generated?
@MrAKGarg007
@MrAKGarg007 4 года назад
Hi, How I can use jwt for another model except user? Like auth guard ?
@user-el8gf3ly4g
@user-el8gf3ly4g 3 месяца назад
how to use jwts with mongodb in laravel10 ?
@thamerbelfkih6593
@thamerbelfkih6593 4 года назад
thank you for this tutorial, just I want to point out that the try/catch for the authentication user is a best use case to use Laravel middleware instead of putting it within a method in the controller
@soultouchingsongs
@soultouchingsongs 3 года назад
Awesome tutorial. Subscribed👍
@aminraeisi
@aminraeisi 5 лет назад
Thanks! This really helped.
@prashantjani2963
@prashantjani2963 2 года назад
can anyone tell me how auth()->attempt($creds) generated token?
@bulldog2024
@bulldog2024 4 года назад
How would I go about adding custom claims to the jwt such as roles, etc?
@R055Cuk
@R055Cuk 3 года назад
I have the same question
@manuelli_sebastian
@manuelli_sebastian 3 года назад
Awesome, thank you.
@pratik8787
@pratik8787 3 года назад
tymon/jwt-auth is not compatible with php8. Can you advice any other jwt package?
@hermanheunis9354
@hermanheunis9354 2 года назад
Package repo is shifting to PHP-Open-Source-Saver / jwt-auth . It is compatible with Laravel 8 and Php8.0. I tried it and it works.
@aserlink
@aserlink 3 года назад
Works perfectly with laravel 8.* .. thanks sir
@Desmait
@Desmait 3 года назад
Hi, what are you storing in db, only login email and token?
@aserlink
@aserlink 3 года назад
@@Desmait The only fields you're storing in the Db are Email and Password that you use to generate the token. After generating it, you dont store it in the database since Jwt will automatically check and verify it. If you are building a mobile application, you can save this token in shared preferences and then attach it to every request.
@Desmait
@Desmait 3 года назад
@@aserlink i just can't understand how to properly do registration function in controller(in case with jwt). Can you please provide some example? Thanks
@aserlink
@aserlink 3 года назад
@@Desmait The registration function remains the same whether with jwt or not. You simply receive your registration fields and save them in the database. Jwt comes in during login at the login function...that is where you change from session to Jwt as explained in this tutorial. If you follow the tutorial step by step, you will manage to get it up and running.
@Desmait
@Desmait 3 года назад
@@aserlink Thanks a lot, managed to do this but now i'm having problems with cors xD
@ahmedbh8837
@ahmedbh8837 4 года назад
you the best bro thx
@mediontamginskii5889
@mediontamginskii5889 3 года назад
Nice Job!!! It was very helpfull!!!
@sleepydev4700
@sleepydev4700 4 года назад
this is a great tutorial, but can you tell me why it was return token: true if we did not setdefaultdriver to api thanks in advance
@charlespitagan8241
@charlespitagan8241 4 года назад
Hello Andrew , I love your tutorials. Please make a video for protecting the routes in vue-router. Thank you!
@aschmelyun
@aschmelyun 4 года назад
Coming soon!
@easyvideott7505
@easyvideott7505 3 года назад
Hi Andrew, can you explain in short in plain english how would an external app consume that api.... it sends the credentials on login and in response it gets the token.... and then what? For example if this would be done with guzzle... you need to somehow have that token in all consecutive requests.... and that's the part that I'm missing/don't understand.
@calixtus019
@calixtus019 5 лет назад
how to change the content-type of request form-data in application/json?
@lingting9790
@lingting9790 3 года назад
how to implement with change/update password checking old password ?
@NoahNobody
@NoahNobody 4 года назад
I'm quite new to the Laravel scene. Would this also work for Lumen?
@maciej_s
@maciej_s 5 лет назад
Hi, great vid! How would you persist the JWT on the client? Let's say decoupled VUE.js app. I am looking to achieve high level of security.
@Brianle180
@Brianle180 5 лет назад
Hi, Your approach to create the new controller and set the default driver for auth work like a charm for me. But can you let me know why we cannot use something like middleware('auth:api') in the group routes at the api.php? (actually I tried and fail since auth()->attempts() just return boolean instead of token key ). Moreover, Is there anyway to know which is the current default driver???
@aminebenkhouya7295
@aminebenkhouya7295 5 лет назад
you should use jwt.auth middleware.
@raufhashimov241
@raufhashimov241 4 года назад
This code from this video tutorial is not working well.
@ahmedmarzouk5126
@ahmedmarzouk5126 4 года назад
Thank you very much for your efforts. I have a question please. When using this way of authentication without sending client_id and client_secret to get the access token, Does this mean that any application on the internet can use my API ? If yes, is it safe? Thank you!
@ashishbhardwaj9760
@ashishbhardwaj9760 3 года назад
Hi, Andrew nice tutorial about jwt token. I've implemented it successfully. Thanks I'll like your vscode theme and vscode font, color-code. Will u pls share these settings of vscode?? Thanks in advance...
@GinishaVibes
@GinishaVibes 4 года назад
How to do it without database. Like just an cahche
@sureshkukumati6526
@sureshkukumati6526 3 года назад
Thank you sir 👍
@fahmyizzuddin9849
@fahmyizzuddin9849 5 лет назад
Can you help me why hit this type of error "Interface 'Tymon\\JWTAuth\\Contracts\\JWTSubject' not found"
@aschmelyun
@aschmelyun 5 лет назад
What's the version number for jwt-auth listed in your composer.json file?
@fahmyizzuddin9849
@fahmyizzuddin9849 5 лет назад
@@aschmelyun i'm using the latest version 1.0.0-rc.4.1
@keyjeyelpi
@keyjeyelpi 4 года назад
Great, you applied it. How about making a vid where you apply on the laravel routes?
@flayshon
@flayshon 4 года назад
Just a heads up: if you put that try-catch in the parent class function and it throws an the UserNotDefinedException, the "return $user->posts" statement in the PostsController will throw another exception, because it expects $user to be an instance of App\User, but the authUser() method will return a JsonResponse with an error message. The refresh method try-catch needs to catch \Tymon\JWTAuth\Exceptions\JWTException too. It will be thrown if you try to /refresh without using any token.
@dennisbarnaja1708
@dennisbarnaja1708 3 года назад
Thanks
@TheMarvelsWorld
@TheMarvelsWorld 4 года назад
JWT works without auth?
@prianasaputra7583
@prianasaputra7583 2 года назад
Thanks
@ifrashable
@ifrashable 5 лет назад
but how to refresh when its expire?
@ellgibug
@ellgibug 4 года назад
Thanks for tutorial) PS I think it better to make middleware, not try ... catch in every method %)
@ih4722
@ih4722 4 года назад
Can someone share the link to the GitHub repo PLEASE.
@jhoanborges6574
@jhoanborges6574 4 года назад
Genius.
@asimabusallam3147
@asimabusallam3147 3 года назад
thanks a lot
@alexios4392
@alexios4392 4 года назад
So where are you gonna keep this token on fronted?
@aschmelyun
@aschmelyun 4 года назад
If I'm using Vue, I'll keep it in the state management (Vuex). Otherwise, localstorage isn't a bad option either.
@scottpurdie3598
@scottpurdie3598 4 года назад
I would love to see this updated for 6.x Laravel.
@aschmelyun
@aschmelyun 4 года назад
Taylor Otwell is planning a package to simplify this soon, you can count on a new video when that drops! In the meantime, most of this should work for 6.x but I'll go through and make sure of it!
@premkantsahu7816
@premkantsahu7816 3 года назад
Getting token:false My credentials are fine...
@subhadipghorui2216
@subhadipghorui2216 4 года назад
How to logout the user ??
@aschmelyun
@aschmelyun 4 года назад
You would use auth()->logout(). See more from the documentation here: jwt-auth.readthedocs.io/en/develop/auth-guard/#logout
@Fuzkin
@Fuzkin 3 года назад
Bro we don't need to hear your keyboard.
@DotnetistEnterprise
@DotnetistEnterprise 3 года назад
thanks
Далее
Sigma Girl Pizza #funny #memes #comedy
00:14
Просмотров 1,7 млн
Create a local Laravel dev environment with Docker
14:48
Getting started with Laravel Passport and OAuth2
29:52
Laravel SPA Authentication - setup and common mistakes
16:55
Authentication in Vue using Laravel Sanctum
23:06
Просмотров 71 тыс.
Running multiple Laravel apps locally with Docker
13:20
PHP is the future
34:27
Просмотров 175 тыс.
Laravel JWT Authorization - Tutorial for beginners
21:51