Move login and logout actions into app/(auth)/actions.ts

main
Deardo Satria 2 weeks ago
parent bcfdc5c27a
commit a44fb24136

@ -34,6 +34,7 @@ Tanpa folder `src/`. Kode aplikasi di root, sejajar config.
``` ```
app/ app/
(auth)/actions.ts Server Actions auth (login, logout), dipakai lintas route group
(auth)/login/page.tsx halaman publik (belum login) (auth)/login/page.tsx halaman publik (belum login)
(dashboard)/layout.tsx kerangka UI halaman yang wajib login (bukan tempat cek session) (dashboard)/layout.tsx kerangka UI halaman yang wajib login (bukan tempat cek session)
(dashboard)/<fitur>/ (dashboard)/<fitur>/
@ -60,6 +61,9 @@ proxy.ts pengganti middleware di Next 16; hanya cek rin
- Folder berawalan `_` tidak menjadi route; pakai untuk komponen yang dikolokasikan. - Folder berawalan `_` tidak menjadi route; pakai untuk komponen yang dikolokasikan.
- Route group `(nama)` hanya untuk mengelompokkan layout, tidak muncul di URL. - Route group `(nama)` hanya untuk mengelompokkan layout, tidak muncul di URL.
- Import pakai alias `@/` (mis. `@/db`, `@/data/barang`). - Import pakai alias `@/` (mis. `@/db`, `@/data/barang`).
- `actions.ts` dikolokasikan dengan fitur yang memakainya. Action yang dipakai lintas route group
(mis. `logout` dipanggil dari `(dashboard)`) ditaruh di level route group pemiliknya, lalu
di-import lewat alias (`@/app/(auth)/actions`), bukan path relatif `../(auth)/...`.
## Aturan data dan Server Actions ## Aturan data dan Server Actions

@ -48,3 +48,8 @@ export async function login(
redirect("/"); redirect("/");
} }
export async function logout() {
await auth.api.signOut({ headers: await headers() });
redirect("/login");
}

@ -16,7 +16,7 @@ import {
FieldLabel, FieldLabel,
} from "@/components/ui/field"; } from "@/components/ui/field";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { login, type LoginState } from "../actions"; import { login, type LoginState } from "../../actions";
const initialState: LoginState = {}; const initialState: LoginState = {};

@ -1,10 +0,0 @@
"use server";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
export async function logout() {
await auth.api.signOut({ headers: await headers() });
redirect("/login");
}

@ -1,6 +1,6 @@
import { Suspense } from "react"; import { Suspense } from "react";
import { getCurrentUser } from "@/data/auth"; import { getCurrentUser } from "@/data/auth";
import { logout } from "./actions"; import { logout } from "@/app/(auth)/actions";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
export default function DashboardPage() { export default function DashboardPage() {

Loading…
Cancel
Save