import type { AxiosRequestConfig , AxiosResponse } from 'axios' ; import { BackendError , BackendErrorLabel , HttpClient } from '../http/' ; import { ClientType } from '../client/' ; import { ForbiddenPhoneNumberError , InvalidPhoneNumberError , PasswordExistsError } from './AuthenticationError' ; import { retrieveCookie , sendRequestWithCookie } from '../shims/node/cookie' ; import type { AccessTokenData , LoginData , SendLoginCode } from '../auth/' ; import type { CookieList } from './CookieList' ; import type { LoginCodeResponse } from './LoginCodeResponse' ; import type { RegisterData } from './RegisterData' ; import type { User } from '../user/' ; export class AuthAPI { constructor ( private readonly client ) { } public static readonly URL = { ACCESS : '/access' , COOKIES : '/cookies' , EMAIL : 'email' , INITIATE_BIND : '/sso-initiate-bind' , INITIATE_LOGIN : 'initiate-login' , LOGIN : '/login' , LOGOUT : 'logout' , REGISTER : '/register' , REMOVE : 'remove' , SELF : 'self' , SEND : 'send' , SSO : '/sso' , } ; public getCookies ( labels ? ) { const config = { method : 'get' , params : { } , url : AuthAPI . URL . COOKIES , } ; if ( labels ) { config . params . labels = labels . join ( ',' ) ; } return this . client . sendRequest ( config ) ; } public async postCookiesRemove ( password , labels ? , ids ? ) { const config = { data : { ids , labels , password , } , method : 'post' , url : `${ AuthAPI . URL . COOKIES }/${ AuthAPI . URL . REMOVE }` , withCredentials : true , } ; await this . client . sendJSON ( config ) ; } public async postLogin ( loginData ) { const login = { ... loginData , clientType : undefined as any , password : loginData . password ? String ( loginData . password ) : undefined , } ; const config = { data : login , method : 'post' , params : { persist : loginData . clientType === ClientType . PERMANENT , } , url : AuthAPI . URL . LOGIN , withCredentials : true , } ; const response = await this . client . sendJSON < AccessTokenData > ( config ) ; return retrieveCookie ( response ) ; } param loginRequest see https public async postLoginSend ( loginRequest ) { const defaultLoginRequest = { force : false } ; const config = { data : { ... defaultLoginRequest , ... loginRequest } , method : 'post' , url : `${ AuthAPI . URL . LOGIN }/${ AuthAPI . URL . SEND }` , } ; try { const response = await this . client . sendJSON < LoginCodeResponse > ( config ) ; return response . data ; } catch ( error ) { const backendError = error as BackendError ; switch ( backendError . label ) { case BackendErrorLabel . BAD_REQUEST : { throw new InvalidPhoneNumberError ( backendError . message ) ; } case BackendErrorLabel . INVALID_PHONE : case BackendErrorLabel . UNAUTHORIZED : { throw new ForbiddenPhoneNumberError ( backendError . message ) ; } case BackendErrorLabel . PASSWORD_EXISTS : { throw new PasswordExistsError ( backendError . message ) ; } } throw error ; } } public async postLogout ( ) { const config = { method : 'post' , url : `${ AuthAPI . URL . ACCESS }/${ AuthAPI . URL . LOGOUT }` , withCredentials : true , } ; await sendRequestWithCookie ( this . client , config ) ; } public async postRegister ( userAccount ) { const config = { data : userAccount , method : 'post' , url : AuthAPI . URL . REGISTER , withCredentials : true , } ; const response = await this . client . sendJSON < User > ( config ) ; return retrieveCookie ( response ) ; } public async putEmail ( emailData ) { const config = { data : emailData , method : 'put' , url : `${ AuthAPI . URL . ACCESS }/${ AuthAPI . URL . SELF }/${ AuthAPI . URL . EMAIL }` , withCredentials : true , } ; await this . client . sendJSON ( config ) ; } public async headInitiateLogin ( ssoCode ) { const config = { method : 'head' , url : `${ AuthAPI . URL . SSO }/${ AuthAPI . URL . INITIATE_LOGIN }/${ ssoCode }` , } ; await this . client . sendJSON ( config ) ; } public async headInitiateBind ( ssoCode ) { const config = { method : 'head' , url : `${ AuthAPI . URL . INITIATE_BIND }/${ ssoCode }` , } ; await this . client . sendJSON ( config ) ; } }