feat: implement basic oauth authentication

This commit is contained in:
Maximilian Liebmann 2025-04-18 21:05:49 +02:00 committed by SomeCodecat
parent 9cfc799a9b
commit d86c86e137
8 changed files with 120 additions and 0 deletions

View file

@ -0,0 +1,2 @@
import { handlers } from '@/auth'; // Referring to the auth.ts we just created
export const { GET, POST } = handlers;

20
src/app/login/page.tsx Normal file
View file

@ -0,0 +1,20 @@
import { auth } from '@/auth';
import SignIn from '@/components/user/signin-button';
import { SignOut } from '@/components/user/signout-button';
export default async function Login() {
const session = await auth();
return (
<div>
<h1>Login</h1>
{!session?.user ? (
<SignIn></SignIn>
) : (
<>
<h2>Hallo {session.user.name}</h2>
<SignOut></SignOut>
</>
)}
</div>
);
}

6
src/auth.ts Normal file
View file

@ -0,0 +1,6 @@
import NextAuth from 'next-auth';
import Authentik from 'next-auth/providers/authentik';
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [Authentik],
});

View file

@ -0,0 +1,14 @@
import { signIn } from '@/auth';
export default function SignIn() {
return (
<form
action={async () => {
'use server';
await signIn('authentik');
}}
>
<button type='submit'>Signin with Authentik</button>
</form>
);
}

View file

@ -0,0 +1,14 @@
import { signOut } from '@/auth';
export function SignOut() {
return (
<form
action={async () => {
'use server';
await signOut();
}}
>
<button type='submit'>Sign Out</button>
</form>
);
}

1
src/middleware.ts Normal file
View file

@ -0,0 +1 @@
export { auth as middleware } from '@/auth';