From b0c0a633254ea9537304d69dcf1d54035bc1b5cc Mon Sep 17 00:00:00 2001 From: SeonghunYang Date: Wed, 27 Mar 2024 13:20:58 +0000 Subject: [PATCH] feat: Add SignInResponseSchema to user.command.ts --- app/business/user/user.command.ts | 26 ++++++++++++++++++++++++-- app/business/user/user.type.ts | 8 ++++---- app/business/user/user.validation.ts | 5 +++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/business/user/user.command.ts b/app/business/user/user.command.ts index b248d4d2..ace15408 100644 --- a/app/business/user/user.command.ts +++ b/app/business/user/user.command.ts @@ -5,7 +5,18 @@ import { API_PATH } from '../api-path'; import { SignUpRequestBody, SignInRequestBody } from './user.type'; import { httpErrorHandler } from '@/app/utils/http/http-error-handler'; import { BadRequestError } from '@/app/utils/http/http-error'; -import { SignUpFormSchema, SignInFormSchema } from './user.validation'; +import { SignUpFormSchema, SignInFormSchema, SignInResponseSchema } from './user.validation'; +import { cookies } from 'next/headers'; +import { z } from 'zod'; + +function isValidation>(data: any, schema: T): data is z.infer { + try { + schema.parse(data); + return true; + } catch (error) { + return false; + } +} export async function authenticate(prevState: FormState, formData: FormData): Promise { const validatedFields = SignInFormSchema.safeParse({ @@ -39,7 +50,18 @@ export async function authenticate(prevState: FormState, formData: FormData): Pr httpErrorHandler(response, result); - // cookie + if (isValidation(result, SignInResponseSchema)) { + cookies().set('accessToken', result.accessToken, { + httpOnly: true, + // secure: process.env.NODE_ENV === 'production', + path: '/', + }); + cookies().set('refreshToken', result.refreshToken, { + httpOnly: true, + secure: process.env.NODE_ENV === 'production', + path: '/', + }); + } } catch (error) { if (error instanceof BadRequestError) { // 잘못된 요청 처리 로직 diff --git a/app/business/user/user.type.ts b/app/business/user/user.type.ts index ff5a5eb3..c453cbc5 100644 --- a/app/business/user/user.type.ts +++ b/app/business/user/user.type.ts @@ -1,6 +1,9 @@ // https://stackoverflow.com/questions/76957592/error-only-async-functions-are-allowed-to-be-exported-in-a-use-server-file // server action 파일에서는 async function만 export 가능 +import { SignInResponseSchema } from './user.validation'; +import z from 'zod'; + export interface SignUpRequestBody { authId: string; password: string; @@ -13,7 +16,4 @@ export interface SignInRequestBody { password: string; } -export interface SignInResponse { - accessToken: string; - refreshToken: string; -} +export type SignInResponse = z.infer; diff --git a/app/business/user/user.validation.ts b/app/business/user/user.validation.ts index a640deb0..4e8573d7 100644 --- a/app/business/user/user.validation.ts +++ b/app/business/user/user.validation.ts @@ -5,6 +5,11 @@ export const SignInFormSchema = z.object({ password: z.string(), }); +export const SignInResponseSchema = z.object({ + accessToken: z.string(), + refreshToken: z.string(), +}); + export const SignUpFormSchema = z .object({ authId: z