busybar-ui / src / core / auth / api.ts
api.ts
Raw
import { client } from '@/api'
import { CallbackResponse, OAuthProvider, UserSession } from './types'

export const getAuthUrl = async (provider: OAuthProvider, redirectUrl: string): Promise<string> => {
  const response = await client.get(`/v1/auth/${provider}/url`, {
    params: { redirect_url: redirectUrl },
  })
  if (response.status !== 200) {
    throw new Error(`Failed to get auth URL for ${provider}`)
  }

  return response.data.auth_url
}

export const fetchUserSession = async (accessToken: string): Promise<UserSession> => {
  const response = await client.get(`/v1/auth/session`, {
    headers: {
      Authorization: `${accessToken}`,
    },
  })

  if (response.status !== 200) {
    throw new Error(`Failed to fetch user session`)
  }

  return response.data?.user
}

export const logout = async (accessToken: string): Promise<void> => {
  await client.get(`/v1/auth/logout`, { headers: { Authorization: `${accessToken}` } })
}