|
1 | 1 | 'use server';
|
2 | 2 |
|
3 |
| -import { API_HOST } from '@/api'; |
| 3 | +import { API_HOST, ApiResponse, createApiFetchError, interceptResponse } from '@/api'; |
4 | 4 | import { getCommonFetchConfig } from '@/api/config';
|
5 | 5 | import { cookies } from 'next/headers';
|
6 | 6 |
|
7 |
| -export const logoutMutation = async (): Promise<void> => { |
8 |
| - await fetch(`${API_HOST}/auth/logout`, { |
9 |
| - method: 'DELETE', |
10 |
| - ...(await getCommonFetchConfig()) |
11 |
| - }); |
12 |
| - const cookieStore = await cookies(); |
13 |
| - cookieStore.delete('_maruToken'); |
| 7 | +export const logoutMutation = async (): Promise<ApiResponse<boolean>> => { |
| 8 | + try { |
| 9 | + const response = await fetch(`${API_HOST}/auth/logout`, { |
| 10 | + method: 'DELETE', |
| 11 | + ...(await getCommonFetchConfig()) |
| 12 | + }); |
| 13 | + return interceptResponse(response, async () => { |
| 14 | + const cookieStore = await cookies(); |
| 15 | + cookieStore.delete('_maruToken'); |
| 16 | + }); |
| 17 | + } catch (error) { |
| 18 | + return createApiFetchError(); |
| 19 | + } |
14 | 20 | };
|
15 | 21 |
|
16 | 22 | export interface RegisterMutationParams {
|
17 | 23 | readonly nickname: string;
|
18 | 24 | readonly registerToken: string;
|
19 | 25 | }
|
20 |
| -export const registerMutation = async ({ nickname, registerToken }: RegisterMutationParams): Promise<void> => { |
21 |
| - const response = await fetch(`${API_HOST}/auth/register`, { |
22 |
| - method: 'POST', |
23 |
| - body: JSON.stringify({ nickname }), |
24 |
| - ...(await getCommonFetchConfig({ |
25 |
| - 'X-Maru-RegisterToken': registerToken |
26 |
| - })) |
27 |
| - }); |
28 | 26 |
|
29 |
| - const cookie = response.headers.get('set-cookie'); |
30 |
| - const token = cookie?.split(';'); |
31 |
| - token?.forEach((t) => { |
32 |
| - const [key, value] = t.split('='); |
33 |
| - if (key === '_maruToken') { |
34 |
| - const cookieStore = cookies(); |
35 |
| - cookieStore.then((store) => { |
36 |
| - store.set('_maruToken', value); |
| 27 | +export const registerMutation = async ({ |
| 28 | + nickname, |
| 29 | + registerToken |
| 30 | +}: RegisterMutationParams): Promise<ApiResponse<boolean>> => { |
| 31 | + try { |
| 32 | + const response = await fetch(`${API_HOST}/auth/register`, { |
| 33 | + method: 'POST', |
| 34 | + body: JSON.stringify({ nickname }), |
| 35 | + ...(await getCommonFetchConfig({ |
| 36 | + 'X-Maru-RegisterToken': registerToken |
| 37 | + })) |
| 38 | + }); |
| 39 | + return interceptResponse(response, async () => { |
| 40 | + const cookie = response.headers.get('set-cookie'); |
| 41 | + const token = cookie?.split(';'); |
| 42 | + token?.forEach((t) => { |
| 43 | + const [key, value] = t.split('='); |
| 44 | + if (key === '_maruToken') { |
| 45 | + const cookieStore = cookies(); |
| 46 | + cookieStore.then((store) => { |
| 47 | + store.set('_maruToken', value); |
| 48 | + }); |
| 49 | + } |
37 | 50 | });
|
38 |
| - } |
39 |
| - }); |
| 51 | + }); |
| 52 | + } catch (error) { |
| 53 | + return createApiFetchError(); |
| 54 | + } |
40 | 55 | };
|
0 commit comments