import { Button, Input } from "@mui/joy"; import React, { FormEvent, useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { Link, useNavigate } from "react-router-dom"; import * as api from "../helpers/api"; import useLoading from "../hooks/useLoading"; import { useAppSelector } from "../stores"; import useUserStore from "../stores/v1/user"; const SignIn: React.FC = () => { const navigate = useNavigate(); const userStore = useUserStore(); const { workspaceProfile: { disallowSignUp, profile: { mode }, }, } = useAppSelector((state) => state.global); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const actionBtnLoadingState = useLoading(false); const allowConfirm = email.length > 0 && password.length > 0; useEffect(() => { if (userStore.getCurrentUser()) { return navigate("/", { replace: true, }); } if (mode === "demo") { setEmail("steven@usememos.com"); setPassword("secret"); } }, []); const handleEmailInputChanged = (e: React.ChangeEvent) => { const text = e.target.value as string; setEmail(text); }; const handlePasswordInputChanged = (e: React.ChangeEvent) => { const text = e.target.value as string; setPassword(text); }; const handleSigninBtnClick = async (e: FormEvent) => { e.preventDefault(); if (actionBtnLoadingState.isLoading) { return; } try { actionBtnLoadingState.setLoading(); await api.signin(email, password); const user = await userStore.fetchCurrentUser(); if (user) { navigate("/", { replace: true, }); } else { toast.error("Signin failed"); } } catch (error: any) { console.error(error); toast.error(error.response.data.message); } actionBtnLoadingState.setFinish(); }; return (
logo Slash
Email
Password
{!disallowSignUp && (

{"Don't have an account yet?"} Sign up

)}
); }; export default SignIn;