-
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
1 parent
c593a01
commit 6d490bc
Showing
11 changed files
with
315 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { AuthService } from "@/lib/auth-service"; | ||
import { cookies } from "next/headers"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function POST(request: Request) { | ||
try { | ||
const body = await request.json(); | ||
const { username, password } = body; | ||
|
||
const authResponse = await AuthService.login({ username, password }); | ||
|
||
console.log("authResponse:", authResponse); // 로그 추가 | ||
|
||
if (!authResponse) { | ||
return NextResponse.json( | ||
{ error: "잘못된 아이디 또는 비밀번호입니다." }, | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
// Set the access token cookie | ||
(await cookies()).set("accessToken", authResponse.accessToken, { | ||
// httpOnly: true, | ||
secure: process.env.NODE_ENV === "production", | ||
sameSite: "lax", | ||
maxAge: 24 * 60 * 60, // 24 hours | ||
}); | ||
|
||
return NextResponse.json({ | ||
user: authResponse.user, | ||
}); | ||
} catch (error) { | ||
console.error("Login error:", error); | ||
return NextResponse.json( | ||
{ error: "로그인 처리 중 오류가 발생했습니다." }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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,16 @@ | ||
import { cookies } from "next/headers"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function POST() { | ||
try { | ||
// 쿠키 삭제 | ||
(await cookies()).delete("accessToken"); | ||
return NextResponse.json({ success: true }); | ||
} catch (error) { | ||
console.error("Logout error:", error); | ||
return NextResponse.json( | ||
{ error: "로그아웃 처리 중 오류가 발생했습니다." }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import LoginPage from "../component/login-page"; | ||
import { MainLayout } from "@/components/layout/main-layout"; | ||
|
||
export default function Login() { | ||
return <LoginPage />; | ||
return ( | ||
<MainLayout> | ||
<LoginPage /> | ||
</MainLayout> | ||
); | ||
} |
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,32 @@ | ||
import { AuthResponse, LoginCredentials, User } from '@/types/auth'; | ||
import { TokenService } from './token-service'; | ||
|
||
export class AuthService { | ||
static async login(credentials: LoginCredentials): Promise<AuthResponse | null> { | ||
// 하드코딩된 사용자 인증 | ||
const { username, password } = credentials; | ||
|
||
let user: User | null = null; | ||
|
||
if (username === '1111' && password === '1111') { | ||
user = { id: '1', role: 'admin' }; | ||
} else if (username === '2222' && password === '2222') { | ||
user = { id: '2', role: 'user' }; | ||
} | ||
|
||
if (!user) { | ||
return null; | ||
} | ||
|
||
const accessToken = await TokenService.generateToken(user); | ||
|
||
return { | ||
user, | ||
accessToken | ||
}; | ||
} | ||
|
||
static async validateToken(token: string): Promise<User | null> { | ||
return TokenService.verifyToken(token); | ||
} | ||
} |
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,25 @@ | ||
import { User } from '@/types/auth'; | ||
import { jwtVerify, SignJWT } from 'jose'; | ||
|
||
const SECRET_KEY = new TextEncoder().encode(process.env.JWT_SECRET || 'your-secret-key'); | ||
|
||
export class TokenService { | ||
static async generateToken(user: User): Promise<string> { | ||
return new SignJWT({ sub: user.id, role: user.role }) | ||
.setProtectedHeader({ alg: 'HS256' }) | ||
.setExpirationTime('24h') | ||
.sign(SECRET_KEY); | ||
} | ||
|
||
static async verifyToken(token: string): Promise<User | null> { | ||
try { | ||
const { payload } = await jwtVerify(token, SECRET_KEY); | ||
return { | ||
id: payload.sub as string, | ||
role: payload.role as 'admin' | 'user' | ||
}; | ||
} catch { | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.