EveMonvie
EveMonvie

Reputation: 1

How do i redirect to my protected URL once i got the JWT?

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

Upvotes: 0

Views: 77

Answers (1)

Kobra
Kobra

Reputation: 1350

Normally on an API the auth route only returns the token, by res.json or res.cookie, if you're using CookieParser.

The res.headers is normally used when you want to authenticate to an existing API, so, in your case, you should just return the token as a JSON or cookie, and the Frontend will store it somewhere.

The redirect you want, you should add it in a Middleware, where if:

  1. The user has an authenticated token, you use next() to continue the normal routing

  2. The user hasn't an authenticated token or doesn't have a token, you use res.redirect() to a route where you show a message like Unauthorized or 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.

Upvotes: 0

Related Questions