|
| 1 | +import { useState } from "react"; |
| 2 | +import { useMutation } from "@apollo/client"; |
| 3 | +import LOGIN_MUTATION from './LoginMutation'; |
| 4 | +import VerifyOneTimeCode from './2faMutation'; |
| 5 | +import TwoFactorPopup from "./Twofactorpopup"; |
| 6 | +import { useNavigate, useSearchParams } from "react-router-dom"; |
| 7 | +import { toast } from "react-toastify"; |
| 8 | +import React from "react"; |
| 9 | + |
| 10 | +const LoginWith2FA = () => { |
| 11 | + const [isOpen, setIsOpen] = useState(false); // For 2FA popup |
| 12 | + const [error, setError] = useState(""); |
| 13 | + const [loading, setLoading] = useState(false); |
| 14 | + const [otpInput, setOtpInput] = useState(Array(6).fill("")); // 6-digit OTP |
| 15 | + const [loginUser, setLoginUser] = useState<{ user: { id: string; role: string; } } | null>(null); |
| 16 | + const navigate = useNavigate(); |
| 17 | + const [searchParams] = useSearchParams(); |
| 18 | + |
| 19 | + // Move handleError above useMutation hooks |
| 20 | + const handleError = (err: any) => { |
| 21 | + console.log(err.message); |
| 22 | + if (err.networkError) { |
| 23 | + toast.error("There was a problem contacting the server"); |
| 24 | + } else if (err.message.toLowerCase() !== "invalid credentials") { |
| 25 | + toast.error("Please wait to be added to a program or cohort"); |
| 26 | + } else { |
| 27 | + setError("Invalid credentials"); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + const [login] = useMutation(LOGIN_MUTATION, { |
| 32 | + onCompleted: (data) => { |
| 33 | + if (data.loginUser.requiresOtp) { |
| 34 | + setLoginUser({ user: { id: data.loginUser.id, role: data.loginUser.role } }); // Save user data for post-2FA actions |
| 35 | + setIsOpen(true); |
| 36 | + } else { |
| 37 | + // Call handlePostLogin with just the role |
| 38 | + handlePostLogin({ user: { role: data.loginUser.role } }); |
| 39 | + } |
| 40 | + }, |
| 41 | + onError: handleError, |
| 42 | + }); |
| 43 | + |
| 44 | + |
| 45 | + const [verifyOtp] = useMutation(VerifyOneTimeCode, { |
| 46 | + onCompleted: (data) => { |
| 47 | + if (data.verifyOtp.success) { |
| 48 | + if (loginUser) { |
| 49 | + handlePostLogin(loginUser); // Continue login flow after OTP verification |
| 50 | + setIsOpen(false); // Close the popup |
| 51 | + } else { |
| 52 | + setError("User not found for post-login."); |
| 53 | + } |
| 54 | + } else { |
| 55 | + setError("Invalid OTP. Please try again."); |
| 56 | + } |
| 57 | + }, |
| 58 | + onError: handleError, |
| 59 | + }); |
| 60 | + |
| 61 | + const handleLogin = async () => { |
| 62 | + setLoading(true); |
| 63 | + try { |
| 64 | + await login({ variables: { loginInput: { /* Include necessary loginInput fields here */ } } }); |
| 65 | + } finally { |
| 66 | + setLoading(false); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + const handlePostLogin = (user: { user: { role: string; } }) => { |
| 71 | + toast.success("Login successful"); |
| 72 | + const redirect = searchParams.get("redirect"); |
| 73 | + const role = user.user.role; |
| 74 | + if (redirect) { |
| 75 | + navigate(redirect); |
| 76 | + } else { |
| 77 | + switch (role) { |
| 78 | + case "superAdmin": |
| 79 | + navigate("/organizations"); |
| 80 | + break; |
| 81 | + case "admin": |
| 82 | + navigate("/trainees"); |
| 83 | + break; |
| 84 | + case "coordinator": |
| 85 | + navigate("/trainees"); |
| 86 | + break; |
| 87 | + case "manager": |
| 88 | + navigate("/dashboard"); |
| 89 | + break; |
| 90 | + case "ttl": |
| 91 | + navigate("/ttl-trainees"); |
| 92 | + break; |
| 93 | + default: |
| 94 | + navigate("/performance"); |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + const handleOtpInput = (index: number, e: React.ChangeEvent<HTMLInputElement>) => { |
| 101 | + const updatedInput = [...otpInput]; |
| 102 | + updatedInput[index] = e.target.value; |
| 103 | + setOtpInput(updatedInput); |
| 104 | + }; |
| 105 | + |
| 106 | + const handleOtpVerification = () => { |
| 107 | + if (loginUser) { |
| 108 | + verifyOtp({ variables: { otp: otpInput.join(""), userId: loginUser.user.id } }); |
| 109 | + } else { |
| 110 | + setError("User not found for OTP verification."); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + return ( |
| 115 | + <div> |
| 116 | + <button onClick={handleLogin}>Login</button> |
| 117 | + |
| 118 | + <TwoFactorPopup |
| 119 | + isOpen={isOpen} |
| 120 | + error={error} |
| 121 | + loading={loading} |
| 122 | + input={otpInput} |
| 123 | + handleInput={handleOtpInput} |
| 124 | + verifyOtp={handleOtpVerification} |
| 125 | + /> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default LoginWith2FA; |
0 commit comments