import { Callout } from "nextra/components" import { Screenshot } from "@/components/Screenshot" import { Code } from "@/components/Code"
To signin your users, make sure you have at least one authentication method setup. You then need to build a button which will call the sign in function from your Auth.js framework package.
import { signIn } from "@/auth.ts"
export function SignIn() {
return (
<form
action={async () => {
"use server"
await signIn()
}}
>
<button type="submit">Sign in</button>
</form>
)
}
In a client component, you can still use the imports from next-auth/react
as they are marked with the "use client;"
directive.
import { signIn } from "next-auth/react"
export function SignIn() {
return <Button onClick={() => signIn()}>Sign In</Button>
}
</Code.Next>
<Code.NextClient>
import { signIn } from "next-auth/react"
export function SignIn() {
return <Button onClick={() => signIn()}>Sign In</Button>
}
</Code.NextClient>
<Code.Svelte>
The SvelteKit client supports two signin and signout methods, one server-side using Form Actions, and one client-side using requests and redirects.
To signin your users using a SvelteKit form action, we can use the SignIn
component exported from @auth/sveltekit/components
.
<script lang="ts">
import { SignIn } from "@auth/sveltekit/components"
</script>
<header>
<nav>
<SignIn>
<span slot="submitButton">Sign In with GitHub</span>
</SignIn>
</nav>
</header>
This requires a server action at /signin
, this path can be customized with the signInPage
prop on the SignIn
component.
import { signIn } from "../../auth"
import type { Actions } from "./$types"
export const actions: Actions = { default: signIn }
Client-side is a bit simpler as we just need to import a button on:click
handler from @auth/sveltekit/client
.
<script lang="ts">
import { signIn } from '@auth/sveltekit/client'
</script>
<div>
<nav>
<img src="/img/logo.svg" alt="Company Logo" />
<button on:click={signIn}>Signin</button>
</nav>
</div>
Just like in other frameworks, you can also pass a provider to the signIn
function which will attempt to login directly with that provider.
</Code.Svelte>
<Code.Express>
The Express package runs server-side and therefore it doesn't make sense to create a "SignIn button component". However, to signin or signout with Express, send a request to the appropriate REST API Endpoints from your client (i.e. /auth/signin
, /auth/signout
, etc.).
</Code.Express>
<Code.Fastify>
The Fastify package runs server-side and therefore it doesn't make sense to create a "SignIn button component". However, to signin or signout with Fastify, send a request to the appropriate REST API Endpoints from your client (i.e. /auth/signin
, /auth/signout
, etc.).
</Code.Fastify>
You can also pass a provider to the signIn
function which will attempt to login directly with that provider. Otherwise, when clicking this button in your application, the user will be redirected to the configured sign in page. If you did not setup a custom sign in page, the user will be redirected to the default signin page at /[basePath]/signin
.
import DefaultSignInPage from "../../../public/img/getting-started/default-signin-page.webp"
Once authenticated, the user will be redirected back to the page they started the signin from. If
you want the user to be redirected somewhere else after sign in (.i.e /dashboard
), you can do so
by passing the target URL as redirectTo
in the sign-in options.
import { signIn } from "../../auth.ts"
export function SignIn() {
return (
<form
action={async () => {
"use server"
await signIn("github", { redirectTo: "/dashboard" })
}}
>
<button type="submit">Sign in</button>
</form>
)
}
</Code.Next>
<Code.NextClient>
import { signIn } from "next-auth/react"
export function SignIn() {
return (
<Button onClick={() => signIn("github", { redirectTo: "/dashboard" })}>
Sign In
</Button>
)
}
</Code.NextClient>
<Code.Svelte>
<script lang="ts">
import { page } from "$app/stores"
import { SignIn } from "@auth/sveltekit/components"
</script>
<header>
<nav>
<SignIn
options={{
redirectTo: $page.data.redirectTo
? `/${decodeURIComponent($page.data.redirectTo).slice(1)}`
: `/dashboard`,
}}
className="w-full"
>
<span slot="submitButton">Sign in</span>
</SignIn>
</nav>
</header>
</Code.Svelte>
Signing out can be done similarly to signing in. Most frameworks offer both a client-side and server-side method for signing out as well.
To sign out users with a form action, you can build a button that calls the exported signout function from your Auth.js config.
import { signOut } from "../../auth.ts"
export function SignOut() {
return (
<form
action={async () => {
"use server"
await signOut()
}}
>
<button type="submit">Sign Out</button>
</form>
)
}
Again, in a client component, you can use the next-auth/react
imports as they are marked with the "use client;"
directive.
import { signOut } from "next-auth/react"
export function SignOut() {
return <Button onClick={() => signOut()}>Signout</Button>
}
</Code.Next>
<Code.NextClient>
import { signOut } from "next-auth/react"
export function SignOut() {
return <button onClick={() => signOut()}>Sign Out</button>
}
</Code.NextClient>
<Code.Svelte>
SvelteKit supports both server and client-side methods for signing out as well.
To use the SvelteKit form action for signing out, we can use the SignOut
component exported from @auth/sveltekit/components
.
<script lang="ts">
import { SignOut } from "@auth/sveltekit/components"
</script>
<header>
<nav>
<SignOut>
<span slot="submitButton">Signout</span>
</SignIn>
</nav>
</header>
This requires a server action at /signout
, this path can be customized with the signOutPage
prop on the <SignOut>
component.
import { signOut } from "../../auth"
import type { Actions } from "./$types"
export const actions: Actions = { default: signOut }
Client-side is a bit simpler as we just need to import a button on:click
handler from @auth/sveltekit/client
.
<script lang="ts">
import { signOut } from '@auth/sveltekit/client'
</script>
<div>
<nav>
<img src="/img/logo.svg" alt="Company Logo" />
<button on:click={signOut}>Signout</button>
</nav>
</div>
</Code.Svelte>
<Code.Express>
The Express package runs server-side and therefore it doesn't make sense to create a "SignIn button component". However, to signin or signout with Express, send a request to the appropriate REST API Endpoints from your client (i.e. /auth/signin
, /auth/signout
, etc.).
</Code.Express>
<Code.Fastify>
The Fastify package runs server-side and therefore it doesn't make sense to create a "SignIn button component". However, to signin or signout with Fastify, send a request to the appropriate REST API Endpoints from your client (i.e. /auth/signin
, /auth/signout
, etc.).
</Code.Fastify>