feat: implement basic oauth authentication
This commit is contained in:
parent
9cfc799a9b
commit
d86c86e137
8 changed files with 120 additions and 0 deletions
2
src/app/api/auth/[...nextauth]/route.ts
Normal file
2
src/app/api/auth/[...nextauth]/route.ts
Normal 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
20
src/app/login/page.tsx
Normal 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
6
src/auth.ts
Normal 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],
|
||||
});
|
14
src/components/user/signin-button.tsx
Normal file
14
src/components/user/signin-button.tsx
Normal 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>
|
||||
);
|
||||
}
|
14
src/components/user/signout-button.tsx
Normal file
14
src/components/user/signout-button.tsx
Normal 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
1
src/middleware.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export { auth as middleware } from '@/auth';
|
Loading…
Add table
Add a link
Reference in a new issue