Skip to content

Passing accessToken from server to client #286

@thomasballinger

Description

@thomasballinger

I'd like to pass an initial accessToken from the server into the client AuthKit component.


For routes protected by middleware, the access token and user can be loaded in a server component with

const { accessToken } = await withAuth();

but if a child client component renders an AuthKitProvider, it will take a while for that AuthKitProvider to have an access token to use.

  ...
    <AuthKitProvider>
      <MyComponent/>
    </AuthKitProvider>
  ...

function MyComponent() {
  const { user, loading: userLoading } = useAuth();
  const { accessToken, loading: tokenLoading } = useAccessToken();
  if (accessToken) {
    return <MyApp accessToken={accessToken}/>
  }
  return <Loading />
}

Here we'll initially render <Loading /> . I'd like to pass the access token from the server through to my application to get a head start. I could do this without AuthKit's help,

  ...
    <AuthKitProvider>
      <MyComponent accessToken={accessToken} />
    </AuthKitProvider>
  ...

but it would be more convenient (I wouldn't need to wrap the AuthKit useAuth() in my own wrapper hook everywhere) if I could pass this token to AuthKit:

  ...
    <AuthKitProvider initialAccessToken={accessToken} >
      <MyComponent />
    </AuthKitProvider>
  ...

Is this something that makes sense in authkit-nextjs?

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