8base / react-sdk

Easy-to-use React SDK for working with 8base
4 stars 9 forks source link

usage with ssr/ nextjs #51

Open moklick opened 3 years ago

moklick commented 3 years ago

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.

Selnapenek commented 3 years ago

@moklick We have not yet planned SSR support in the roadmap. Feel free to do any PR.