React Native Login with JWT Auth Context

  Рет қаралды 47,110

Simon Grimm

Simon Grimm

Күн бұрын

In this video, we'll explore how to add JSON Web Token (JWT) authentication to a React Native app in order to protect users' sensitive data. And how to use Expo's Secure Storage to manage your JWTs.
🔥 Learn React Native FAST: galaxies.dev/reactnative
⚡️ Get the code: galaxies.dev/react-native-log...
#############################
🤷‍♂️ Want more Ionic tutorials?
There you go: devdactic.com/
#############################
❤️ You can also find me on:
Instagram: / simongrimm_
Twitter: / schlimmson
Facebook: / devdactic
TikTok: / simongrimm_
Or join the Simonics Facebook group:
/ simonics
#############################
00:00 Intro
00:46 Creating an Authentication Context
10:43 Secure your React Native App
15:00 Building the Login
18:10 Making authenticated API Requests
24:15 Outro

Пікірлер: 77
@galaxies_dev
@galaxies_dev 2 ай бұрын
Join Galaxies.dev today - the Home of the Best React Native content🚀
@Jopu96
@Jopu96 Жыл бұрын
Hi! Thank you for the video. You need to set autoCapitalize="none" inside the TextInput, so the simulator or real device does not capitalize the first character. And you don't have to type it in your Text editor and copy it :D
@user-wt4zu3jc1u
@user-wt4zu3jc1u 11 ай бұрын
Many thanks dude. You save my time a lot for my first React Native project with this!
@galaxies_dev
@galaxies_dev 10 ай бұрын
Awesome, always happy to help React Native devs :)
@kayongojohnsonbrian3902
@kayongojohnsonbrian3902 10 ай бұрын
AMazing video and is just on point, thanks a lot for the videos. I am learning a lot of the complex and hard parts from you man. Thanks a lot
@galaxies_dev
@galaxies_dev 9 ай бұрын
Super happy to help, always let me know when you got questions!
@komilolimov2257
@komilolimov2257 5 ай бұрын
Thank's Simon i really appreciate yoor work
@olalekanajayi3877
@olalekanajayi3877 6 ай бұрын
Had to subscribe, this video saved me tonight. Thank you so much
@galaxies_dev
@galaxies_dev 6 ай бұрын
Happy to hear that - would be awesome if you could share it with others 🙌
@comanmaryan7338
@comanmaryan7338 6 ай бұрын
Yeah , I suppose not enough revenue from ads, so let's add a subscription for the source code , not that I needed it personally , but would be great if you have mentioned that a subscription is necessary in order to get it.
@messylife4550
@messylife4550 9 ай бұрын
@galaxies_dev awesome tut! Curious what version of react-native and typescript did you run for this proj? I am getting an error from your Login.tsx - line:21 - when clicking login TypeError: onLogin is not a function (it is undefined) same goes for the register block. Would appreciate your guidance brotha!
@galaxies_dev
@galaxies_dev 9 ай бұрын
Don't think this issue comes from a TS version but more likely the function not being defined correctly in your component!
@argonjs
@argonjs 4 ай бұрын
when creating context define the methods too. like this const AuthContext = createContext({ authState: { token: null, authenticated: null }, onLogin: () => Promise.resolve({}), onLogout: () => {}, }); this will resolve your issue
@johncmm9759
@johncmm9759 9 ай бұрын
This is fantastic content! Thank you so much. I'm not copying your code line for line but can I use this as a reference for an app I'm building?
@galaxies_dev
@galaxies_dev 9 ай бұрын
Of course, I'm happy if you use my code :)
@ivanbatanov6588
@ivanbatanov6588 5 ай бұрын
Great video! How would you deal with refresh tokens when the JWT Token expires after some time for example.
@galaxies_dev
@galaxies_dev 5 ай бұрын
You could implement a check in useEffect() and automatically try to get a new token if you have a refresh/access token flow
@ricecodesdev
@ricecodesdev 2 ай бұрын
Hey Simon, once the user is authenticated, is there any way of accessing their data in the app? For example, I need to make a GET request and pass in the user's ID to get their data on the home page. Right now I can't make this call, because I have no way of passing any information (such as an id or a token) into the GET request on the homepage. Cheers
@ricecodesdev
@ricecodesdev 2 ай бұрын
Don't worry, figured it out. I had to import "useAuth" into my home page, store it in a variable (called "auth" in this case), and access the token via "auth.authState?.token".
@nguyen-van-quang
@nguyen-van-quang Жыл бұрын
I follow you a long time ago, you're finally back to react-native? Maybe flutter in next time : ), flutter is better for mobile cross-platform now! But react-native is still the best choice for a web js developer.
@galaxies_dev
@galaxies_dev Жыл бұрын
I'm into all of that, and yes there will be more RN and also Flutter over the next time :)
@hagosoftware
@hagosoftware 10 ай бұрын
Hi there, good tut! You use "useAuth()" on Login screen. Where does it come from? I reviewed the AuthContext, and it doesn't come from that.
@galaxies_dev
@galaxies_dev 10 ай бұрын
It's in the AuthContext almost at the top, an easy way to access the provider from the context!
@bilelrahmouni01
@bilelrahmouni01 11 ай бұрын
thank you
@jduffy6929
@jduffy6929 Жыл бұрын
I have something similar but what I’m trying to work out is how to handle when that token expires? Do I need to implement refresh tokens or how do I sign the user out once it expires?
@galaxies_dev
@galaxies_dev Жыл бұрын
Yeah you would have to setup a refresh/access token flow and when you get back a certain status from your backend, automatically make a call to get a new token!
@christophersalame4219
@christophersalame4219 6 ай бұрын
Then how do we store the refreshtoken?@@galaxies_dev
@DrHalloumiStix
@DrHalloumiStix 3 ай бұрын
I think your live api needed to be changed back to https rather than http at the end of the video. That may be why login wasn't working?
@luisdagraca2232
@luisdagraca2232 7 ай бұрын
Hello. I want to create an Expo app with users. Which form of authentication (JWT, Clerk, or Firebase) seems best to use when I also want to include MySQL in my app? In different tabs of the app, I want to display information from the user from the database.
@zajjajbinsana
@zajjajbinsana 18 күн бұрын
I would suggest clerk , from my knowledge. I know I am a lil too late at reply
@chaitanyasondur8483
@chaitanyasondur8483 4 ай бұрын
I must appreciate the content you are uploading! It really helped me to understand the concept of Auth Context in React Native. I need your help in this issue that I am facing: From 11:19, I have wrapped my Login component inside the AuthProvider, so Login can access the context. But from the Login page, I have a button to "create an account" which navigates to a Register component using Link from expo-router. I am unable to access the onRegister method and the state from the Register component and the error says "onRegister is not a function (it is undefined)". Additionally, when I wrap my Register component inside Auth Provider it works perfectly fine. But when I navigate to Login component it again says "onLogin is not a function (it is undefined)". I am not using the stack layout for Login and Register components. My initial screen is the Login screen and from there I have created Stack navigation for navigating to other pages. I assume that the authState will not be accessible from other pages too in my case. I have used all the imports and the useAuth() correctly as mentioned in this video.
@GeorgiTraykov-gw5jx
@GeorgiTraykov-gw5jx Ай бұрын
Should we get signed out automatically when the expiration of the jwt is due, or should we stay signed in?
@galaxies_dev
@galaxies_dev Ай бұрын
Depends on your system requirements. I'd kick out the user when the next request fails due do an expired token, not automatically when the time is up!
@GeorgiTraykov-gw5jx
@GeorgiTraykov-gw5jx Ай бұрын
@@galaxies_dev do you think setting up a listener would be good so when the token expires the user gets kicked out?
@andssuo517
@andssuo517 5 ай бұрын
hello, i am doing my project with expo router, not with stacknavigator, could i do the same in my main layout where is my login and my (tabs) which are already the home, etc?
@galaxies_dev
@galaxies_dev 5 ай бұрын
It works a bit different, you can check out the API and Auth course on Galaxies.dev or this video which comes close to it: kzbin.info/www/bejne/sJmZhJZnm9F-aMk
@belkocik
@belkocik 11 ай бұрын
Would you make a video how to implement this with global state management system a library called: JOTAI instead of React Context?
@galaxies_dev
@galaxies_dev 11 ай бұрын
Yeah looking into Zustand / Jotai is on my list!
@belkocik
@belkocik 11 ай бұрын
​@@galaxies_dev Nice. That video would help me to figure it out how to do it with Zustand :D
@WhiteRickRoss
@WhiteRickRoss 3 ай бұрын
Waiting for Expo router and JWT videoo
@GladsonReis
@GladsonReis 6 ай бұрын
Perfect !!! Please, where do I find the github of this project ?
@galaxies_dev
@galaxies_dev 6 ай бұрын
Tutorial is on Galaxies: galaxies.dev/react-native-login-jwt-auth
@miloniemaz1485
@miloniemaz1485 6 ай бұрын
What if I am using expo router ? And _layout file instead of App file that you are using ? (probably because the video is old)
@galaxies_dev
@galaxies_dev 6 ай бұрын
It works a bit different, but we got a full course on that on Galaxies as well: galaxies.dev/course/react-native-tanstack
@ubokabasi9782
@ubokabasi9782 2 ай бұрын
Can I still follow this video when using a bare react native project and not expo
@galaxies_dev
@galaxies_dev 2 ай бұрын
Yeah this is more about TS and RN than about Expo!
@ubokabasi9782
@ubokabasi9782 2 ай бұрын
@@galaxies_dev what alternative do you suggest I use to expo-secure-store,if I am using a bare react native project not with expo
@user-dc3vv1qr6k
@user-dc3vv1qr6k 7 ай бұрын
Could you please make a video on Expo Router with JWT Auth Context?
@galaxies_dev
@galaxies_dev 6 ай бұрын
I actually have a full course on it here: galaxies.dev/course/react-native-tanstack
@reacaosistemas5746
@reacaosistemas5746 6 ай бұрын
If using expo-router v2 ? Great job!
@miloniemaz1485
@miloniemaz1485 6 ай бұрын
hey man, did you manage to do it with expo ?
@reacaosistemas5746
@reacaosistemas5746 6 ай бұрын
@@miloniemaz1485 Consegui. Foi dificil mas deu certo
@miloniemaz1485
@miloniemaz1485 6 ай бұрын
@@reacaosistemas5746 I’m currently encoutering the same issue, could you maybe help me ?
@miloniemaz1485
@miloniemaz1485 6 ай бұрын
@@reacaosistemas5746 if you don’t want to help me, could you at least give some clues ? 🙏🙏
@sparsetech1671
@sparsetech1671 4 ай бұрын
Hi, I start working see your videos expo v2 project , But may be i am wrong , You don't proper guidline about expo v2 jwt authentication . I am upset.
@galaxies_dev
@galaxies_dev 4 ай бұрын
We do have a course on Galaxies.dev about Expo Router and Auth as well :)
@RamChickPig
@RamChickPig 3 ай бұрын
I'm getting authState doesn't exist in Layout. Any clue why. Thans.
@galaxies_dev
@galaxies_dev 3 ай бұрын
Did you add the context correctly? Does authState exist in the provider?
@TheLeonKwameShow
@TheLeonKwameShow 3 ай бұрын
How are persisting login on app refresh using context api?
@galaxies_dev
@galaxies_dev 2 ай бұрын
You can store the token in AsyncStorage and load/check it on app start again!
@Rocketos
@Rocketos 3 ай бұрын
Can i use this same login using expo router v2?
@galaxies_dev
@galaxies_dev 2 ай бұрын
It works a bit different with file-based routing, gotta do a video on that
@imdevbutok
@imdevbutok 3 ай бұрын
Não funcionou o login porque faltou um 's' em http. 🤡
@KhiemTranVietnamese
@KhiemTranVietnamese 8 ай бұрын
Can I have your codesnippets for the console.log() at 6:39
@argonjs
@argonjs 4 ай бұрын
that turbo console log extention for vscode
@user-om9ig2cr8k
@user-om9ig2cr8k 9 ай бұрын
PKCE?
@thomasfreedy
@thomasfreedy Ай бұрын
Hi, is there a github for this?
@galaxies_dev
@galaxies_dev Ай бұрын
It's available for Galaxies members here: galaxies.dev/react-native-login-jwt-auth
@FluxxSet
@FluxxSet 10 ай бұрын
hey , i think you forgot to mention link for source code ..
@galaxies_dev
@galaxies_dev 10 ай бұрын
It's linked below in the tutorial!
@balbalnyesh7684
@balbalnyesh7684 Жыл бұрын
hi i use your code and i make some different, because i not use expo. i use a package @react-native-async-storage/async-storage and when check the token i got null and in navigation not direct to login screen
@chiragkushwaha76
@chiragkushwaha76 Жыл бұрын
the api works, it didn't worked bcos of "http" make it "https"
@galaxies_dev
@galaxies_dev Жыл бұрын
Yeah I messed that up, sorry!
@algorithmprime
@algorithmprime 11 ай бұрын
@@galaxies_dev You did not add the api on the link below
@oztv_
@oztv_ 10 ай бұрын
@@algorithmprime yea
@quemasps
@quemasps 3 ай бұрын
The error occurred because the "authentication" header was not deleted, in logout() method. delete axios.defaults.headers.common['Authorization']
React Native Image Upload with Expo and PHP 🤩
18:31
Simon Grimm
Рет қаралды 11 М.
Omega Boy Past 3 #funny #viral #comedy
00:22
CRAZY GREAPA
Рет қаралды 34 МЛН
Super sport🤯
00:15
Lexa_Merin
Рет қаралды 20 МЛН
когда достали одноклассники!
00:49
БРУНО
Рет қаралды 3,6 МЛН
Ну Лилит))) прода в онк: завидные котики
00:51
#1 Create a Simple Login Screen in React Native
5:15
WithFrame
Рет қаралды 10 М.
10 Must-Have React Native Components 😎
10:12
Simon Grimm
Рет қаралды 38 М.
React Native Authentication with Firebase and Expo in 27 minutes
27:49
Made With Matt
Рет қаралды 249 М.
You might not need useEffect() ...
21:45
Academind
Рет қаралды 145 М.
Sign In with Google | React Native Expo | Tutorial 2023
36:07
Code with Beto
Рет қаралды 61 М.
Custom Authentication (JSON Web Tokens)
13:03
FlutterFlow
Рет қаралды 12 М.
Master React Native Authentication with Expo Router & Clerk
35:53
What is JWT? JSON Web Tokens Explained (Java Brains)
14:53
Java Brains
Рет қаралды 1 МЛН
iPhone 15 Pro vs Samsung s24🤣 #shorts
0:10
Tech Tonics
Рет қаралды 10 МЛН
5 НЕЛЕГАЛЬНЫХ гаджетов, за которые вас посадят
0:59
Кибер Андерсон
Рет қаралды 1 МЛН
Will the battery emit smoke if it rotates rapidly?
0:11
Meaningful Cartoons 183
Рет қаралды 1,9 МЛН
Эволюция телефонов!
0:30
ТРЕНДИ ШОРТС
Рет қаралды 6 МЛН