authts / oidc-client-ts

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications
https://authts.github.io/oidc-client-ts/
Apache License 2.0
1.33k stars 198 forks source link

Error when try to use userManager.signInRedirect #1314

Open RCPOliveira opened 9 months ago

RCPOliveira commented 9 months ago

When I try to use the signInRedirect I'm getting the error ReferenceError: window is not defined. Can you help me identify what I'm doing wrong?

// Create a UserManager instance with the OpenID Connect settings
  const userManager = new UserManager({
    authority: `https://${sessionConfig.configurable_url}/oauth2/v1/authorize`, // The issuer URL of the OpenID Connect provider
    client_id: sessionConfig.client_id, // The client ID registered with the provider
    client_secret: sessionConfig.client_secret, // The client secret registered with the provider
    redirect_uri: sessionConfig.callback_url, // The redirect URI after authentication
    response_type: 'code', // The response type for the authorization code flow with PKCE
    scope: 'openid email', // The scope of the requested claims
  });

  // SERVICES
  /**
   * getting the login form link
   */
  app.get('/login', async (req, res) => {
    try {
      await userManager.signinRedirect();
    } catch (error) {
      console.log(error);
    }
  });
pamapa commented 9 months ago

Your code looks like back-end code, this library has been designed for front-ends (running in browser where you have a global window). However its should also be possible to run this in back-end call, it just means a little more effort on your side.

Maybe your problem is : https://github.com/authts/oidc-client-ts/blob/9f55da27fd6b6965f4a871a2b16fcee441235120/src/UserManager.ts#L188

You will need to change the default parameter to something useful in your case.