Closed4

Sveltekit Auth.jsでOAuthする

3w36zj63w36zj6

Configuring Auth.js

https://authjs.dev/getting-started/providers/oauth-tutorial?frameworks=sveltekit

src/hooks.server.ts
import { SvelteKitAuth } from "@auth/sveltekit"
import GitHub from "@auth/core/providers/github"
import { GITHUB_ID, GITHUB_SECRET } from "$env/static/private"

export const handle = SvelteKitAuth({
  providers: [GitHub({ clientId: GITHUB_ID, clientSecret: GITHUB_SECRET })],
})
src/routes/+layout.server.ts
import type { LayoutServerLoad } from "./$types"

export const load: LayoutServerLoad = async (event) => {
  return {
    session: await event.locals.auth(),
  }
}
src/routes/+page.svelte
<script>
  import { page } from "$app/stores"
  import { signIn, signOut } from "@auth/sveltekit/client"
</script>

<h1>SvelteKit Auth Example</h1>
<p>
  {#if $page.data.session?.user}
  <p>Signed in as {$page.data.session.user.email}</p>
  <button on:click={() => signOut()} class="button">Sign out</button>
  <img alt={$page.data.session.user.name} src={$page.data.session.user.image} />
{:else}
  <p>Not signed in.</p>
  <button on:click={() => signIn("github")}>Sign in</button>
{/if}
</p>
このスクラップは2024/03/08にクローズされました