2025-05-26 12:28:46 +00:00
|
|
|
|
import React, { useState, FormEvent, useEffect } from "react";
|
2025-03-25 06:17:41 +00:00
|
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
|
import { LogoIconLarge } from "../components/icons/Logo";
|
|
|
|
|
import { EyeIcon } from "../components/icons/ExportCommonIcons";
|
2025-04-28 03:55:51 +00:00
|
|
|
|
import {
|
|
|
|
|
useLoadingProgress,
|
|
|
|
|
useOrganization,
|
|
|
|
|
useUserName,
|
2025-05-13 12:23:00 +00:00
|
|
|
|
} from "../store/builder/store";
|
2025-03-25 11:05:54 +00:00
|
|
|
|
import { signInApi } from "../services/factoryBuilder/signInSignUp/signInApi";
|
|
|
|
|
import { signUpApi } from "../services/factoryBuilder/signInSignUp/signUpApi";
|
2025-05-26 12:28:46 +00:00
|
|
|
|
import FingerprintJS from "@fingerprintjs/fingerprintjs";
|
2025-06-09 12:26:54 +00:00
|
|
|
|
import { recentlyViewed } from "../services/dashboard/recentlyViewed";
|
|
|
|
|
import { getUserData } from "../components/Dashboard/functions/getUserData";
|
2025-03-25 06:17:41 +00:00
|
|
|
|
|
|
|
|
|
const UserAuth: React.FC = () => {
|
|
|
|
|
const [email, setEmail] = useState("");
|
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
const [error, setError] = useState("");
|
2025-03-25 11:05:54 +00:00
|
|
|
|
const [isSignIn, setIsSignIn] = useState(true);
|
|
|
|
|
const { userName, setUserName } = useUserName();
|
2025-03-26 13:03:51 +00:00
|
|
|
|
const { setOrganization } = useOrganization();
|
|
|
|
|
const { setLoadingProgress } = useLoadingProgress();
|
2025-05-26 12:28:46 +00:00
|
|
|
|
const [fingerprint, setFingerprint] = useState("");
|
2025-03-25 06:17:41 +00:00
|
|
|
|
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
2025-05-26 12:28:46 +00:00
|
|
|
|
const initializeFingerprint = async () => {
|
|
|
|
|
const fp = await FingerprintJS.load();
|
|
|
|
|
const result = await fp.get();
|
|
|
|
|
setFingerprint(result.visitorId); // Set the fingerprint
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
initializeFingerprint();
|
|
|
|
|
}, [])
|
|
|
|
|
|
2025-06-09 12:26:54 +00:00
|
|
|
|
const { userId, organization } = getUserData();
|
|
|
|
|
|
|
|
|
|
|
2025-05-26 12:28:46 +00:00
|
|
|
|
|
2025-03-25 11:05:54 +00:00
|
|
|
|
const handleLogin = async (e: FormEvent<HTMLFormElement>) => {
|
2025-03-25 06:17:41 +00:00
|
|
|
|
e.preventDefault();
|
2025-04-28 03:55:51 +00:00
|
|
|
|
const organization = email.split("@")[1].split(".")[0];
|
2025-03-25 11:05:54 +00:00
|
|
|
|
try {
|
2025-05-26 12:28:46 +00:00
|
|
|
|
const res = await signInApi(email, password, organization, fingerprint);
|
|
|
|
|
if (res.message.message === "login successfull") {
|
2025-03-25 11:05:54 +00:00
|
|
|
|
setError("");
|
|
|
|
|
setOrganization(organization);
|
2025-05-26 12:28:46 +00:00
|
|
|
|
setUserName(res.message.name);
|
|
|
|
|
// console.log(' res.userId: ', res.message.userId);
|
|
|
|
|
localStorage.setItem("userId", res.message.userId);
|
|
|
|
|
localStorage.setItem("email", res.message.email);
|
|
|
|
|
localStorage.setItem("userName", res.message.name);
|
|
|
|
|
localStorage.setItem("token", res.message.token);
|
|
|
|
|
localStorage.setItem("refreshToken", res.message.refreshToken);
|
|
|
|
|
|
2025-06-09 12:26:54 +00:00
|
|
|
|
try {
|
|
|
|
|
const projects = await recentlyViewed(organization, res.message.userId);
|
2025-06-10 06:09:23 +00:00
|
|
|
|
|
2025-06-09 12:26:54 +00:00
|
|
|
|
if (Object.values(projects.RecentlyViewed).length > 0) {
|
|
|
|
|
const firstId = (Object.values(projects?.RecentlyViewed || {})[0] as any)?._id;
|
|
|
|
|
setLoadingProgress(1)
|
|
|
|
|
navigate(`/${firstId}`)
|
|
|
|
|
} else {
|
|
|
|
|
if (res.message.isShare) {
|
|
|
|
|
setLoadingProgress(1);
|
|
|
|
|
// navigate("/Project");
|
|
|
|
|
navigate("/Dashboard");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching recent projects:", error);
|
2025-03-25 11:05:54 +00:00
|
|
|
|
}
|
2025-06-09 12:26:54 +00:00
|
|
|
|
|
|
|
|
|
|
2025-03-25 11:05:54 +00:00
|
|
|
|
} else if (res.message === "User Not Found!!! Kindly signup...") {
|
|
|
|
|
setError("Account not found");
|
2025-05-26 12:28:46 +00:00
|
|
|
|
} else if (res.message === "Already LoggedIn on another browser....Please logout!!!") {
|
|
|
|
|
setError("Already logged in on another browser. Please logout first.");
|
|
|
|
|
navigate("/");
|
|
|
|
|
setError("")
|
|
|
|
|
// setError("");
|
|
|
|
|
// setOrganization(organization);
|
|
|
|
|
// setUserName(res.ForceLogoutData.userName);
|
|
|
|
|
// console.log(' res.userId: ', res.ForceLogoutData.userId);
|
|
|
|
|
// localStorage.setItem("userId", res.ForceLogoutData.userId);
|
|
|
|
|
// localStorage.setItem("email", res.ForceLogoutData.Email);
|
|
|
|
|
// localStorage.setItem("userName", res.ForceLogoutData.userName);
|
|
|
|
|
// localStorage.setItem("token", res.ForceLogoutData.token);
|
|
|
|
|
// localStorage.setItem("refreshToken", res.ForceLogoutData.refreshToken);
|
|
|
|
|
// if (res.ForceLogoutData.isShare) {
|
|
|
|
|
// setLoadingProgress(1);
|
|
|
|
|
// navigate("/Dashboard");
|
|
|
|
|
// }
|
2025-03-25 11:05:54 +00:00
|
|
|
|
}
|
2025-05-08 09:49:21 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
echo.error("Login failed");
|
|
|
|
|
}
|
2025-03-25 06:17:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
2025-03-25 11:05:54 +00:00
|
|
|
|
const handleRegister = async (e: FormEvent) => {
|
2025-03-25 06:17:41 +00:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (email && password && userName) {
|
|
|
|
|
setError("");
|
2025-03-25 11:05:54 +00:00
|
|
|
|
try {
|
2025-04-28 03:55:51 +00:00
|
|
|
|
const organization = email.split("@")[1].split(".")[0];
|
2025-03-25 11:05:54 +00:00
|
|
|
|
const res = await signUpApi(userName, email, password, organization);
|
|
|
|
|
|
|
|
|
|
if (res.message === "New User created") {
|
|
|
|
|
setIsSignIn(true);
|
|
|
|
|
}
|
|
|
|
|
if (res.message === "User already exists") {
|
|
|
|
|
setError("User already exists");
|
|
|
|
|
}
|
2025-05-08 09:49:21 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
echo.error("Register user failed");
|
|
|
|
|
}
|
2025-03-25 06:17:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
setError("Please fill all the fields!");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-04-28 03:55:51 +00:00
|
|
|
|
<div className="auth-container">
|
|
|
|
|
<div className="logo-icon">
|
|
|
|
|
<LogoIconLarge />
|
|
|
|
|
</div>
|
|
|
|
|
<h1>Welcome to Dwinzo</h1>
|
|
|
|
|
<p>
|
|
|
|
|
{isSignIn ? (
|
|
|
|
|
<>
|
|
|
|
|
Don’t have an account?{" "}
|
|
|
|
|
<span
|
|
|
|
|
className="link"
|
|
|
|
|
onClick={() => setIsSignIn(false)}
|
|
|
|
|
style={{ cursor: "pointer" }}
|
|
|
|
|
>
|
|
|
|
|
Register here!
|
|
|
|
|
</span>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
Already have an account?{" "}
|
|
|
|
|
<span
|
|
|
|
|
className="link"
|
|
|
|
|
onClick={() => setIsSignIn(true)}
|
|
|
|
|
style={{ cursor: "pointer" }}
|
|
|
|
|
>
|
|
|
|
|
Login here!
|
|
|
|
|
</span>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</p>
|
2025-03-25 06:17:41 +00:00
|
|
|
|
|
2025-05-14 13:09:47 +00:00
|
|
|
|
<button id="google-login" className="google-login">
|
2025-04-28 03:55:51 +00:00
|
|
|
|
<span className="google-icon">G</span> Continue with Google
|
|
|
|
|
</button>
|
2025-03-25 06:17:41 +00:00
|
|
|
|
|
2025-04-28 03:55:51 +00:00
|
|
|
|
{error && <div className="error-message">🛈 {error}</div>}
|
2025-03-25 06:17:41 +00:00
|
|
|
|
|
2025-04-28 03:55:51 +00:00
|
|
|
|
<form
|
|
|
|
|
onSubmit={isSignIn ? handleLogin : handleRegister}
|
|
|
|
|
className="auth-form"
|
|
|
|
|
>
|
|
|
|
|
{!isSignIn && (
|
2025-03-25 06:17:41 +00:00
|
|
|
|
<input
|
2025-04-28 03:55:51 +00:00
|
|
|
|
type="text"
|
|
|
|
|
value={userName}
|
|
|
|
|
placeholder="Username"
|
|
|
|
|
onChange={(e) => setUserName(e.target.value)}
|
2025-03-25 06:17:41 +00:00
|
|
|
|
required
|
|
|
|
|
/>
|
2025-04-28 03:55:51 +00:00
|
|
|
|
)}
|
|
|
|
|
<input
|
|
|
|
|
type="email"
|
|
|
|
|
name="email"
|
|
|
|
|
value={email}
|
|
|
|
|
placeholder="Email"
|
|
|
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<div className="password-container">
|
|
|
|
|
<input
|
|
|
|
|
name="password"
|
|
|
|
|
type={showPassword ? "text" : "password"}
|
|
|
|
|
value={password}
|
|
|
|
|
placeholder="Password"
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<button
|
2025-05-14 13:09:47 +00:00
|
|
|
|
id="toogle-password"
|
2025-04-28 03:55:51 +00:00
|
|
|
|
type="button"
|
|
|
|
|
className="toggle-password"
|
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
2025-03-25 06:17:41 +00:00
|
|
|
|
>
|
2025-04-28 03:55:51 +00:00
|
|
|
|
<EyeIcon isClosed={showPassword} />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
{!isSignIn && (
|
|
|
|
|
<div className="policy-checkbox">
|
|
|
|
|
<input type="checkbox" name="" id="" required />
|
|
|
|
|
<div className="label">
|
|
|
|
|
I have read and agree to the terms of service
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-05-14 13:09:47 +00:00
|
|
|
|
<button id="form-submit" type="submit" className="continue-button">
|
2025-04-28 03:55:51 +00:00
|
|
|
|
{isSignIn ? "Continue" : "Register"}
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
<p className="policy">
|
|
|
|
|
By signing up for, or logging into, an account, you agree to our{" "}
|
|
|
|
|
<span
|
|
|
|
|
className="link"
|
|
|
|
|
onClick={() => navigate("/privacy")}
|
|
|
|
|
style={{ cursor: "pointer" }}
|
|
|
|
|
>
|
|
|
|
|
privacy policy
|
|
|
|
|
</span>{" "}
|
|
|
|
|
&{" "}
|
|
|
|
|
<span
|
|
|
|
|
className="link"
|
|
|
|
|
onClick={() => navigate("/terms")}
|
|
|
|
|
style={{ cursor: "pointer" }}
|
|
|
|
|
>
|
|
|
|
|
terms of service
|
|
|
|
|
</span>{" "}
|
|
|
|
|
whether you read them or not. You can also find these terms on our
|
|
|
|
|
website.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2025-03-25 06:17:41 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default UserAuth;
|