Login working

This commit is contained in:
Maxime Lalo 2023-11-27 10:54:07 +01:00
parent 485b63d5ed
commit 3315bd53bd
2 changed files with 22 additions and 6 deletions

View File

@ -25,6 +25,12 @@ export type ISetPasswordParams = {
totpCode: string;
};
export type ILoginParams = {
password: string;
email: string;
totpCode: string;
};
export default class Auth extends BaseApiService {
private static instance: Auth;
protected readonly namespaceUrl = this.getBaseUrl().concat("/customer");
@ -63,4 +69,14 @@ export default class Auth extends BaseApiService {
return Promise.reject(err);
}
}
public async login(body: ILoginParams): Promise<ICustomerTokens> {
const url = new URL(this.baseURl.concat("/login"));
try {
return this.postRequest<ICustomerTokens>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
}

View File

@ -49,9 +49,9 @@ export default function Login() {
async (e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) => {
try {
if (!values["email"]) return;
setEmail(values["email"]);
const res = await Auth.getInstance().mailVerifySms({ email: values["email"] });
setPartialPhoneNumber(res.partialPhoneNumber);
setEmail(values["email"]);
setStep(LoginStep.TOTP);
} catch (error: any) {
// If token already exists and is still valid redirect to the connect/register page
@ -133,16 +133,16 @@ export default function Login() {
return;
}
},
[email, totpCode, setValidationErrors],
[totpCode, email, router],
);
const onPasswordSubmit = useCallback(
async (e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) => {
try {
if (!values["password"]) return;
const res = await Auth.getInstance().setPassword({ totpCode, email, password: values["password"] });
// If set password worked, setting the token and redirecting
const token = await Auth.getInstance().login({ totpCode, email, password: values["password"] });
CustomerStore.instance.connect(token.accessToken, token.refreshToken);
router.push(Module.getInstance().get().modules.pages.Folder.pages.Select.props.path);
} catch (error: any) {
setValidationErrors([
{
@ -155,7 +155,7 @@ export default function Login() {
return;
}
},
[email, totpCode],
[email, router, totpCode],
);
return (