✨ Login working
This commit is contained in:
parent
485b63d5ed
commit
3315bd53bd
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 (
|
||||
|
Loading…
x
Reference in New Issue
Block a user