Install
Adding MediaKit/auth to SolidStart
Install
pnpm add @solid-mediakit/auth@latest @auth/core@latest
Env variables
The env variables are required for the app to work!
AUTH_URL
- Url for the auth api endpoint:http://localhost:3000
VITE_AUTH_PATH
-auth api endpoint:/api/auth
After adding the VITE_AUTH_PATH
env, make sure to also add it to the config:
import { type SolidAuthConfig } from '@solid-mediakit/auth'
export const authOpts: SolidAuthConfig = {
...
basePath: import.meta.env.VITE_AUTH_PATH,
}
SessionProvider
In order to update the session context accordingly, the entire app needs to be wrapped with a SessionProvider, this is a SolidJS context that will hold the current session.
// app.tsx
// @refresh reload
import { MetaProvider, Title } from '@solidjs/meta'
import { Router } from '@solidjs/router'
import { FileRoutes } from '@solidjs/start/router'
import { Suspense } from 'solid-js'
import './app.css'
import { SessionProvider } from '@solid-mediakit/auth/client'
export default function App() {
return (
<Router
root={(props) => (
<MetaProvider>
<Title>SolidStart - Basic</Title>
<Suspense>
<SessionProvider>{props.children}</SessionProvider>
</Suspense>
</MetaProvider>
)}
>
<FileRoutes />
</Router>
)
}
Now that everything is set up on the client, we can start setting up the server.
API Route
Go ahead to routes/api/auth/[...solidauth].ts
, since we are using AuthJS, we can access all of its well known providers (Google, Github, etc). Make sure you have these enviroment variables set up:
GITHUB_ID
- Github Client IDGITHUB_SECRET
- Github Client SecretAUTH_SECRET
- Secret used to sign token:openssl rand -base64 32
// routes/api/auth/[...solidauth].ts
import { SolidAuth, type SolidAuthConfig } from '@solid-mediakit/auth'
import GitHub from '@auth/core/providers/github'
export const authOpts: SolidAuthConfig = {
providers: [
GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
],
debug: false,
basePath: import.meta.env.VITE_AUTH_PATH,
}
export const { GET, POST } = SolidAuth(authOpts)
Now that everything is configured correctly, we can go ahead and start using the client functions: