Skip to content

Commit

Permalink
로그인 관련 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcgrdn committed Nov 20, 2024
1 parent 5ce975f commit 6dccbb2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/lib/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from 'axios';

export const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || '',
withCredentials: true,
headers: {
'Content-Type': 'application/json',
},
Expand Down
28 changes: 21 additions & 7 deletions src/provider/userProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,34 @@ export function UserProvider({ children }: { children: React.ReactNode }) {
const fetchUser = async () => {
try {
const { data } = await api.get<Artist>("/artist");

if (!data || typeof data !== 'object') {
throw new Error('Invalid response data');
}

const requiredFields: (keyof Artist)[] = ['uuid', 'name', 'role', 'email', 'artistImage'];
const missingFields = requiredFields.filter(field => !(field in data));

if (missingFields.length > 0) {
throw new Error(`Missing required fields: ${missingFields.join(', ')}`);
}

setUser(data);
} catch (err) {
if (err instanceof AxiosError) {
if (axios.isAxiosError(err)) {
if (err.response?.status === 401) {
setIsLoggedIn(false);
setUser(null);
} else {
setError(
new Error(
err.response?.data?.message ||
`Failed to fetch user data: ${err.response?.status}`
)
);
}
setError(
new Error(
err.response?.data?.message || "Failed to fetch user data"
)
);
} else {
setError(new Error("An unexpected error occurred"));
setError(new Error("An unexpected error occurred while fetching user data"));
}
} finally {
setIsLoading(false);
Expand Down
6 changes: 2 additions & 4 deletions src/services/albumService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import { api } from '@/lib/axios';

export interface Album {
uuid: string;
Expand All @@ -8,11 +8,9 @@ export interface Album {
releaseDate: string;
}

const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL;

export const getAllAlbums = async (): Promise<Album[]> => {
try {
const response = await axios.get(`${BASE_URL}/album`);
const response = await api.get<Album[]>("/album");
return response.data;
} catch (error) {
console.error('앨범 데이터 조회 실패:', error);
Expand Down

0 comments on commit 6dccbb2

Please sign in to comment.