더북(TheBook)

15.6.3 회원 인증 API 함수 만들기

이제 로그인 기능과 회원 가입 기능을 구현하기 위해 관련 API를 호출하는 함수를 작성해봅시다.

api/auth.ts

import client from './client';
import {AuthResult, User} from './types';

export async function register(params: RegisterParams) {
  const response = await client.post<AuthResult>(
    '/auth/local/register',
    params,
  );
  return response.data;
}

export async function login(params: LoginParams) {
  const response = await client.post<AuthResult>('/auth/local', params);
  return response.data;
}

export async function getLoginStatus() {
  const response = await client.get<User>('/users/me');
  return response.data;
}

interface RegisterParams {
  username: string;
  email: string;
  password: string;
}

interface LoginParams {
  identifier: string;
  password: string;
}

register 함수와 login 함수에서 사용하는 파라미터는 객체 형태의 타입으로 최하단에 선언해줬습니다. 타입은 사용해야 하는 코드의 위에 선언해도 되고, 아래에 선언해도 됩니다.