panva / openid-client

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes
MIT License
1.83k stars 392 forks source link

TypeError: client must be an instance of openid-client Client #168

Closed CrypticWoodWhite closed 5 years ago

CrypticWoodWhite commented 5 years ago

I'm getting a client typeError.

The relevant code is:

var googleClient;
Issuer.discover('https://accounts.google.com/.well-known/openid-configuration')
    .then((googleIssuer: { issuer: any; metadata: any; Client: any; }) => {
        console.log('Discovered issuer %s %O', googleIssuer.issuer, googleIssuer.metadata);
        googleClient = new googleIssuer.Client({
            client_id: process.env.GOOGLE_ID,
            client_secret: process.env.GOOGLE_SECRET,
            redirect_uris: [list of links here],
            response_types: ['code token id_token'],
        });
        console.log(googleClient);
    });

console.log(googleClient);

const params = {
    client_id: process.env.GOOGLE_ID,
    response_type: 'code token id_token',
    scope: 'openid profile email',
    nonce: generators.nonce(),
    redirect_uri: 'link here',
    state: generators.state(),
    prompt: 'select_account',
    display: 'popup',
    login_hint: 'sub',
};

const verify = ( tokenSet: any, userInfo: any, done: (arg0: null, arg1: any) => void ) => {
    console.log('USERINFO: ', userInfo);
    console.log('TOKENSET: ', tokenSet);
    return done(null, tokenSet);
};

const options = {
    googleClient,
    params,
};
Passport.use('openid-client', new Strategy( options, verify ));

googleClient is returned as undefined. I'm expecting to get a Client instance. The error is:

[1] TypeError: client must be an instance of openid-client Client [1] at new OpenIDConnectStrategy (C:\Users..........\node_modules\openid-client\lib\passport_strategy.js:37:11)

panva commented 5 years ago

You're not passing a client property here.

const options = {
  googleClient,
  params,
};

should be

const options = {
  client: googleClient,
  params,
};
CrypticWoodWhite commented 5 years ago

I'm still getting the same error

panva commented 5 years ago

Well, you ought to wait for async operations, such as the discovery, to finish first your variable is undefined because you call for new strategy instance before the client is assigned to it.

CrypticWoodWhite commented 5 years ago

Ah I see, I put all the code inside the .then(.....) which then worked. I had thought that just assigning googleClient in there was enough. Thanks!