From the course: Next.js Ecommerce: Build a Shopping Platform from Scratch

Unlock this course with a free trial

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

Customize token with JWT callback

Customize token with JWT callback

All right, guys, so we have a working authentication system. And there is one more thing I want to do before we end this section and go into the shopping cart. And that is add a JWT callback. Because right now, what happens is we get a JSON Web Token sent to the client when we log in. And then that token is sent to the server with every request. And that's what establishes and keeps that authentication and how it knows which user we are. Now, by default, that token has some data in it, like the user ID. But if you want to customize that, which we do, for instance, we want to add the user role to the token, because we're going to need that later, then you have to create a callback called JWT. Now, this callback is called whenever a JSON web token is created, so when you sign in, or updated whenever a session is accessed. And basically, we can set values to it. In this case, they're setting an access token and an ID set to account access token profile ID, most likely the Google…

Contents