Skip to content

Commit ebb5d2d

Browse files
장아영장아영
authored andcommitted
[#120] ✨ create sign-up query
1 parent ada42ae commit ebb5d2d

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
NEXT_PUBLIC_API_BASE_URL=http://43.202.50.174:8080/
1+
# NEXT_PUBLIC_API_BASE_URL=http://localhost:3000

src/app/(pages)/sign-in/page.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use client'
22

3+
import { useRouter } from 'next/navigation'
4+
35
import { SubmitHandler, useForm } from 'react-hook-form'
46

57
import { SignInRequest } from '@/types/auth.types'
68

79
export default function LoginPage(): JSX.Element {
10+
const router = useRouter()
811
const {
912
register,
1013
handleSubmit,
@@ -28,6 +31,7 @@ export default function LoginPage(): JSX.Element {
2831
const result = await response.json()
2932
console.log('Login successful:', result)
3033
alert('로그인 성공')
34+
router.push('/')
3135
} catch (error) {
3236
console.error('Login error', error)
3337
alert('로그인 요청 중 오류 발생')

src/app/(pages)/sign-up/page.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use client'
2+
3+
import { useRouter } from 'next/navigation'
4+
5+
import { SubmitHandler, useForm } from 'react-hook-form'
6+
7+
import { SignUpRequest } from '@/types/auth.types'
8+
9+
import { SignUp } from '@/services/auth/auth'
10+
11+
export default function SignUpPage(): JSX.Element {
12+
const router = useRouter()
13+
const {
14+
register,
15+
handleSubmit,
16+
formState: { errors },
17+
} = useForm<SignUpRequest>()
18+
19+
const onSubmit: SubmitHandler<SignUpRequest> = async data => {
20+
try {
21+
const response = await SignUp(data)
22+
if (!response.ok) {
23+
console.error('회원가입 실패')
24+
alert('회원가입 실패')
25+
return
26+
}
27+
const result = await response.json()
28+
console.log('회원가입 성공:', result)
29+
30+
alert('회원가입이 완료되었습니다!')
31+
router.push('/sign-in')
32+
} catch (error) {
33+
console.error('회원가입 요청 중 오류 발생', error)
34+
alert('회원가입 중 오류 발생')
35+
}
36+
}
37+
38+
return (
39+
<div>
40+
<h1>회원가입</h1>
41+
<form onSubmit={handleSubmit(onSubmit)}>
42+
<div>
43+
<label htmlFor='email'>이메일</label>
44+
<input
45+
id='email'
46+
type='email'
47+
{...register('email', { required: '이메일을 입력하세요.' })}
48+
/>
49+
</div>
50+
<div>
51+
<label htmlFor='name'>이름</label>
52+
<input
53+
id='name'
54+
type='name'
55+
{...register('name', { required: '이름을 입력하세요.' })}
56+
/>
57+
</div>
58+
<div>
59+
<label htmlFor='password'>비밀번호</label>
60+
<input
61+
id='password'
62+
type='password'
63+
{...register('password', { required: '비밀번호를 입력하세요.' })}
64+
/>
65+
</div>
66+
<div>
67+
<label htmlFor='gitHub'>깃허브 주소</label>
68+
<input
69+
id='gitHub'
70+
type='gitHub'
71+
{...register('gitHub', { required: '깃허브 주소를 입력하세요.' })}
72+
/>
73+
</div>
74+
<button type='submit'>회원가입</button>
75+
</form>
76+
</div>
77+
)
78+
}

src/services/auth/auth.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SignInRequest, SignInResponse } from '@/types/auth.types'
1+
import { SignInRequest, SignInResponse, SignUpRequest} from '@/types/auth.types'
22
import axios, { AxiosResponse } from 'axios'
33

44
const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL
@@ -24,3 +24,14 @@ export const signIn = async (
2424
export const signOut = async () => {
2525
return await axios.post('/v1/auth/logout', {}, { withCredentials: true })
2626
}
27+
28+
const BACKEND_BASE_URL = process.env.NEXT_PUBLIC_BACKEND_BASE_URL
29+
export const SignUp = async (data: SignUpRequest): Promise<Response> => {
30+
return await fetch(`${BACKEND_BASE_URL}/v1/auth/sign-up`, {
31+
method: 'POST',
32+
headers: {
33+
'Content-Type': 'application/json',
34+
},
35+
body: JSON.stringify(data),
36+
})
37+
}

0 commit comments

Comments
 (0)