-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { LoginForm } from '@/module/auth/components/login' | ||
import { getSession } from '@/module/auth/lib' | ||
import { PropsWithChildren } from 'react' | ||
|
||
export default async function Layout({ children }: PropsWithChildren) { | ||
const session = await getSession() | ||
if (!session) return <LoginForm /> | ||
return <>{children}</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Loading() { | ||
return <div>Loading user..</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getSession, logout } from '@/module/auth/lib' | ||
|
||
export default async function AuthPage() { | ||
const session = await getSession() | ||
return ( | ||
<div> | ||
hello {session?.user?.name} | ||
<form | ||
action={async () => { | ||
'use server' | ||
await logout() | ||
}} | ||
> | ||
<button type="submit">Logout</button> | ||
</form> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { NextRequest } from 'next/server' | ||
import { updateSession } from '@/module/auth/lib' | ||
|
||
export async function middleware(request: NextRequest) { | ||
return await updateSession(request) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { redirect } from 'next/navigation' | ||
import { getSession, login, logout } from '../lib' | ||
import { Stack } from '@/components/primitives/stack' | ||
|
||
export async function LoginForm() { | ||
const session = await getSession() | ||
return ( | ||
<Stack className='text-sm'> | ||
<form | ||
action={async (formData) => { | ||
'use server' | ||
await login(formData) | ||
// redirect('/bible/system/auth') | ||
}} | ||
> | ||
<input className='p-xs' type="email" name="email" placeholder="Email" /> | ||
<br /> | ||
<br /> | ||
<button type="submit">Login</button> | ||
</form> | ||
<form | ||
action={async () => { | ||
'use server' | ||
await logout() | ||
redirect('/') | ||
}} | ||
> | ||
<button type="submit">Logout</button> | ||
</form> | ||
<pre>{JSON.stringify(session, null, 2)}</pre> | ||
</Stack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Text } from '@/components/typography/text' | ||
import { getSession } from '../lib' | ||
import { cn } from '@/lib/utils' | ||
import { Stack } from '@/components/primitives/stack' | ||
|
||
export const UserCard = async () => { | ||
const session = await getSession() | ||
if (!session) return null | ||
return ( | ||
<Stack gap="xs" className={cn('p-sm border rounded-md animate-in slide-in-from-top overflow-hidden')}> | ||
<Text>{session?.user.name}</Text> | ||
<Text tone="muted">{session?.user.email}</Text> | ||
</Stack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { SignJWT, jwtVerify } from 'jose' | ||
import { cookies } from 'next/headers' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import { Session } from './types' | ||
|
||
const secretKey = 'secret' | ||
const key = new TextEncoder().encode(secretKey) | ||
const expires = new Date(Date.now() + 86400 * 1000) // 86400 sec / 1 day | ||
|
||
export async function encrypt(payload: any) { | ||
return await new SignJWT(payload) | ||
.setProtectedHeader({ alg: 'HS256' }) | ||
.setIssuedAt() | ||
.setExpirationTime('1 day from now') | ||
.sign(key) | ||
} | ||
|
||
export async function decrypt(input: string): Promise<any> { | ||
const { payload } = await jwtVerify(input, key, { | ||
algorithms: ['HS256'], | ||
}) | ||
return payload | ||
} | ||
|
||
export async function login(formData: FormData) { | ||
// Verify credentials && get the user | ||
|
||
const user = { email: formData.get('email'), name: 'John' } | ||
|
||
// Create the session | ||
const session = await encrypt({ user, expires }) | ||
|
||
// Save the session in a cookie | ||
cookies().set('session', session, { expires, httpOnly: true }) | ||
} | ||
|
||
export async function logout() { | ||
// Destroy the session | ||
cookies().set('session', '', { expires: new Date(0) }) | ||
} | ||
|
||
export async function getSession(): Promise<Session | null> { | ||
const session = cookies().get('session')?.value | ||
if (!session) return null | ||
return await decrypt(session) | ||
} | ||
|
||
export async function updateSession(request: NextRequest) { | ||
const session = request.cookies.get('session')?.value | ||
if (!session) return | ||
|
||
// Refresh the session so it doesn't expire | ||
const parsed = await decrypt(session) | ||
parsed.expires = expires | ||
const res = NextResponse.next() | ||
res.cookies.set({ | ||
name: 'session', | ||
value: await encrypt(parsed), | ||
httpOnly: true, | ||
expires: parsed.expires, | ||
}) | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type User = { | ||
email: string | null | ||
name: string | ||
} | ||
|
||
export type Session = { | ||
expires: string | ||
iat: number | ||
exp: number | ||
user: User | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters