Node.JS Express - How to redirect user to original request after refreshing access tokens?

60 views Asked by At

I'm trying to redirect a user back to their original request/data after they successfully refresh their access token.

For every resource request, I check the access token through a cookie in a middleware:

// The route to access the index page (must be authorized to access)
router.get('/', middlewares.verifyAccessToken(), indexController.indexPage);

When it gets to the verifyAccessToken() middleware, if the access token is valid, it will simply do next(), if it's invalid, it will redirect to /refresh:

// Access Token verification middleware to access resources
// Continues to the next request in the route if the access token is valid
// Continue to the /refresh route if the access token is expired

exports.verifyAccessToken = (redirect) => (req, res, next) => {
    const accessToken = req.cookies.accessToken;

    // No access token was even found
    // Inform the user of the error and they will have to log back in and generate a new access/refresh token pair
    if (!accessToken) {
        return res.status(401).json({ message: 'Verification Error: Access Token missing' });
    }

    // Verify the access token
    jwt.verify(accessToken, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
        if (err) {
            // The access token is no longer valid
            // Redirect the user to the /refresh route
            // The /refresh route will have access to the refreshToken through the cookie on this path
            return res.redirect('/refresh');

        } else {
            // The access token is valid
            // Extract some information about the user and pass it onto the next chain in the route
            req.decoded = decoded;
            next();
        }
    });
};

But now I don't know what to do in the /refresh route. The /refresh route has access to the refresh token, and I can successfully generate new tokens, but how do I get the user to go back to the original request? (in this case, accessing the index page)

In this case, I want it to redirect back to the next route in the chain, which would be the indexPage controller.

How do I get that "next()" back essentially from before I redirected them to "/refresh"

Is there a better way to go about this?

1

There are 1 answers

2
Nazrul Chowdhury On

You could store the original request URL before redirecting to the refresh route. You can then redirect the user back to this original URL after successfully refreshing the access token. Try something like this,

exports.verifyAccessToken = () => (req, res, next) => {
    const accessToken = req.cookies.accessToken;

    // Store the original request URL
    req.session.originalUrl = req.originalUrl;

    // Your existing verification logic
    // ...

    // Redirect to the /refresh route if the access token is expired
    return res.redirect('/refresh');
};
// handle token refresh
app.get('/refresh', (req, res) => {
    // Your token refresh logic
    // ...

    // Redirect back to the original URL
    const originalUrl = req.session.originalUrl || '/';
    delete req.session.originalUrl; // Clear the stored URL
    return res.redirect(originalUrl);
});