From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Fetching authenticated user data

Fetching authenticated user data

Now, in this lecture, we will fetch the logged in users data which can be used to render the username later on. So first I will get the token from the cookie. Then I will verify the token and fetch the user data based on the token. So I will navigate to the auth actions JS file and create the server action by saying export async function get user data. Now I will verify token that is present in the cookie. So I will create another function by saying export async function JWT token verification. Now in the function, we will first get the token from the cookie. So I will say const token is equal to getCookie and the cookie name JWT underscore token. Then we will verify the JWT using the verifyJWT function and it will return the token data which contains the user ID. Now if token data is not obtained, then the verification will fail and will redirect to the slash login route, else we will return the token data which is actually the JWT object. Now in the get user data, we will get the…

Contents