Skip to content

Commit

Permalink
feat: 회원가입 후 로그인 처리 #10
Browse files Browse the repository at this point in the history
  • Loading branch information
yemsu committed Feb 19, 2024
1 parent 7858e9b commit 925b9fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
9 changes: 8 additions & 1 deletion src/components/auth/JoinForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client'

import { zodResolver } from '@hookform/resolvers/zod'
import { useRouter } from 'next/navigation'
import { useState } from 'react'
import { FieldErrors, useForm } from 'react-hook-form'
import { z } from 'zod'
Expand All @@ -19,6 +20,7 @@ import RequestButton from '@/components/RequestButton'
import NewEmailFieldArea from '@/components/auth/NewEmailFieldArea'

import { postSignUp } from '@/service/member'
import useAuthStore from '@/store/useAuthStore'

const defaultValues = {
newEmail: '',
Expand All @@ -31,6 +33,8 @@ const defaultValues = {
export type DefaultValues = typeof defaultValues

function JoinForm() {
const { login } = useAuthStore()
const router = useRouter()
const [isNicknameChecked, setIsNicknameChecked] = useState(false)

const form = useForm<z.infer<typeof joinSchema>>({
Expand Down Expand Up @@ -60,7 +64,10 @@ function JoinForm() {
if (res.state === RESPONSE_STATES.FAILED) {
throw new Error(ERROR_NOTICE.COMMON(res.error))
}
console.log('onSubmit', res)

const { token, email, nickname } = res.result
login(token, { email, nickname })
router.push('/')
}

function onErrors(errors: FieldErrors) {
Expand Down
18 changes: 10 additions & 8 deletions src/service/http.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ERROR_NOTICE, RESPONSE_STATES } from '@/constants/response'
import { ErrorResponse, HttpMethodResult } from '@/types/response'

export type ParamsObj = {
export interface ParamsObj {
[key: string]: string | number
}

export type ErrorCatch = {
export interface ErrorCatch {
response: {
data: string
status: string
Expand All @@ -16,15 +16,21 @@ export type ErrorCatch = {
config: string
}

export const httpHeaders = new Headers({
'Content-Type': 'application/json',
})

class Http {
service: string
url: string
authorization: string | null
headers: Headers

constructor(service: string) {
this.service = service
this.url = `${process.env.API_URL}/api/${service}`
this.authorization = null
this.headers = httpHeaders
}

async get<ResponseData>(
Expand All @@ -37,9 +43,7 @@ class Http {
const response = await fetch(uri, {
method: 'GET',
signal: this.getSignal(),
headers: {
'Content-Type': 'application/json',
},
headers: this.headers,
})
const { result } = await response.json()

Expand All @@ -62,9 +66,7 @@ class Http {
const response = await fetch(uri, {
method: 'POST',
signal: this.getSignal(),
headers: {
'Content-Type': 'application/json',
},
headers: this.headers,
body,
})
const { result } = await response.json()
Expand Down
1 change: 1 addition & 0 deletions src/service/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface MemberResponse {
id: number
email: string
nickname: string
token: string
}
export const postSignUp = (payload: SignupPayload) => {
return http.post<MemberResponse, SignupPayload>(`/signup`, payload)
Expand Down
30 changes: 22 additions & 8 deletions src/store/useAuthStore.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import { useInfiniteQuery } from '@tanstack/react-query'
import { create } from 'zustand'

import { httpHeaders } from '@/service/http'

const TOKEN_STORAGE_NAME = 'TERRACOTTA_TOKEN'
const USERINFO_STORAGE_NAME = 'TERRACOTTA_USER_INFO'

type UserInfo = {
email: string
nickname: string
}
type State = {
isLogin: boolean
userInfo: {
email: string
nickname: string
} | null
isLogin: boolean | undefined
userInfo: UserInfo | undefined
}

type Action = {
setIsLogin: (isLogin: State['isLogin']) => void
setUserInfo: (isLogin: State['userInfo']) => void
login: (token: string, userInfo: UserInfo) => void
}

const useAuthStore = create<State & Action>((set) => ({
isLogin: false,
userInfo: null,
const useAuthStore = create<State & Action>((set, get) => ({
isLogin: undefined,
userInfo: undefined,
setIsLogin: (isLogin) => set(() => ({ isLogin })),
setUserInfo: (userInfo) => set(() => ({ userInfo })),
login: (token: string, userInfo: UserInfo) => {
sessionStorage.setItem(TOKEN_STORAGE_NAME, token)
sessionStorage.setItem(USERINFO_STORAGE_NAME, JSON.stringify(userInfo))
httpHeaders.set('Authorization', token)
set(() => ({ isLogin: true, userInfo }))
},
}))

export default useAuthStore

0 comments on commit 925b9fb

Please sign in to comment.