All files / src/api/service/auth-service index.ts

62.71% Statements 37/59
100% Branches 3/3
42.85% Functions 3/7
62.71% Lines 37/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x           6x 6x 6x 6x 6x 6x 2x 2x 2x 6x 6x 6x                   6x 6x 6x 1x 1x 1x 6x 6x 6x                 6x  
import { api } from '@/api/core';
import { clearAccessToken, setAccessToken } from '@/lib/auth/token';
import {
  GoogleOAuthExchangeRequest,
  GoogleOAuthExchangeResponse,
  LoginRequest,
  LoginResponse,
  RefreshResponse,
  SignupRequest,
  SignupResponse,
} from '@/types/service/auth';
 
export const authServiceRemote = () => ({
  // 로그인
  login: async (payload: LoginRequest) => {
    const data = await api.post<LoginResponse>('/auth/login', payload, { withCredentials: true });

    setAccessToken(data.accessToken, data.expiresIn);
    return data;
  },
 
  // 회원가입
  signup: (payload: SignupRequest) => api.post<SignupResponse>(`/auth/signup`, payload),
 
  // 로그아웃
  logout: async () => {
    await api.post<void>('/auth/logout', null, { withCredentials: true });
    clearAccessToken();
  },
 
  // 액세스 토큰 재발급
  refresh: async (redirect: boolean = true) => {
    const data = await api.post<RefreshResponse>(
      '/auth/refresh',
      {},
      { _retry: true, withCredentials: true, skipAuthRedirect: redirect },
    );

    setAccessToken(data.accessToken, data.expiresIn);
    return data;
  },
 
  // 회원 탈퇴
  withdraw: async () => {
    await api.delete<void>('/auth/withdraw', { withCredentials: true });
    clearAccessToken();
  },
 
  // 구글 OAuth 코드 교환
  exchangeGoogleCode: async (payload: GoogleOAuthExchangeRequest) => {
    const data = await api.post<GoogleOAuthExchangeResponse>('/auth/google', payload, {
      withCredentials: true,
    });

    setAccessToken(data.accessToken, data.expiresIn);

    return data;
  },
});