feat: simple impl

This commit is contained in:
2025-08-04 07:50:13 +03:00
parent a92c43ab1b
commit a812e650ce
21 changed files with 1800 additions and 58 deletions

View File

@ -1,15 +1,17 @@
"use client";
import { FC, useEffect, useState } from "react";
import { useSearchParams } from "next/navigation";
import { useSelector } from "react-redux";
import { Button, Text } from "@mantine/core";
import { Auth } from "@/client";
import SCOPES from "@/constants/scopes";
import { Scopes } from "@/enums/Scopes";
import { notifications } from "@/lib/notifications";
import { RootState } from "@/lib/store";
import { AuthService } from "@/mocks/authService";
const ConsentButton: FC = () => {
const searchParams = useSearchParams();
const auth = useSelector((state: RootState) => state.auth);
const [clientName, setClientName] = useState<string>(Scopes.UNDEFINED);
const [serviceRequiredAccess, setServiceRequiredAccess] =
@ -25,18 +27,17 @@ const ConsentButton: FC = () => {
};
const requestConsent = () => {
if (!auth.loginChallenge || auth.scope.length === 0) return;
new AuthService()
.requestConsent(auth.loginChallenge)
.then(response => response.data)
.then(({ clientName }) => {
setClientName(clientName);
})
.catch(error => {
console.error(error);
notifications.error({ message: error.toString() });
});
// if (!auth.loginChallenge || auth.scope.length === 0) return;
// new AuthService()
// .requestConsent(auth.loginChallenge)
// .then(response => response.data)
// .then(({ clientName }) => {
// setClientName(clientName);
// })
// .catch(error => {
// console.error(error);
// notifications.error({ message: error.toString() });
// });
};
useEffect(() => {
@ -45,18 +46,40 @@ const ConsentButton: FC = () => {
}, []);
const confirmAccess = () => {
if (!auth.loginChallenge) return;
new AuthService()
.approveConsent(auth.loginChallenge)
const consentChallenge = searchParams.get("consent_challenge");
if (!consentChallenge) {
console.error("Consent challenge is missing in the URL");
return;
}
Auth.postAuthConsentAccept({
body: { consent_challenge: consentChallenge },
})
.then(response => response.data)
.then(({ redirectUrl }) => {
window.location.href = redirectUrl;
})
.catch(error => {
console.error(error);
notifications.error({ message: error.toString() });
.then(response => {
if (!response) {
console.error("Response is empty");
return;
}
const { redirect_url, ok, message } = response;
notifications.guess(ok, { message });
if (redirect_url) {
window.location.href = redirect_url;
} else {
console.error("Redirect URL is missing in the response");
}
});
// if (!auth.loginChallenge) return;
// new AuthService()
// .approveConsent(auth.loginChallenge)
// .then(response => response.data)
// .then(({ redirectUrl }) => {
// window.location.href = redirectUrl;
// })
// .catch(error => {
// console.error(error);
// notifications.error({ message: error.toString() });
// });
};
return (

View File

@ -7,10 +7,9 @@ import { Button, PinInput, Stack } from "@mantine/core";
import { useForm } from "@mantine/form";
import ResendVerificationCode from "@/app/verify-phone/components/ResendVerificationCode/ResendVerificationCode";
import style from "@/app/verify-phone/components/VerifyPhoneForm/VerifyPhone.module.css";
import { setScope } from "@/lib/features/auth/authSlice";
import { Auth } from "@/client";
import { notifications } from "@/lib/notifications";
import { RootState, useAppDispatch } from "@/lib/store";
import { AuthService } from "@/mocks/authService";
import { RootState } from "@/lib/store";
type VerifyNumberForm = {
code: string;
@ -26,25 +25,30 @@ const VerifyPhoneForm: FC = () => {
},
});
const authState = useSelector((state: RootState) => state.auth);
const dispatch = useAppDispatch();
const handleSubmit = (values: VerifyNumberForm) => {
if (!authState.phoneNumber || !authState.loginChallenge) return;
new AuthService()
.approveLogin(
authState.phoneNumber,
values.code,
authState.loginChallenge
)
if (!authState.phoneNumber || !authState.loginChallenge) return;
console.log(authState.phoneNumber.replace(/ /g, ""));
Auth.postAuthOtpVerify({
body: {
phone_number: authState.phoneNumber.replace(" ", ""),
login_challenge: authState.loginChallenge,
otp: values.code,
},
})
.then(response => response.data)
.then(({ redirectUrl, scope }) => {
dispatch(setScope(scope));
window.location.href = redirectUrl;
})
.catch(error => {
console.error(error);
notifications.error({ message: error.toString() });
.then(response => {
if (!response) return;
const { redirect_url, ok } = response;
if (!ok) {
notifications.error({
message: "Ошибка при подтверждении номера",
});
return;
}
window.location.href = redirect_url;
});
};