feat: login_challenge and scope storing, mock api
This commit is contained in:
@ -1,21 +1,27 @@
|
||||
"use client";
|
||||
|
||||
import { FC, useState } from "react";
|
||||
import { redirect } from "next/navigation";
|
||||
import { FC, useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Button, Stack } from "@mantine/core";
|
||||
import { useForm } from "@mantine/form";
|
||||
import PhoneInput from "@/components/PhoneInput/PhoneInput";
|
||||
import { useAppDispatch } from "@/lib/store";
|
||||
import { setLoginChallenge, setPhoneNumber } from "@/lib/features/auth/authSlice";
|
||||
import { AuthService } from "@/mocks/authService";
|
||||
import { notifications } from "@/lib/notifications";
|
||||
|
||||
type LoginForm = {
|
||||
phoneNumber: string;
|
||||
};
|
||||
|
||||
type Props = {
|
||||
loginChallenge?: string;
|
||||
isCreatingId?: boolean;
|
||||
};
|
||||
|
||||
const LoginForm: FC<Props> = ({ isCreatingId = false }) => {
|
||||
const LoginForm: FC<Props> = ({ loginChallenge, isCreatingId = false }) => {
|
||||
const [phoneMask, setPhoneMask] = useState<string>("");
|
||||
const router = useRouter();
|
||||
const form = useForm<LoginForm>({
|
||||
initialValues: {
|
||||
phoneNumber: "",
|
||||
@ -26,17 +32,33 @@ const LoginForm: FC<Props> = ({ isCreatingId = false }) => {
|
||||
"Введите корректный номер",
|
||||
},
|
||||
});
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(setLoginChallenge(loginChallenge ?? null));
|
||||
}, [loginChallenge]);
|
||||
|
||||
const handleSubmit = (values: LoginForm) => {
|
||||
console.log(values);
|
||||
console.log(phoneMask);
|
||||
dispatch(setPhoneNumber(values.phoneNumber));
|
||||
|
||||
redirect("/verify-phone");
|
||||
new AuthService().requestLogin(values.phoneNumber)
|
||||
.then(response => response.data)
|
||||
.then(({ ok, message }) => {
|
||||
if (!ok) {
|
||||
notifications.error({ message });
|
||||
} else {
|
||||
router.push("/verify-phone");
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
notifications.error({ message: error.toString() });
|
||||
})
|
||||
};
|
||||
|
||||
const navigateToCreateId = () => redirect("/create-id");
|
||||
const navigateToCreateId = () => router.push("/create-id");
|
||||
|
||||
const navigateToLogin = () => redirect("/");
|
||||
const navigateToLogin = () => router.push("/");
|
||||
|
||||
return (
|
||||
<form onSubmit={form.onSubmit(handleSubmit)}>
|
||||
|
||||
Reference in New Issue
Block a user