first commit
This commit is contained in:
95
app/auth/actions.ts
Normal file
95
app/auth/actions.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
'use server'
|
||||
|
||||
import { cookies, headers } from 'next/headers'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { getToken } from 'next-auth/jwt'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import {
|
||||
applySessionCookie,
|
||||
encodeSessionJwt,
|
||||
fetchRefreshedBackendJwt,
|
||||
shouldRefreshBackendToken,
|
||||
} from '@/lib/backend-jwt-refresh'
|
||||
|
||||
const API_BASE = process.env.API_BASE_URL ?? 'http://localhost:8080'
|
||||
|
||||
export type AuthFormState = {
|
||||
error?: string
|
||||
success?: boolean
|
||||
message?: string
|
||||
}
|
||||
|
||||
export async function register(
|
||||
_prev: AuthFormState,
|
||||
formData: FormData
|
||||
): Promise<AuthFormState> {
|
||||
const body = {
|
||||
email: formData.get('email') as string,
|
||||
username: formData.get('username') as string,
|
||||
first_name: formData.get('first_name') as string,
|
||||
last_name: formData.get('last_name') as string,
|
||||
password: formData.get('password') as string,
|
||||
confirm_password: formData.get('confirm_password') as string,
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/v1/auth/register`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}))
|
||||
return { error: data?.error ?? 'Kayıt başarısız' }
|
||||
}
|
||||
|
||||
return { success: true, message: 'Kayıt başarılı. Lütfen giriş yapın.' }
|
||||
}
|
||||
|
||||
export async function logout(): Promise<void> {
|
||||
redirect('/api/auth/signout?callbackUrl=/auth/login')
|
||||
}
|
||||
|
||||
async function getJwtFromRequest() {
|
||||
const cookieStore = await cookies()
|
||||
const headersList = await headers()
|
||||
const secret = process.env.NEXTAUTH_SECRET ?? process.env.AUTH_SECRET
|
||||
const cookieMap = Object.fromEntries(cookieStore.getAll().map((c) => [c.name, c.value]))
|
||||
return getToken({
|
||||
req: {
|
||||
headers: headersList,
|
||||
cookies: cookieMap,
|
||||
} as unknown as Parameters<typeof getToken>[0]['req'],
|
||||
secret,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Backend access token’ı yeniler ve NextAuth session çerezini günceller.
|
||||
* Sadece credentials (backend refresh) oturumunda anlamlıdır.
|
||||
*/
|
||||
export async function refreshAccessToken(): Promise<string | null> {
|
||||
const token = await getJwtFromRequest()
|
||||
if (!token?.refreshToken) return null
|
||||
|
||||
if (!shouldRefreshBackendToken(token)) {
|
||||
return typeof token.accessToken === 'string' ? token.accessToken : null
|
||||
}
|
||||
|
||||
const next = await fetchRefreshedBackendJwt(token)
|
||||
if (!next?.accessToken) return null
|
||||
|
||||
const jwt = await encodeSessionJwt(next)
|
||||
const cookieStore = await cookies()
|
||||
applySessionCookie(cookieStore, jwt)
|
||||
|
||||
return next.accessToken as string
|
||||
}
|
||||
|
||||
export async function getAccessToken(): Promise<string | null> {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (session?.error === 'RefreshAccessTokenError') return null
|
||||
if (!session?.accessToken) return null
|
||||
return session.accessToken
|
||||
}
|
||||
115
app/auth/login/page.tsx
Normal file
115
app/auth/login/page.tsx
Normal file
@@ -0,0 +1,115 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { signIn } from 'next-auth/react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { AlertTriangle, Loader2 } from 'lucide-react'
|
||||
|
||||
export default function LoginPage() {
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [pending, setPending] = useState(false)
|
||||
const [providerPending, setProviderPending] = useState<null | 'google' | 'github'>(null)
|
||||
|
||||
async function onSubmit(formData: FormData) {
|
||||
setError(null)
|
||||
setPending(true)
|
||||
|
||||
const email = String(formData.get('email') ?? '')
|
||||
const password = String(formData.get('password') ?? '')
|
||||
|
||||
const result = await signIn('credentials', {
|
||||
email,
|
||||
password,
|
||||
redirect: false,
|
||||
})
|
||||
|
||||
if (!result || result.error) {
|
||||
setError('Giriş başarısız')
|
||||
setPending(false)
|
||||
return
|
||||
}
|
||||
|
||||
window.location.href = '/admin/users'
|
||||
}
|
||||
|
||||
async function onProviderLogin(provider: 'google' | 'github') {
|
||||
setError(null)
|
||||
setProviderPending(provider)
|
||||
await signIn(provider, { callbackUrl: '/admin/users' })
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex min-h-[calc(100vh-4rem)] items-center justify-center px-4 py-12">
|
||||
<Card className="w-full max-w-sm">
|
||||
<CardHeader className="text-center">
|
||||
<CardTitle className="text-2xl">Hoş Geldiniz</CardTitle>
|
||||
<CardDescription>Hesabınıza giriş yapın</CardDescription>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent>
|
||||
<div className="mb-4 grid grid-cols-1 gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={providerPending !== null}
|
||||
onClick={() => void onProviderLogin('google')}
|
||||
>
|
||||
{providerPending === 'google' && <Loader2 className="size-4 animate-spin" />}
|
||||
Google ile giris yap
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={providerPending !== null}
|
||||
onClick={() => void onProviderLogin('github')}
|
||||
>
|
||||
{providerPending === 'github' && <Loader2 className="size-4 animate-spin" />}
|
||||
GitHub ile giris yap
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="mb-4 flex items-center gap-3">
|
||||
<div className="h-px flex-1 bg-border" />
|
||||
<span className="text-xs text-muted-foreground">veya e-posta ile devam et</span>
|
||||
<div className="h-px flex-1 bg-border" />
|
||||
</div>
|
||||
|
||||
<form action={onSubmit} className="space-y-4">
|
||||
{error && (
|
||||
<div className="flex items-start gap-2 rounded-lg border border-destructive/30 bg-destructive/10 px-3 py-2.5 text-sm text-destructive">
|
||||
<AlertTriangle className="mt-0.5 size-4 shrink-0" />
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="email">E-posta</Label>
|
||||
<Input id="email" name="email" type="email" required autoComplete="email" placeholder="ornek@mail.com" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="password">Şifre</Label>
|
||||
<Input id="password" name="password" type="password" required autoComplete="current-password" placeholder="••••••••" />
|
||||
</div>
|
||||
|
||||
<Button type="submit" disabled={pending} className="w-full">
|
||||
{pending && <Loader2 className="size-4 animate-spin" />}
|
||||
{pending ? 'Giriş yapılıyor…' : 'Giriş Yap'}
|
||||
</Button>
|
||||
</form>
|
||||
</CardContent>
|
||||
|
||||
<CardFooter className="justify-center text-sm text-muted-foreground">
|
||||
Hesabınız yok mu?
|
||||
<Link href="/auth/register" className="font-medium text-primary underline-offset-4 hover:underline">
|
||||
Kayıt Ol
|
||||
</Link>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
87
app/auth/register/page.tsx
Normal file
87
app/auth/register/page.tsx
Normal file
@@ -0,0 +1,87 @@
|
||||
'use client'
|
||||
|
||||
import { useActionState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { register, type AuthFormState } from '../actions'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { AlertTriangle, Loader2 } from 'lucide-react'
|
||||
|
||||
export default function RegisterPage() {
|
||||
const [state, formAction, pending] = useActionState<AuthFormState, FormData>(
|
||||
register,
|
||||
{}
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="flex min-h-[calc(100vh-4rem)] items-center justify-center px-4 py-12">
|
||||
<Card className="w-full max-w-sm">
|
||||
<CardHeader className="text-center">
|
||||
<CardTitle className="text-2xl">Hesap Oluştur</CardTitle>
|
||||
<CardDescription>Bilgilerinizi girerek kayıt olun</CardDescription>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent>
|
||||
<form action={formAction} className="space-y-4">
|
||||
{state.error && (
|
||||
<div className="flex items-start gap-2 rounded-lg border border-destructive/30 bg-destructive/10 px-3 py-2.5 text-sm text-destructive">
|
||||
<AlertTriangle className="mt-0.5 size-4 shrink-0" />
|
||||
{state.error}
|
||||
</div>
|
||||
)}
|
||||
{state.success && (
|
||||
<div className="rounded-lg border border-green-500/30 bg-green-500/10 px-3 py-2.5 text-sm text-green-700 dark:text-green-400">
|
||||
{state.message ?? 'Kayıt başarılı. Lütfen giriş yapın.'}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="first_name">Ad</Label>
|
||||
<Input id="first_name" name="first_name" type="text" required />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="last_name">Soyad</Label>
|
||||
<Input id="last_name" name="last_name" type="text" required />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="username">Kullanıcı Adı</Label>
|
||||
<Input id="username" name="username" type="text" required autoComplete="username" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="email">E-posta</Label>
|
||||
<Input id="email" name="email" type="email" required autoComplete="email" placeholder="ornek@mail.com" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="password">Şifre</Label>
|
||||
<Input id="password" name="password" type="password" required autoComplete="new-password" placeholder="••••••••" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="confirm_password">Şifre Tekrar</Label>
|
||||
<Input id="confirm_password" name="confirm_password" type="password" required autoComplete="new-password" placeholder="••••••••" />
|
||||
</div>
|
||||
|
||||
<Button type="submit" disabled={pending} className="w-full">
|
||||
{pending && <Loader2 className="size-4 animate-spin" />}
|
||||
{pending ? 'Kayıt yapılıyor…' : 'Kayıt Ol'}
|
||||
</Button>
|
||||
</form>
|
||||
</CardContent>
|
||||
|
||||
<CardFooter className="justify-center text-sm text-muted-foreground">
|
||||
Zaten hesabınız var mı?
|
||||
<Link href="/auth/login" className="font-medium text-primary underline-offset-4 hover:underline">
|
||||
Giriş Yap
|
||||
</Link>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user