Skip to content

usage with ssr/ nextjs #51

@moklick

Description

@moklick

At the moment it is not possible to create an auth client within a nextjs app because the @8base/web-auth0-auth-client package uses the window object which is not defined in a ssr context.

This is my code:

const origin = typeof window !== 'undefined' ? window.location.origin : '';
const authClient = Auth.createClient(
  {
    strategy: AUTH_STRATEGIES.WEB_8BASE,
    subscribable: true,
  },
  {
    clientId: process.env.NEXT_PUBLIC_APP_AUTH_CLIENT_ID,
    domain: process.env.NEXT_PUBLIC_APP_AUTH_CLIENT_ID,
    redirectUri: `${origin}/auth/callback/path`,
    logoutRedirectUri: `${origin}/`,
  }
);

It fails with an error that window is not defined.

The problem seems to be in web-auth0-auth-client/dist/WebAuth0AuthClient:

constructor(options, storage = window.localStorage, storageKey = 'auth')

when you check if the window exists, everything works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions