I created a registration system from a react native app, user got created, I used payload's login URL to authenticate user and generate token, user.json() in secure storage. Manually authenticate app with a token on the next open. For web apps Payload automatically send HTTP Only cookie. How does the behavior work in native environments?
Here is how it works in web's cookie environment:
// On mount, get user and set
useEffect(() => {
const fetchMe = async () => {
const result = await fetch(`${NEXT_PUBLIC_CMS_URL}/api/users/me`, {
// Make sure to include cookies with fetch
credentials: 'include',
}).then(req => req.json());
setUser(result.user || null);
console.log('THIS: ', result.user);
};
fetchMe();
}, []);
How to do this on react native when you have user token and details json saved in secure storage and you want to open the app logged in?
Hey @aayush214 do you still need need help here?
Hey @seanzubrickas, yes I need help, For now, I've moved on by simply checking if the token exists in encrypted storage and put that in state. I believe I should in some way be checking if that said token is valid or not?
I have essentially skipped /api/users/me call. Pretty sure it's not secure.
@aayush214 This would be a great place to use / verify JWT tokens
JWT libraries exist both on the web / node / other languages too
Payload provides JWT auth tokens. Using your Payload secret key, you can confirm that the token is non-tampered as well
@notchr isn't that what the /me end point does?
It creates an http-only cookie, which is essentially the JWT token
But you can verify the authenticity of a jwt token using a library, even on native
You take the token and the secret key and the library will be able to determine if it is valid
Ok. I'll give that a shot. Thanks for the help 😃
Of course! Be sure to read through the following to get a better grasp on the flow of jwt
https://www.npmjs.com/package/jsonwebtoken
In addition, Payload allows you to specify which collection or user data is included in a JWT token (apart from your default auth information)
Also, you using raw react native or something like expo?
I think frameworks like Expo has some helper libraries for authentication
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.