Skip to content

Auth0 SDK for Solid Single Page Applications (SPA)

License

Notifications You must be signed in to change notification settings

cdegrel/auth0-solid

Repository files navigation

auth0-solid

Auth0 SDK for Solid Application.

Table of Contents

Installation

pnpm install auth0-solid

Getting Started

// src/index.tsx
import { Auth0Provider } from 'auth0-solid'
import { render } from 'solid-js/web'

import App from './App'

render(
  () => (
    <Auth0Provider
      clientId={import.meta.env.VITE_APP_CLIENT_ID}
      domain={import.meta.env.VITE_APP_DOMAIN}
      authorizationParams={{
        audience: import.meta.env.VITE_APP_AUDIENCE,
        scope: 'profile email',
        redirect_uri: window.location.origin,
      }}
    >
      <App />
    </Auth0Provider>
  ),
  document.getElementById('root'),
)
// src/App.tsx
import { useAuth0 } from 'auth0-solid'
import { createSignal, Match, Switch } from 'solid-js'

const App = () => {
  const {
    state: auth,
    loginWithRedirect,
    logout,
    getAccessTokenSilently,
  } = useAuth0()

  const [token, setToken] = createSignal('')

  return (
    <Switch
      fallback={
        <div>
          <button onClick={() => loginWithRedirect()}>Login</button>
        </div>
      }
    >
      <Match when={auth.isLoading}>Loading...</Match>
      <Match when={auth.isAuthenticated}>
        <div>
          <h1>Hello {auth.user?.name}</h1>
          <pre>{token()}</pre>
          <button
            onClick={async () => setToken(await getAccessTokenSilently())}
          >
            Get access token
          </button>
          <button onClick={() => logout()}>Logout</button>
        </div>
      </Match>
    </Switch>
  )
}

export default App

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

Auth0 SDK for Solid Single Page Applications (SPA)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published