inserito proxy.ts per il login
This commit is contained in:
parent
1a81ef0865
commit
68b8ab31b7
2 changed files with 51 additions and 1 deletions
|
|
@ -6,10 +6,27 @@ import { auth, signIn } from "@/src/auth";
|
||||||
|
|
||||||
type LoginPageProps = {
|
type LoginPageProps = {
|
||||||
searchParams?: Promise<{
|
searchParams?: Promise<{
|
||||||
|
callbackUrl?: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
}>;
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getSafeRedirectTo(value: FormDataEntryValue | null): string {
|
||||||
|
const raw = String(value ?? "/");
|
||||||
|
|
||||||
|
if (raw.startsWith("/") && !raw.startsWith("//")) {
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = new URL(raw);
|
||||||
|
|
||||||
|
return `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
||||||
|
} catch {
|
||||||
|
return "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function login(formData: FormData) {
|
async function login(formData: FormData) {
|
||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
|
|
@ -24,7 +41,7 @@ async function login(formData: FormData) {
|
||||||
await signIn("credentials", {
|
await signIn("credentials", {
|
||||||
identifier,
|
identifier,
|
||||||
password: formData.get("password"),
|
password: formData.get("password"),
|
||||||
redirectTo: "/",
|
redirectTo: getSafeRedirectTo(formData.get("callbackUrl")),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AuthError) {
|
if (error instanceof AuthError) {
|
||||||
|
|
@ -81,6 +98,12 @@ export default async function LoginPage({ searchParams }: LoginPageProps) {
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
<form action={login} className="space-y-4">
|
<form action={login} className="space-y-4">
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="callbackUrl"
|
||||||
|
value={params?.callbackUrl ?? "/"}
|
||||||
|
/>
|
||||||
|
|
||||||
<label className="block">
|
<label className="block">
|
||||||
<span className="text-sm font-medium text-zinc-700">Utente o email</span>
|
<span className="text-sm font-medium text-zinc-700">Utente o email</span>
|
||||||
<input
|
<input
|
||||||
|
|
|
||||||
27
src/proxy.ts
Normal file
27
src/proxy.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
import { auth } from "@/src/auth";
|
||||||
|
|
||||||
|
export default auth((request) => {
|
||||||
|
const isLoggedIn = Boolean(request.auth);
|
||||||
|
const isLoginPage = request.nextUrl.pathname === "/login";
|
||||||
|
|
||||||
|
if (!isLoggedIn && !isLoginPage) {
|
||||||
|
const loginUrl = new URL("/login", request.nextUrl);
|
||||||
|
loginUrl.searchParams.set("callbackUrl", request.nextUrl.href);
|
||||||
|
|
||||||
|
return NextResponse.redirect(loginUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLoggedIn && isLoginPage) {
|
||||||
|
return NextResponse.redirect(new URL("/", request.nextUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.next();
|
||||||
|
});
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: [
|
||||||
|
"/((?!api/auth|_next/static|_next/image|favicon.ico|images|.*\\..*).*)",
|
||||||
|
],
|
||||||
|
};
|
||||||
Loading…
Add table
Reference in a new issue