refactor: migrate pages and API routes to App Router
This commit is contained in:
@@ -0,0 +1,245 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, type FormEvent } from "react";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import {
|
||||
ArrowRight,
|
||||
Eye,
|
||||
EyeOff,
|
||||
ArrowUpRight,
|
||||
LoaderCircle,
|
||||
} from "lucide-react";
|
||||
import { Brand } from "@/components/brand";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { api, errorText, startOAuth } from "@/lib/api";
|
||||
export default function Login() {
|
||||
const router = useRouter();
|
||||
const [register, setRegister] = useState(false),
|
||||
[email, setEmail] = useState(""),
|
||||
[password, setPassword] = useState(""),
|
||||
[show, setShow] = useState(false),
|
||||
[busy, setBusy] = useState(false),
|
||||
[message, setMessage] = useState(""),
|
||||
[error, setError] = useState("");
|
||||
useEffect(() => {
|
||||
const hash = new URLSearchParams(location.hash.slice(1));
|
||||
const code = hash.get("code"),
|
||||
verify = hash.get("verify_email"),
|
||||
failure = hash.get("error");
|
||||
if (!code && !verify && !failure) return;
|
||||
history.replaceState(null, "", location.pathname);
|
||||
if (failure) {
|
||||
setError("第三方登录未完成,请重新尝试。");
|
||||
return;
|
||||
}
|
||||
setBusy(true);
|
||||
const verifier = sessionStorage.getItem("foundry_verifier");
|
||||
sessionStorage.removeItem("foundry_verifier");
|
||||
(verify
|
||||
? api("auth/verify-email", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ token: verify }),
|
||||
})
|
||||
: verifier
|
||||
? api("auth/exchange", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-Foundry-Code": code!,
|
||||
"X-Foundry-Verifier": verifier,
|
||||
},
|
||||
})
|
||||
: Promise.reject(new Error("invalid_session"))
|
||||
)
|
||||
.then(() => router.replace("/dashboard"))
|
||||
.catch((e) => setError(errorText(e)))
|
||||
.finally(() => setBusy(false));
|
||||
}, [router]);
|
||||
async function submit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
setBusy(true);
|
||||
setError("");
|
||||
setMessage("");
|
||||
try {
|
||||
await api("auth/" + (register ? "register" : "login"), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
if (register)
|
||||
setMessage("验证邮件已请求发送,请检查邮箱并点击验证链接。");
|
||||
else router.push("/dashboard");
|
||||
} catch (e) {
|
||||
setError(errorText(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
async function oauth(p: "google" | "slack") {
|
||||
setError("");
|
||||
setMessage("正在前往 " + (p === "google" ? "Google" : "Slack") + "…");
|
||||
setBusy(true);
|
||||
try {
|
||||
await startOAuth(p);
|
||||
} catch {
|
||||
setBusy(false);
|
||||
setMessage("");
|
||||
setError("无法启动登录,请重新尝试。");
|
||||
}
|
||||
}
|
||||
return (
|
||||
<div className="auth-page">
|
||||
<aside className="auth-story">
|
||||
<Brand light />
|
||||
<div className="auth-story-copy">
|
||||
<div className="eyebrow">A SPACE FOR YOUR NEXT IDEA</div>
|
||||
<h1>
|
||||
给想法留白,
|
||||
<br />
|
||||
让灵感有形。
|
||||
</h1>
|
||||
<p>
|
||||
收集一闪而过的灵感,把有用的知识连接起来。
|
||||
<br />
|
||||
下一件作品,从这里开始。
|
||||
</p>
|
||||
</div>
|
||||
<div className="auth-collage">
|
||||
<div className="auth-orbit" />
|
||||
<article className="auth-note">
|
||||
<small>01 / THE IDEA</small>
|
||||
<h2>
|
||||
留住清晨
|
||||
<br />
|
||||
的一束光。
|
||||
</h2>
|
||||
<p>灵感片段 · 随手记下</p>
|
||||
</article>
|
||||
<article className="auth-art">
|
||||
<small>02 / IN THE MAKING</small>
|
||||
<div className="light-shape" />
|
||||
<h3>光的形状</h3>
|
||||
<p>让一个念头,慢慢成形。</p>
|
||||
</article>
|
||||
</div>
|
||||
<span className="auth-caption">COLLECT. CONNECT. CREATE.</span>
|
||||
<footer>啡跃 Endoleap 为下一次创作,留一个起点。</footer>
|
||||
</aside>
|
||||
<main className="auth-main">
|
||||
<Link href="/" className="auth-back">
|
||||
返回首页 <ArrowUpRight size={14} />
|
||||
</Link>
|
||||
<div className="auth-form-wrap">
|
||||
<div className="eyebrow">WELCOME TO YOUR CREATIVE SPACE</div>
|
||||
<h1>{register ? "创建你的账户" : "欢迎回来"}</h1>
|
||||
<p>
|
||||
{register ? "为下一次创作,留一个起点。" : "登录,继续你的创作。"}
|
||||
</p>
|
||||
<div className="auth-tabs" role="tablist" aria-label="登录方式">
|
||||
<button
|
||||
role="tab"
|
||||
aria-selected={!register}
|
||||
onClick={() => {
|
||||
setRegister(false);
|
||||
setError("");
|
||||
setMessage("");
|
||||
}}
|
||||
>
|
||||
登录
|
||||
</button>
|
||||
<button
|
||||
role="tab"
|
||||
aria-selected={register}
|
||||
onClick={() => {
|
||||
setRegister(true);
|
||||
setError("");
|
||||
setMessage("");
|
||||
}}
|
||||
>
|
||||
创建账户
|
||||
</button>
|
||||
</div>
|
||||
<form onSubmit={submit}>
|
||||
<label htmlFor="email">邮箱地址</label>
|
||||
<input
|
||||
id="email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
placeholder="you@example.com"
|
||||
required
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
/>
|
||||
<label htmlFor="password">密码</label>
|
||||
<div className="password-field">
|
||||
<input
|
||||
id="password"
|
||||
type={show ? "text" : "password"}
|
||||
autoComplete={register ? "new-password" : "current-password"}
|
||||
placeholder={register ? "至少 12 个字符" : "输入你的密码"}
|
||||
required
|
||||
minLength={register ? 12 : 1}
|
||||
maxLength={72}
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={show ? "隐藏密码" : "显示密码"}
|
||||
onClick={() => setShow(!show)}
|
||||
>
|
||||
{show ? <EyeOff size={18} /> : <Eye size={18} />}
|
||||
</button>
|
||||
</div>
|
||||
{error && (
|
||||
<p role="alert" className="form-error">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{message && (
|
||||
<p role="status" className="form-success">
|
||||
{message}
|
||||
</p>
|
||||
)}
|
||||
<Button className="auth-submit" disabled={busy} type="submit">
|
||||
{busy ? (
|
||||
<LoaderCircle className="animate-spin" size={18} />
|
||||
) : (
|
||||
<>
|
||||
{register ? "创建账户" : "登录"}
|
||||
<ArrowRight size={17} />
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</form>
|
||||
<div className="auth-divider">
|
||||
<span>或使用以下方式继续</span>
|
||||
</div>
|
||||
<div className="social-buttons">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => oauth("google")}
|
||||
disabled={busy}
|
||||
>
|
||||
<strong className="google-mark">G</strong> Google
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => oauth("slack")}
|
||||
disabled={busy}
|
||||
>
|
||||
<strong className="slack-mark">#</strong> Slack
|
||||
</Button>
|
||||
</div>
|
||||
<p className="auth-notice">
|
||||
首次注册需要验证邮箱。
|
||||
<br />
|
||||
验证成功后,即可进入你的创作空间。
|
||||
</p>
|
||||
<Link href="/dashboard?demo=1" className="demo-link">
|
||||
先看看工作台 <ArrowUpRight size={13} />
|
||||
</Link>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user