Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e6a032e8e0 | |||
| 81a0c2c6c8 |
@@ -1,4 +1,7 @@
|
|||||||
import type { Metadata } from "next";
|
import type { Metadata } from "next";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { getServerSession } from "next-auth";
|
||||||
|
import { authOptions } from "@/lib/auth";
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: "Masuk",
|
title: "Masuk",
|
||||||
@@ -8,6 +11,15 @@ export const metadata: Metadata = {
|
|||||||
robots: { index: false, follow: true },
|
robots: { index: false, follow: true },
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function LoginLayout({ children }: { children: React.ReactNode }) {
|
export default async function LoginLayout({
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) {
|
||||||
|
// User yang sudah login tidak boleh mengakses halaman login lagi.
|
||||||
|
const session = await getServerSession(authOptions);
|
||||||
|
if (session?.user) {
|
||||||
|
redirect(session.user.isAdmin ? "/admin" : "/");
|
||||||
|
}
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,13 +38,16 @@ function LoginForm() {
|
|||||||
if (result?.error) {
|
if (result?.error) {
|
||||||
setError(result.error);
|
setError(result.error);
|
||||||
} else {
|
} else {
|
||||||
const rawCallback = searchParams.get("callbackUrl");
|
const callbackPath = safeInternalPath(searchParams.get("callbackUrl"));
|
||||||
let next = safeInternalPath(rawCallback);
|
const session = await getSession();
|
||||||
// Tanpa callbackUrl eksplisit, arahkan admin ke dashboard /admin.
|
// Admin selalu diarahkan ke dashboard /admin setelah login — kecuali
|
||||||
if (!rawCallback) {
|
// callbackUrl memang menuju sub-halaman admin (deep link dari /admin/...).
|
||||||
const session = await getSession();
|
// callbackUrl non-admin (mis. "/" sisa dari percobaan login Google) tidak
|
||||||
if (session?.user?.isAdmin) next = "/admin";
|
// boleh membuat admin "nyangkut" di halaman publik.
|
||||||
}
|
const next =
|
||||||
|
session?.user?.isAdmin && !callbackPath.startsWith("/admin")
|
||||||
|
? "/admin"
|
||||||
|
: callbackPath;
|
||||||
router.push(next);
|
router.push(next);
|
||||||
router.refresh();
|
router.refresh();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import type { Metadata } from "next";
|
import type { Metadata } from "next";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { getServerSession } from "next-auth";
|
||||||
|
import { authOptions } from "@/lib/auth";
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: "Daftar Akun",
|
title: "Daftar Akun",
|
||||||
@@ -7,6 +10,15 @@ export const metadata: Metadata = {
|
|||||||
alternates: { canonical: "/register" },
|
alternates: { canonical: "/register" },
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function RegisterLayout({ children }: { children: React.ReactNode }) {
|
export default async function RegisterLayout({
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) {
|
||||||
|
// User yang sudah login tidak boleh mengakses halaman daftar lagi.
|
||||||
|
const session = await getServerSession(authOptions);
|
||||||
|
if (session?.user) {
|
||||||
|
redirect(session.user.isAdmin ? "/admin" : "/");
|
||||||
|
}
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "setrip",
|
"name": "setrip",
|
||||||
"version": "0.16.8",
|
"version": "0.16.9",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "setrip",
|
"name": "setrip",
|
||||||
"version": "0.16.8",
|
"version": "0.16.9",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@next-auth/prisma-adapter": "^1.0.7",
|
"@next-auth/prisma-adapter": "^1.0.7",
|
||||||
"@prisma/adapter-pg": "^7.7.0",
|
"@prisma/adapter-pg": "^7.7.0",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "setrip",
|
"name": "setrip",
|
||||||
"version": "0.16.8",
|
"version": "0.16.9",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
|
|||||||
Reference in New Issue
Block a user