I´m doing a Node Js, Express easy and simple login using JWT for the first time, but i don´t know how to redirection from one route to another, In this image for example, in the /auth route, i make a user verification then i generate an JWT token and make 2 distinctions based on user´s parameters (admin and estado"), then I put in a header the token and send it as a response the image shows this, and how i put the token in the header and the output is this I see the token and a message in a json on the navegator
How do i make to instead showing me this json, it redirect me to /updateUsers route? that route already have a middleware to validate the token but i don´t really know how to go to there from /auth
Normally on an API the
authroute only returns the token, byres.jsonorres.cookie, if you're using CookieParser.The
res.headersis normally used when you want to authenticate to an existingAPI, so, in your case, you should just return the token as aJSONorcookie, and the Frontend will store it somewhere.The redirect you want, you should add it in a Middleware, where if:
The user has an authenticated token, you use
next()to continue the normal routingThe user hasn't an authenticated token or doesn't have a token, you use
res.redirect()to a route where you show a message likeUnauthorizedor something like this.So, basically, you should add this validation on the Middleware you have for authentication(Recommended), or create a new one for that.