Skip to content

Commit ca3f6eb

Browse files
committed
feat(getcloser): add auth token to all APIs except for /v1/auth
1 parent 86b5877 commit ca3f6eb

5 files changed

Lines changed: 41 additions & 13 deletions

File tree

getcloser/frontend/src/app/page1/page.tsx

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,54 @@ import { useFormStore } from '../../store/formStore';
99
import { useRouter } from 'next/navigation';
1010

1111
export default function Page1() {
12-
const { email, setEmail, setId } = useFormStore();
12+
const { email, setEmail, setId, setAccessToken } = useFormStore();
1313
const router = useRouter();
1414

1515
const handleSubmit = async (e: React.FormEvent) => {
1616
e.preventDefault();
1717
console.log('Form Submitted:', { email });
1818

1919
try {
20-
const response = await fetch('/api/v1/users/auth', {
20+
const authResponse = await fetch('/api/v1/auth', {
2121
method: 'POST',
2222
headers: {
2323
'Content-Type': 'application/json',
2424
},
2525
body: JSON.stringify({ email }),
2626
});
2727

28-
if (!response.ok) {
29-
throw new Error(`HTTP error! status: ${response.status}`);
28+
if (!authResponse.ok) {
29+
throw new Error(`HTTP error! status: ${authResponse.status}`);
3030
}
3131

32-
const result = await response.json();
33-
console.log('Auth API Response:', result);
34-
if (result.id) {
35-
setId(result.id); // Store the ID in the form store
32+
const authResult = await authResponse.json();
33+
const newAccessToken = authResult.accessToken;
34+
35+
if (!newAccessToken) {
36+
throw new Error('Access token not received from auth API.');
37+
}
38+
setAccessToken(newAccessToken); // Store the new access token
39+
40+
const userMeResponse = await fetch('/api/v1/users/me', {
41+
method: 'GET',
42+
headers: {
43+
'Content-Type': 'application/json',
44+
'Authorization': `Bearer ${newAccessToken}`, // Use the new access token
45+
},
46+
});
47+
48+
if (!userMeResponse.ok) {
49+
throw new Error(`HTTP error! status: ${userMeResponse.status}`);
50+
}
51+
52+
const userMeResult = await userMeResponse.json();
53+
console.log('User Me API Response:', userMeResult);
54+
55+
if (userMeResult.sub) {
56+
setId(userMeResult.sub);
3657
}
3758
alert('정보가 제출되었습니다!');
38-
router.push('/page2'); // Navigate to page2 after successful submission
59+
router.push('/page2');
3960
} catch (error) {
4061
console.error('Error submitting form:', error);
4162
alert('정보 제출에 실패했습니다.');

getcloser/frontend/src/app/page2/page.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client';
22

3+
import { authenticatedFetch } from '../../lib/api';
34
import React, { useState, useEffect, useRef, useCallback } from 'react';
45
import Link from 'next/link';
56
import { useRouter } from 'next/navigation';
@@ -33,7 +34,7 @@ export default function Page2() {
3334
}
3435

3536
try {
36-
const response = await fetch(`/api/v1/users/${userId}`);
37+
const response = await authenticatedFetch(`/api/v1/users/${userId}`);
3738
if (!response.ok) {
3839
throw new Error(`HTTP error! status: ${response.status}`);
3940
}
@@ -115,7 +116,7 @@ export default function Page2() {
115116

116117
// Step 3: Make the POST request
117118
try {
118-
const response = await fetch('/api/v1/teams/create', {
119+
const response = await authenticatedFetch('/api/v1/teams/create', {
119120
method: 'POST',
120121
headers: {
121122
'Content-Type': 'application/json',

getcloser/frontend/src/app/page3/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Button } from '@/components/ui/button';
66
import { Label } from '@/components/ui/label';
77
import { Textarea } from '@/components/ui/textarea';
88
import { useFormStore } from '../../store/formStore';
9+
import { authenticatedFetch } from '../../lib/api';
910
import React, { useEffect } from 'react'; // Import useEffect
1011

1112
export default function Page3() {
@@ -28,7 +29,7 @@ export default function Page3() {
2829
console.log('Assign Challenges Request Body:', requestBody);
2930

3031
try {
31-
const response = await fetch('/api/v1/challenge/assign-challenges', {
32+
const response = await authenticatedFetch('/api/v1/challenge/assign-challenges', {
3233
method: 'POST',
3334
headers: {
3435
'Content-Type': 'application/json',

getcloser/frontend/src/components/Header.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client';
22

3+
import { authenticatedFetch } from '../lib/api';
34
import React, { useState, useEffect } from 'react';
45
import { useFormStore } from '../store/formStore';
56

@@ -11,7 +12,7 @@ export default function Header() {
1112
if (id) {
1213
const fetchUserName = async () => {
1314
try {
14-
const response = await fetch(`/api/v1/users/${id}`);
15+
const response = await authenticatedFetch(`/api/v1/users/${id}`);
1516
if (!response.ok) {
1617
throw new Error(`HTTP error! status: ${response.status}`);
1718
}

getcloser/frontend/src/store/formStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { create } from 'zustand';
33
interface FormState {
44
email: string;
55
id: string;
6+
accessToken: string;
67
question: string;
78
answer: string;
89
teamId: string;
910
memberIds: string[];
1011
setEmail: (email: string) => void;
1112
setId: (id: string) => void;
13+
setAccessToken: (accessToken: string) => void;
1214
setQuestion: (question: string) => void;
1315
setAnswer: (answer: string) => void;
1416
setTeamId: (teamId: string) => void;
@@ -18,12 +20,14 @@ interface FormState {
1820
export const useFormStore = create<FormState>((set) => ({
1921
email: '',
2022
id: '',
23+
accessToken: '',
2124
question: '',
2225
answer: '',
2326
teamId: '',
2427
memberIds: [],
2528
setEmail: (email) => set({ email }),
2629
setId: (id) => set({ id }),
30+
setAccessToken: (accessToken) => set({ accessToken }),
2731
setQuestion: (question) => set({ question }),
2832
setAnswer: (answer) => set({ answer }),
2933
setTeamId: (teamId) => set({ teamId }),

0 commit comments

Comments
 (0)