zucchetti-sa5/src/api/login.ts

22 lines
846 B
TypeScript

import { axiosInstance } from "./axiosInstance";
import { ApiToken, LoginResponse, FailedLoginError } from "./types";
/**
*
* @param user the name of user logging in (if it's a customer, then it's his email)
* @param password the password of the user
* @returns
*/
export async function login(user: string, password: string): Promise<LoginResponse>{
const res = await axiosInstance.get("/login", { params: { user, password } });
if (res.data.login.result == "ERROR") {
const errCode: number = res.data.login.errorCode;
const errMsg: string = res.data.login.message;
throw new FailedLoginError(errMsg, errCode);
}
const token: ApiToken = res.data.login.customerProperties.token;
const id: number = res.data.login.customerProperties.id;
return { token, id };
}
export { FailedLoginError };