Тёмный
No video :(

Setting cookie to save access token in our React JS app with Laravel API | React, Laravel 

Amitav Roy
Подписаться 19 тыс.
Просмотров 29 тыс.
50% 1

React JS, Laravel API, Admin LTE theme
In this video, we are going to use the universal-cookie npm module to save the access token in a cookie which we will then use to make API calls.
We will also look at having different expiry based on the user's selection of Remember Me option.
Code: github.com/amitavroy/react-sp...

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

 

27 мар 2020

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 53   
@modaralkasem4027
@modaralkasem4027 3 года назад
thank you so much sir
@amitavroydev
@amitavroydev 3 года назад
Most welcome
@soultouchingsongs
@soultouchingsongs 3 года назад
Thanks for the tutorial. It's very nice. As you mentioned about using the cookies for protected routes.. I cannot find that video. Pls share the link. Thank you once again for this awesome video.🙏
@amitavroydev
@amitavroydev 11 месяцев назад
Will check
@JorgeMorgado259
@JorgeMorgado259 4 года назад
The advantage of using cookies over localstorage is that you can set expires date?
@amitavroydev
@amitavroydev 4 года назад
Yes, we can expire the cookies based on their expiry time. That's an advantage
@junaidkhan7069
@junaidkhan7069 4 года назад
hey in my case this.props.history.push('/home) doesn't work first time but when i refresh the login page it works...can you tell what's the solution please?
@amitavroydev
@amitavroydev 10 месяцев назад
Difficult to tell without the code
@raghavayaddanapudi8281
@raghavayaddanapudi8281 4 года назад
All are working good but when I login it's redirect to home but there was one error TypeError: instance.render is not a function when I change protected index.tsx to ProtectedRoute: React.FC = return ; please reply thanks
@amitavroydev
@amitavroydev 9 месяцев назад
too old so archiving
@adityanair3566
@adityanair3566 3 года назад
Hi! Is it possible to store cookies without using universal-cookies package in reactjs, coming directly from server? I really hope you reply to this comment!
@amitavroydev
@amitavroydev 3 года назад
As far as I know, to store cookies you will need to use some package. You can use universal-cookies or js-cookies etc. But yes, I am sure some kind of package is required. Unless of course you want to use Native browser API and know how to do that.
@JorgeMorgado259
@JorgeMorgado259 4 года назад
Plase explain how to get a new token using the refresh_token and update the cookie
@amitavroydev
@amitavroydev 4 года назад
Ok, I will also have to check. Generally, I have always kept a short lived token hence never used refresh token. Will check
@camilosw4753
@camilosw4753 Год назад
Do you know if "universal-cookie" serializes the cookie information?
@amitavroydev
@amitavroydev Год назад
Looked at the github code, yes it does serialise the data before setting the cookie
@shohagkhan3677
@shohagkhan3677 2 года назад
If possible, please make a small and simple authentication ( Login & Registration ) with Next Js and Laravel Sanctum.
@amitavroydev
@amitavroydev 2 года назад
Yes, why not
@shohagkhan3677
@shohagkhan3677 2 года назад
@@amitavroydev Thanks for reply. Laravel saying SPA authentication will be cookie based and the mobile authentication is token based. I think there is some hacks that every one will know by your tutorial. Thanks again for your good work ❤
@amitavroydev
@amitavroydev 2 года назад
See mobile apps don't have the ability to drop cookies, so they will use tokens while web apps will continue to use the cookie based approach. Sanctum allows both
@blog4lives
@blog4lives 4 года назад
Is it possible to change the code so that you can log in with a username instead of email address?
@amitavroydev
@amitavroydev 4 года назад
Yes, Laravel does offer you to change the default field from email to something else. You just need to customise the login controller. Check this link laravel.com/docs/7.x/authentication#included-authenticating Section is username customisation
@blog4lives
@blog4lives 4 года назад
@@amitavroydev How difficult is it to create a registration form and get it working with the laravel API? I find many different tutorials, but no idea what the correction method is.
@neerajsinghtangariya2587
@neerajsinghtangariya2587 3 года назад
Hello sir can you make a video for NextJs same Laravel login Api and frontend with Nextjs?
@amitavroydev
@amitavroydev 11 месяцев назад
Things have changed a lot
@junaidkhan7069
@junaidkhan7069 4 года назад
after login how can i send my login response data to other files where i can use them?
@amitavroydev
@amitavroydev 4 года назад
Send it to other files as in? You have set the data in the cookie. So from now on where ever you want to send the token, you can read the token from the cookie and then attach it to the header. That's the flow.
@junaidkhan7069
@junaidkhan7069 4 года назад
@@amitavroydev thanks that's helpful. Apart from token i'm sending logged in user ID in response which i'm gonna use in other component. i'm having difficulty to send this specific ID to other component files.
@mijnnaamisramon
@mijnnaamisramon 4 года назад
Small question, but your auth.isAuthenticated function only checks if the token exists, but if I change the value of the token in the cookie... this is still considered valid... and thus you are still logged in... this shouldn't be possible right?
@amitavroydev
@amitavroydev 4 года назад
Yes, you are correct. There is a flaw that someone can change value and bypass the login screen and land up on the home page. And, he will see some basic design elements as well. However, the user will not be able to see any data. The reason is when we make an API call, at that point the token is validated. It's a safe tradeoff I feel.
@mijnnaamisramon
@mijnnaamisramon 4 года назад
@@amitavroydev How is that user not able to see data? And does Laravel have a way to validate the token, which we can call with an api?
@EdwinManual
@EdwinManual 3 года назад
@@mijnnaamisramon Laravel validates the token or what's the point of all authentication thing?
@mijnnaamisramon
@mijnnaamisramon 3 года назад
@@EdwinManual I switched from Auth to Laravel Sanctum, it was much more suited for my situation.
@amitavroydev
@amitavroydev 3 года назад
If your requirements are getting filled by cookies then yes, it's a good option
@melboro8745
@melboro8745 3 года назад
How would you get the data of the currently logged in user? like the username you have displayed on the sidebar when you were logged in.
@amitavroydev
@amitavroydev 3 года назад
Store it in cookie. That's the best approach
@melboro8745
@melboro8745 3 года назад
@@amitavroydev How would you get the right data? the login response only give the email and the password. Or do you get the rest of the data by making an API call where you search for the user with the same email address?
@amitavroydev
@amitavroydev 3 года назад
Ideally, you can create an API which returns the entire object of the current logged in user. That way, the app can query that API and get the data anytime it wants to
@melboro8745
@melboro8745 3 года назад
@@amitavroydev Okay, i will try that. Thank you very much for your help!
@amitavroydev
@amitavroydev 3 года назад
Most welcome
@neerajsinghtangariya2587
@neerajsinghtangariya2587 3 года назад
Sir what is it means expiry in session...?
@amitavroydev
@amitavroydev 11 месяцев назад
When a session starts, it is created with an expiry
@drunkengrunze
@drunkengrunze 3 года назад
Any reason you are not using a functional component? Thanks
@amitavroydev
@amitavroydev 3 года назад
When I was doing this video, I was not very comfortable with functional components. However, now if you look at the latest videos, I have switched to functional components. And, I really like the simplicity. However, just one thing - it won't matter a lot whether you use a class based component or a functional component. It's just another way of doing thing.
@drunkengrunze
@drunkengrunze 3 года назад
@@amitavroydev Just one more thing. Someone has already mentioned about that. I do feel bit lack of explanation. Just a feedback. :) Will you be doing react redux aysnc API call as well?
@amitavroydev
@amitavroydev 3 года назад
Thanks for the honest feedback. I will try to ensure that my explanation are more elaborate. In regards to redux - I have not worked with it and hence, I first need to explore it myself.
@tyndecor5236
@tyndecor5236 Год назад
hi guy. How to open httponly flag?
@amitavroydev
@amitavroydev Год назад
Open as in? You want to add that flag?
@user-zr4co4bn3u
@user-zr4co4bn3u 2 года назад
It is not secure to store data like access_token in a cookies…
@amitavroydev
@amitavroydev 2 года назад
Ok, why so? And if not cookies, then where do you want to store it?
@temurbekfayzullaev2812
@temurbekfayzullaev2812 3 года назад
can you share source code
@amitavroydev
@amitavroydev 3 года назад
Updated the description with the git repo link
Далее
ТЫ С ДРУГОМ В ДЕТСТВЕ😂#shorts
00:55
React Authentication with Cookie
16:31
Просмотров 91 тыс.
Laravel 8 REST API With Sanctum Authentication
54:13
Просмотров 544 тыс.
React js project # Login with API
14:28
Просмотров 262 тыс.