refactor: login page

This commit is contained in:
hamster1963 2024-10-21 01:55:55 +08:00
parent 01ab91976d
commit a23ed95028
2 changed files with 52 additions and 1 deletions

View File

@ -4,14 +4,17 @@ import { auth } from "@/auth";
import getEnv from "@/lib/env-entry"; import getEnv from "@/lib/env-entry";
import React from "react"; import React from "react";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";
type DashboardProps = { type DashboardProps = {
children: React.ReactNode; children: React.ReactNode;
}; };
export default async function MainLayout({ children }: DashboardProps) { export default async function MainLayout({ children }: DashboardProps) {
const session = await auth() const session = await auth()
const locale = await getLocale()
if (!session && getEnv("SITE_PASSWORD")) { if (!session && getEnv("SITE_PASSWORD")) {
redirect("/api/auth/signin"); redirect(`/${locale}/login`);
} }
return ( return (

View File

@ -0,0 +1,48 @@
import Footer from "@/app/[locale]/(main)/footer"
import Header from "@/app/[locale]/(main)/header"
import { auth, signIn } from "@/auth"
import { getLocale } from "next-intl/server"
import { redirect } from "next/navigation"
export const runtime = 'edge';
export default async function Login() {
const locale = await getLocale()
const session = await auth()
if (session) {
redirect(`/${locale}`)
}
async function handleSubmit(formData: FormData) {
'use server'
try {
await signIn("credentials", formData, { redirectTo: `/${locale}` })
} catch (error) {
redirect(`/${locale}/login`)
}
}
return (
<div className="flex min-h-screen w-full flex-col">
<main className="flex min-h-[calc(100vh_-_theme(spacing.16))] flex-1 flex-col gap-4 bg-muted/40 p-4 md:p-10 md:pt-8">
<Header />
<form
className="flex flex-col items-center justify-start gap-4 p-4 "
action={handleSubmit}
>
<section className="flex flex-col items-start gap-2">
<label className="flex flex-col items-start gap-1 ">
<p className="text-base font-semibold"></p>
<input className="px-1 border-[1px] rounded-[5px]" name="password" type="password" />
</label>
<button className=" px-1.5 py-0.5 w-fit text-sm font-semibold rounded-[8px] border bg-card hover:brightness-95 transition-all text-card-foreground shadow-lg shadow-neutral-200/40 dark:shadow-none"></button>
</section>
</form>
<Footer />
</main>
</div>
)
}