Improve auth flows
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
"use client";
|
||||
|
||||
import dynamic from "next/dynamic";
|
||||
import Link from "next/link";
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useAuth } from "@/context/AuthContext";
|
||||
|
||||
export default function LoginPage() {
|
||||
function resolveNextPath(candidate) {
|
||||
if (!candidate || !candidate.startsWith("/")) {
|
||||
return "/";
|
||||
}
|
||||
if (candidate.startsWith("//") || candidate.startsWith("/login") || candidate.startsWith("/register")) {
|
||||
return "/";
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
function LoginPage() {
|
||||
const {login} = useAuth();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
@@ -21,7 +33,7 @@ export default function LoginPage() {
|
||||
|
||||
try {
|
||||
await login(username, password);
|
||||
router.push("/");
|
||||
router.push(resolveNextPath(searchParams.get("next")));
|
||||
}
|
||||
|
||||
catch (err) {
|
||||
@@ -68,9 +80,13 @@ export default function LoginPage() {
|
||||
|
||||
<p className="auth-switch">
|
||||
Don't have an account?{" "}
|
||||
<Link href="/register" className="auth-switch-link">Register here</Link>
|
||||
<Link href={searchParams.get("next") ? `/register?next=${encodeURIComponent(searchParams.get("next"))}` : "/register"} className="auth-switch-link">Register here</Link>
|
||||
</p>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
export default dynamic(() => Promise.resolve(LoginPage), {
|
||||
ssr: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user