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

How to use and decode state parameter #80

Open
eric72 opened this issue Jun 5, 2021 · 4 comments
Open

How to use and decode state parameter #80

eric72 opened this issue Jun 5, 2021 · 4 comments

Comments

@eric72
Copy link

eric72 commented Jun 5, 2021

I added in my app the state parameter like that:
constructor() {
super({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_SECRET,
callbackURL: 'my callback url',
state: JSON.stringify({ "redirectUrl" : "test" }),
scope: ['email', 'profile'],
});
}

now when i get it from my request i get my state encoded.
I try to decode it and get my : { "redirectUrl" : "test" } .
But it's not working.
How can i get it ?
I already tried this :
Buffer.from(req.query.state, 'base64').toString('ascii');
Buffer.from(req.query.state).toString('utf-8'));
Buffer.from(req.query.state).toString('ascii'));

But i can't decode it and get my : { "redirectUrl" : "test" } .
Is it possible to get it ?

Thanks,

@adrianotadao
Copy link

adrianotadao commented Jun 9, 2022

hey @eric72 how did you make it work?

@eric72
Copy link
Author

eric72 commented Jun 14, 2022

I didn't used it at the end for the moment, did you resolved the same issue ?

@adrianotadao
Copy link

hm it worked for me:

const authUser = async (
  req: Request,
  _accessToken: unknown,
  _refreshToken: unknown,
  authProfile: unknown,
): Promise<void> => {
  JSON.parse(req.query.state)
};

passport.use(new PassportGoogle.Strategy(
  {
    clientID: params.clientId,
    clientSecret: params.clientSecret,
    callbackURL: '/users/auth/google/callback',
    scope: ['profile', 'email'],
    passReqToCallback: true,
  }, authUser),
);

router.get('/auth/google', (req, res, next) => {
  const state = JSON.stringify(parsedSchema.data);
  passport.authenticate('google', { state })(req, res, next);
});

router.get('/auth/google/callback', (req, res, next) => {
  passport.authenticate('google', { authInfo: true }, async (err, args: OauthPayload) => {
    console.log(args.state.origin);
  })(req, res, next);
});

@louisnk
Copy link

louisnk commented Jun 28, 2022

This fails in Typescript as state is not a defined parameter of the AuthenticateOptions interface. Seems like a similar issue to fastify/fastify-passport#51

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

3 participants