add auth, logout

This commit is contained in:
smanylov
2025-11-28 15:28:15 +07:00
parent 5c1a60645f
commit c4ece25c61
6 changed files with 125 additions and 34 deletions
+27 -15
View File
@@ -1,5 +1,21 @@
import * as z from 'zod'
export type FormState =
| {
errors?: {
name?: string[]
email?: string[]
password?: string[]
}
message?: string
}
| undefined
export type FormAction = (
state: FormState,
formData: FormData
) => Promise<FormState>
export const SignupFormSchema = z
.object({
full_name: z
@@ -21,18 +37,14 @@ export const SignupFormSchema = z
path: ['confirm_password'],
});
export type FormState =
| {
errors?: {
name?: string[]
email?: string[]
password?: string[]
}
message?: string
}
| undefined
export type FormAction = (
state: FormState,
formData: FormData
) => Promise<FormState>
export const loginFormSchema = z
.object({
email: z
.string()
.min(1, { error: 'Email must be not empty' })
.trim(),
password: z
.string()
.min(1, { error: 'Password must be not empty' })
.trim(),
})
+23 -4
View File
@@ -25,10 +25,10 @@ export async function decrypt(session: string | undefined = '') {
}
}
export async function createSession(userId: string) {
const expiresAt = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
const session = await encrypt({ userId, expiresAt })
const cookieStore = await cookies()
export async function createSession(token: string) {
const expiresAt = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
const session = await encrypt({ token, expiresAt });
const cookieStore = await cookies();
cookieStore.set('session', session, {
httpOnly: true,
@@ -39,6 +39,25 @@ export async function createSession(userId: string) {
})
}
export async function getSessionToken(): Promise<string | null> {
const cookieStore = await cookies();
const sessionCookie = cookieStore.get('session')?.value;
if (!sessionCookie) {
return null;
}
const payload = await decrypt(sessionCookie);
if (payload && typeof payload === 'object' && 'token' in payload) {
return payload.token as string;
}
return null;
}
export async function deleteSession() {
const cookieStore = await cookies()
cookieStore.delete('session')