Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TokenError: Code is invalid or expired #974

Open
xonarin opened this issue Mar 20, 2023 · 2 comments
Open

TokenError: Code is invalid or expired #974

xonarin opened this issue Mar 20, 2023 · 2 comments

Comments

@xonarin
Copy link

xonarin commented Mar 20, 2023

Hello.

I use authorization method - https://www.passportjs.org/packages/passport-vkontakte/

And authorization works for me, but sometimes an error pops up and the application crashes.

What could be the problem? Here is the code.


app.get(
    '/auth/vk',
    (_0, res, next) => {
        const now = Date.now();

        if (now - lastVkAuth < 1000) {
            res.redirect('/');
        } else {
            lastVkAuth = Date.now();
            next();
        }
    },
    passport.authenticate('vkontakte', {
        failureRedirect: '/',
        session: false,
    }),
);

app.get(
    '/auth/vk/return',
    passport.authenticate('vkontakte', {
        failureRedirect: '/',
        session: false,
    }),
    async (req, res) => {
        if (req.user && req.authInfo) {
            const expiresIn = 60 * 60 * 24 * 180; // 180 days
            const token = jwt.sign(req.user, config.auth.jwt.secret, { expiresIn });
            res.cookie(config.auth.tokenKey, token, { maxAge: 1000 * expiresIn });

            // Set referrer if it is define in cookies
            const { refId } = req.cookies;
            if (refId && req.authInfo.isRegistered) {
                await repositories.users.setReferrer(req.user.id, refId);
                res.clearCookie('refId');
            }
        }
        res.redirect('/');
    },
);

Error screen - https://ibb.co/mJB1JsY

@xonarin
Copy link
Author

xonarin commented Mar 20, 2023

How to handle TokenError ?

Passing err to the argument and the if(err) condition didn't help anywhere.

@YasharF
Copy link

YasharF commented Jul 21, 2023

It sounds like your token is just expiring. You would need to refresh it with something like passport-oauth2-refresh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants