chore: update auth pages

This commit is contained in:
Steven 2023-10-05 12:38:46 +08:00
parent 8aae0d00cd
commit 01ff3f73f8
4 changed files with 205 additions and 94 deletions

View File

@ -70,7 +70,7 @@ const Header = () => {
title: t("common.settings"), title: t("common.settings"),
icon: <Icon.Settings className="mr-3 w-6 h-auto opacity-70" />, icon: <Icon.Settings className="mr-3 w-6 h-auto opacity-70" />,
}; };
const authNavLink: NavLinkItem = { const signInNavLink: NavLinkItem = {
id: "header-auth", id: "header-auth",
path: "/auth", path: "/auth",
title: t("common.sign-in"), title: t("common.sign-in"),
@ -79,7 +79,7 @@ const Header = () => {
const navLinks: NavLinkItem[] = user const navLinks: NavLinkItem[] = user
? [homeNavLink, dailyReviewNavLink, resourcesNavLink, exploreNavLink, archivedNavLink, settingNavLink] ? [homeNavLink, dailyReviewNavLink, resourcesNavLink, exploreNavLink, archivedNavLink, settingNavLink]
: [exploreNavLink, authNavLink]; : [exploreNavLink, signInNavLink];
return ( return (
<div <div

View File

@ -1,8 +1,8 @@
import { Button, Divider, Input } from "@mui/joy"; import { Button, Divider, Input } from "@mui/joy";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import { Link } from "react-router-dom";
import AppearanceSelect from "@/components/AppearanceSelect"; import AppearanceSelect from "@/components/AppearanceSelect";
import Icon from "@/components/Icon";
import LocaleSelect from "@/components/LocaleSelect"; import LocaleSelect from "@/components/LocaleSelect";
import * as api from "@/helpers/api"; import * as api from "@/helpers/api";
import { absolutifyLink } from "@/helpers/utils"; import { absolutifyLink } from "@/helpers/utils";
@ -10,7 +10,7 @@ import useLoading from "@/hooks/useLoading";
import { useGlobalStore, useUserStore } from "@/store/module"; import { useGlobalStore, useUserStore } from "@/store/module";
import { useTranslate } from "@/utils/i18n"; import { useTranslate } from "@/utils/i18n";
const Auth = () => { const SignIn = () => {
const t = useTranslate(); const t = useTranslate();
const globalStore = useGlobalStore(); const globalStore = useGlobalStore();
const userStore = useUserStore(); const userStore = useUserStore();
@ -57,11 +57,7 @@ const Auth = () => {
const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => { const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
if (systemStatus?.host) { handleSignInButtonClick();
handleSignInButtonClick();
} else {
handleSignUpButtonClick();
}
}; };
const handleSignInButtonClick = async () => { const handleSignInButtonClick = async () => {
@ -89,31 +85,6 @@ const Auth = () => {
actionBtnLoadingState.setFinish(); actionBtnLoadingState.setFinish();
}; };
const handleSignUpButtonClick = async () => {
if (username === "" || password === "") {
return;
}
if (actionBtnLoadingState.isLoading) {
return;
}
try {
actionBtnLoadingState.setLoading();
await api.signup(username, password);
const user = await userStore.doSignIn();
if (user) {
window.location.href = "/";
} else {
toast.error(t("message.signup-failed"));
}
} catch (error: any) {
console.error(error);
toast.error(error.response.data.message || error.message || t("message.signup-failed"));
}
actionBtnLoadingState.setFinish();
};
const handleSignInWithIdentityProvider = async (identityProvider: IdentityProvider) => { const handleSignInWithIdentityProvider = async (identityProvider: IdentityProvider) => {
const stateQueryParameter = `auth.signin.${identityProvider.name}-${identityProvider.id}`; const stateQueryParameter = `auth.signin.${identityProvider.name}-${identityProvider.id}`;
if (identityProvider.type === "OAUTH2") { if (identityProvider.type === "OAUTH2") {
@ -132,62 +103,62 @@ const Auth = () => {
<div className="flex flex-row justify-center items-center w-full h-full dark:bg-zinc-800"> <div className="flex flex-row justify-center items-center w-full h-full dark:bg-zinc-800">
<div className="w-80 max-w-full h-full py-4 flex flex-col justify-start items-center"> <div className="w-80 max-w-full h-full py-4 flex flex-col justify-start items-center">
<div className="w-full py-4 grow flex flex-col justify-center items-center"> <div className="w-full py-4 grow flex flex-col justify-center items-center">
<div className="w-full flex flex-col justify-center items-center mb-2"> <div className="w-full flex flex-row justify-center items-center mb-6">
<img className="h-20 w-auto rounded-full shadow" src={systemStatus.customizedProfile.logoUrl} alt="" /> <img className="h-14 w-auto rounded-full shadow" src={systemStatus.customizedProfile.logoUrl} alt="" />
<p className="mt-2 text-3xl text-black opacity-80 dark:text-gray-200">{systemStatus.customizedProfile.name}</p> <p className="ml-4 text-5xl text-black opacity-80 dark:text-gray-200">{systemStatus.customizedProfile.name}</p>
</div> </div>
{!disablePasswordLogin && ( {!disablePasswordLogin && (
<form className="w-full mt-2" onSubmit={handleFormSubmit}> <>
<div className="flex flex-col justify-start items-start w-full gap-4"> <form className="w-full mt-2" onSubmit={handleFormSubmit}>
<Input <div className="flex flex-col justify-start items-start w-full gap-4">
className="w-full" <div className="w-full flex flex-col justify-start items-start gap-2">
size="lg" <span className="leading-8 text-gray-600">{t("common.username")}</span>
type="text" <Input
readOnly={actionBtnLoadingState.isLoading} className="w-full"
placeholder={t("common.username")} size="lg"
value={username} type="text"
onChange={handleUsernameInputChanged} readOnly={actionBtnLoadingState.isLoading}
required placeholder={t("common.username")}
/> value={username}
<Input onChange={handleUsernameInputChanged}
className="w-full" required
size="lg" />
type="password" </div>
readOnly={actionBtnLoadingState.isLoading} <div className="w-full flex flex-col justify-start items-start gap-2">
placeholder={t("common.password")} <span className="leading-8 text-gray-600">{t("common.password")}</span>
value={password} <Input
onChange={handlePasswordInputChanged} className="w-full"
required size="lg"
/> type="password"
</div> readOnly={actionBtnLoadingState.isLoading}
<div className="flex flex-row justify-end items-center w-full mt-6"> placeholder={t("common.password")}
{actionBtnLoadingState.isLoading && <Icon.Loader className="w-4 h-auto mr-2 animate-spin dark:text-gray-300" />} value={password}
{!systemStatus.host ? ( onChange={handlePasswordInputChanged}
<Button disabled={actionBtnLoadingState.isLoading} onClick={handleSignUpButtonClick}> required
{t("common.sign-up")} />
</div>
</div>
<div className="flex flex-row justify-end items-center w-full mt-6">
<Button
className="w-full"
type="submit"
disabled={actionBtnLoadingState.isLoading}
loading={actionBtnLoadingState.isLoading}
onClick={handleSignInButtonClick}
>
{t("common.sign-in")}
</Button> </Button>
) : ( </div>
<> </form>
{systemStatus?.allowSignUp && ( {systemStatus.allowSignUp && (
<> <p className="w-full mt-4 text-sm">
<Button variant={"plain"} disabled={actionBtnLoadingState.isLoading} onClick={handleSignUpButtonClick}> <span className="dark:text-gray-500">{"Don't have an account yet?"}</span>
{t("common.sign-up")} <Link to="/auth/signup" className="cursor-pointer ml-2 text-blue-600 hover:underline">
</Button> {t("common.sign-up")}
<span className="mr-2 font-mono text-gray-200">/</span> </Link>
</> </p>
)} )}
<Button type="submit" disabled={actionBtnLoadingState.isLoading} onClick={handleSignInButtonClick}> </>
{t("common.sign-in")}
</Button>
</>
)}
</div>
</form>
)}
{!systemStatus.host && (
<p className="w-full inline-block float-right text-sm mt-4 text-gray-500 text-right whitespace-pre-wrap">
{t("auth.host-tip")}
</p>
)} )}
{identityProviderList.length > 0 && ( {identityProviderList.length > 0 && (
<> <>
@ -218,4 +189,4 @@ const Auth = () => {
); );
}; };
export default Auth; export default SignIn;

135
web/src/pages/SignUp.tsx Normal file
View File

@ -0,0 +1,135 @@
import { Button, Input } from "@mui/joy";
import { useState } from "react";
import { toast } from "react-hot-toast";
import { Link } from "react-router-dom";
import AppearanceSelect from "@/components/AppearanceSelect";
import LocaleSelect from "@/components/LocaleSelect";
import * as api from "@/helpers/api";
import useLoading from "@/hooks/useLoading";
import { useGlobalStore, useUserStore } from "@/store/module";
import { useTranslate } from "@/utils/i18n";
const SignUp = () => {
const t = useTranslate();
const globalStore = useGlobalStore();
const userStore = useUserStore();
const actionBtnLoadingState = useLoading(false);
const { appearance, locale, systemStatus } = globalStore.state;
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const handleUsernameInputChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
const text = e.target.value as string;
setUsername(text);
};
const handlePasswordInputChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
const text = e.target.value as string;
setPassword(text);
};
const handleLocaleSelectChange = (locale: Locale) => {
globalStore.setLocale(locale);
};
const handleAppearanceSelectChange = (appearance: Appearance) => {
globalStore.setAppearance(appearance);
};
const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
handleSignUpButtonClick();
};
const handleSignUpButtonClick = async () => {
if (username === "" || password === "") {
return;
}
if (actionBtnLoadingState.isLoading) {
return;
}
try {
actionBtnLoadingState.setLoading();
await api.signup(username, password);
const user = await userStore.doSignIn();
if (user) {
window.location.href = "/";
} else {
toast.error(t("message.signup-failed"));
}
} catch (error: any) {
console.error(error);
toast.error(error.response.data.message || error.message || t("message.signup-failed"));
}
actionBtnLoadingState.setFinish();
};
return (
<div className="flex flex-row justify-center items-center w-full h-full dark:bg-zinc-800">
<div className="w-80 max-w-full h-full py-4 flex flex-col justify-start items-center">
<div className="w-full py-4 grow flex flex-col justify-center items-center">
<div className="w-full flex flex-row justify-center items-center mb-6">
<img className="h-14 w-auto rounded-full shadow" src={systemStatus.customizedProfile.logoUrl} alt="" />
<p className="ml-4 text-5xl text-black opacity-80 dark:text-gray-200">{systemStatus.customizedProfile.name}</p>
</div>
<p className="w-full text-2xl mt-6 dark:text-gray-500">Create your account</p>
<form className="w-full mt-2" onSubmit={handleFormSubmit}>
<div className="flex flex-col justify-start items-start w-full gap-4">
<div className="w-full flex flex-col justify-start items-start gap-2">
<span className="leading-8 text-gray-600">{t("common.username")}</span>
<Input
className="w-full"
size="lg"
type="text"
readOnly={actionBtnLoadingState.isLoading}
placeholder={t("common.username")}
value={username}
onChange={handleUsernameInputChanged}
required
/>
</div>
<div className="w-full flex flex-col justify-start items-start gap-2">
<span className="leading-8 text-gray-600">{t("common.password")}</span>
<Input
className="w-full"
size="lg"
type="password"
readOnly={actionBtnLoadingState.isLoading}
placeholder={t("common.password")}
value={password}
onChange={handlePasswordInputChanged}
required
/>
</div>
</div>
<div className="flex flex-row justify-end items-center w-full mt-6">
<Button
className="w-full"
type="submit"
disabled={actionBtnLoadingState.isLoading}
loading={actionBtnLoadingState.isLoading}
onClick={handleSignUpButtonClick}
>
{t("common.sign-up")}
</Button>
</div>
</form>
<p className="w-full mt-4 text-sm">
<span className="dark:text-gray-500">{"Already has an account?"}</span>
<Link to="/auth" className="cursor-pointer ml-2 text-blue-600 hover:underline">
{t("common.sign-in")}
</Link>
</p>
</div>
<div className="flex flex-row items-center justify-center w-full gap-2">
<LocaleSelect value={locale} onChange={handleLocaleSelectChange} />
<AppearanceSelect value={appearance} onChange={handleAppearanceSelectChange} />
</div>
</div>
</div>
);
};
export default SignUp;

View File

@ -1,20 +1,21 @@
import { lazy } from "react"; import { lazy } from "react";
import { createBrowserRouter, redirect } from "react-router-dom"; import { createBrowserRouter, redirect } from "react-router-dom";
import App from "@/App"; import App from "@/App";
import Archived from "@/pages/Archived";
import DailyReview from "@/pages/DailyReview";
import Resources from "@/pages/Resources";
import Setting from "@/pages/Setting";
import { initialGlobalState, initialUserState } from "@/store/module"; import { initialGlobalState, initialUserState } from "@/store/module";
const Root = lazy(() => import("@/layouts/Root")); const Root = lazy(() => import("@/layouts/Root"));
const Auth = lazy(() => import("@/pages/Auth")); const SignIn = lazy(() => import("@/pages/SignIn"));
const SignUp = lazy(() => import("@/pages/SignUp"));
const AuthCallback = lazy(() => import("@/pages/AuthCallback")); const AuthCallback = lazy(() => import("@/pages/AuthCallback"));
const Explore = lazy(() => import("@/pages/Explore")); const Explore = lazy(() => import("@/pages/Explore"));
const Home = lazy(() => import("@/pages/Home")); const Home = lazy(() => import("@/pages/Home"));
const UserProfile = lazy(() => import("@/pages/UserProfile")); const UserProfile = lazy(() => import("@/pages/UserProfile"));
const MemoDetail = lazy(() => import("@/pages/MemoDetail")); const MemoDetail = lazy(() => import("@/pages/MemoDetail"));
const EmbedMemo = lazy(() => import("@/pages/EmbedMemo")); const EmbedMemo = lazy(() => import("@/pages/EmbedMemo"));
const Archived = lazy(() => import("@/pages/Archived"));
const DailyReview = lazy(() => import("@/pages/DailyReview"));
const Resources = lazy(() => import("@/pages/Resources"));
const Setting = lazy(() => import("@/pages/Setting"));
const NotFound = lazy(() => import("@/pages/NotFound")); const NotFound = lazy(() => import("@/pages/NotFound"));
const initialGlobalStateLoader = (() => { const initialGlobalStateLoader = (() => {
@ -58,7 +59,11 @@ const router = createBrowserRouter([
children: [ children: [
{ {
path: "/auth", path: "/auth",
element: <Auth />, element: <SignIn />,
},
{
path: "/auth/signup",
element: <SignUp />,
}, },
{ {
path: "/auth/callback", path: "/auth/callback",