Skip to content

Commit

Permalink
feat: Add SignInResponseSchema to user.command.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghunYang committed Mar 27, 2024
1 parent 1b9b3c2 commit b0c0a63
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
26 changes: 24 additions & 2 deletions app/business/user/user.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends z.ZodObject<any>>(data: any, schema: T): data is z.infer<T> {
try {
schema.parse(data);
return true;
} catch (error) {
return false;
}
}

export async function authenticate(prevState: FormState, formData: FormData): Promise<FormState> {
const validatedFields = SignInFormSchema.safeParse({
Expand Down Expand Up @@ -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) {
// 잘못된 요청 처리 로직
Expand Down
8 changes: 4 additions & 4 deletions app/business/user/user.type.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +16,4 @@ export interface SignInRequestBody {
password: string;
}

export interface SignInResponse {
accessToken: string;
refreshToken: string;
}
export type SignInResponse = z.infer<typeof SignInResponseSchema>;
5 changes: 5 additions & 0 deletions app/business/user/user.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b0c0a63

Please sign in to comment.