add oauth

This commit is contained in:
Philipp Traber
2024-10-18 18:28:53 +02:00
parent 43d197e05b
commit 34b1d33ffb
26 changed files with 468 additions and 91 deletions
-4
View File
@@ -22,10 +22,6 @@ WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
## WORKAROUND:
RUN rm -rf src/app/~/dash
RUN rm -rf src/app/actions
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
+2
View File
@@ -14,3 +14,5 @@ services:
build: .
ports:
- 3000:3000
env_file:
- .env
+155 -1
View File
@@ -12,6 +12,8 @@
"@prisma/client": "^5.18.0",
"js-sha256": "^0.11.0",
"next": "^15.0.0-canary.195",
"next-auth": "^5.0.0-beta.22",
"react-hot-toast": "^2.4.1",
"ua-parser-js": "^1.0.38",
"zod": "^3.23.8"
},
@@ -40,6 +42,37 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/@auth/core": {
"version": "0.35.3",
"resolved": "https://registry.npmjs.org/@auth/core/-/core-0.35.3.tgz",
"integrity": "sha512-g6qfiqU4OtyvIEZ8J7UoIwAxEnNnLJV0/f/DW41U+4G5nhBlaCrnKhawJIJpU0D3uavXLeDT3B0BkjtiimvMDA==",
"license": "ISC",
"dependencies": {
"@panva/hkdf": "^1.1.1",
"@types/cookie": "0.6.0",
"cookie": "0.6.0",
"jose": "^5.1.3",
"oauth4webapi": "^2.10.4",
"preact": "10.11.3",
"preact-render-to-string": "5.2.3"
},
"peerDependencies": {
"@simplewebauthn/browser": "^9.0.1",
"@simplewebauthn/server": "^9.0.2",
"nodemailer": "^6.8.0"
},
"peerDependenciesMeta": {
"@simplewebauthn/browser": {
"optional": true
},
"@simplewebauthn/server": {
"optional": true
},
"nodemailer": {
"optional": true
}
}
},
"node_modules/@emnapi/runtime": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.2.0.tgz",
@@ -810,6 +843,15 @@
"node": ">=12.4.0"
}
},
"node_modules/@panva/hkdf": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@panva/hkdf/-/hkdf-1.2.1.tgz",
"integrity": "sha512-6oclG6Y3PiDFcoyk8srjLfVKyMfVCKJ27JwNPViuXziFpmdz+MZnZN/aKY0JGXgYuO/VghU0jcOAZgWXZ1Dmrw==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/panva"
}
},
"node_modules/@pkgjs/parseargs": {
"version": "0.11.0",
"resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz",
@@ -911,6 +953,12 @@
"tslib": "^2.4.0"
}
},
"node_modules/@types/cookie": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.6.0.tgz",
"integrity": "sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA==",
"license": "MIT"
},
"node_modules/@types/json5": {
"version": "0.0.29",
"resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz",
@@ -1657,6 +1705,15 @@
"dev": true,
"license": "MIT"
},
"node_modules/cookie": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz",
"integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
@@ -1689,7 +1746,6 @@
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
"integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==",
"dev": true,
"license": "MIT"
},
"node_modules/damerau-levenshtein": {
@@ -2952,6 +3008,15 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/goober": {
"version": "2.1.16",
"resolved": "https://registry.npmjs.org/goober/-/goober-2.1.16.tgz",
"integrity": "sha512-erjk19y1U33+XAMe1VTvIONHYoSqE4iS7BYUZfHaqeohLmnC0FdxEh7rQU+6MZ4OajItzjZFSRtVANrQwNq6/g==",
"license": "MIT",
"peerDependencies": {
"csstype": "^3.0.10"
}
},
"node_modules/gopd": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz",
@@ -3627,6 +3692,15 @@
"jiti": "bin/jiti.js"
}
},
"node_modules/jose": {
"version": "5.9.4",
"resolved": "https://registry.npmjs.org/jose/-/jose-5.9.4.tgz",
"integrity": "sha512-WBBl6au1qg6OHj67yCffCgFR3BADJBXN8MdRvCgJDuMv3driV2nHr7jdGvaKX9IolosAsn+M0XRArqLXUhyJHQ==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/panva"
}
},
"node_modules/js-sha256": {
"version": "0.11.0",
"resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.11.0.tgz",
@@ -3960,6 +4034,33 @@
}
}
},
"node_modules/next-auth": {
"version": "5.0.0-beta.22",
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-5.0.0-beta.22.tgz",
"integrity": "sha512-QGBo9HGOjmnJBHGXvtFztl0tM5tL0porDlk74HVoCCzXd986ApOlIW3EmiCuho7YzEopgkFiwwmcXpoCrHAtYw==",
"license": "ISC",
"dependencies": {
"@auth/core": "0.35.3"
},
"peerDependencies": {
"@simplewebauthn/browser": "^9.0.1",
"@simplewebauthn/server": "^9.0.2",
"next": "^14.0.0-0 || ^15.0.0-0",
"nodemailer": "^6.6.5",
"react": "^18.2.0 || ^19.0.0-0"
},
"peerDependenciesMeta": {
"@simplewebauthn/browser": {
"optional": true
},
"@simplewebauthn/server": {
"optional": true
},
"nodemailer": {
"optional": true
}
}
},
"node_modules/next/node_modules/postcss": {
"version": "8.4.31",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.31.tgz",
@@ -3998,6 +4099,15 @@
"node": ">=0.10.0"
}
},
"node_modules/oauth4webapi": {
"version": "2.17.0",
"resolved": "https://registry.npmjs.org/oauth4webapi/-/oauth4webapi-2.17.0.tgz",
"integrity": "sha512-lbC0Z7uzAFNFyzEYRIC+pkSVvDHJTbEW+dYlSBAlCYDe6RxUkJ26bClhk8ocBZip1wfI9uKTe0fm4Ib4RHn6uQ==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/panva"
}
},
"node_modules/object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
@@ -4493,6 +4603,28 @@
"dev": true,
"license": "MIT"
},
"node_modules/preact": {
"version": "10.11.3",
"resolved": "https://registry.npmjs.org/preact/-/preact-10.11.3.tgz",
"integrity": "sha512-eY93IVpod/zG3uMF22Unl8h9KkrcKIRs2EGar8hwLZZDU1lkjph303V9HZBwufh2s736U6VXuhD109LYqPoffg==",
"license": "MIT",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/preact"
}
},
"node_modules/preact-render-to-string": {
"version": "5.2.3",
"resolved": "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.2.3.tgz",
"integrity": "sha512-aPDxUn5o3GhWdtJtW0svRC2SS/l8D9MAgo2+AWml+BhDImb27ALf04Q2d+AHqUUOc6RdSXFIBVa2gxzgMKgtZA==",
"license": "MIT",
"dependencies": {
"pretty-format": "^3.8.0"
},
"peerDependencies": {
"preact": ">=10"
}
},
"node_modules/prelude-ls": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",
@@ -4503,6 +4635,12 @@
"node": ">= 0.8.0"
}
},
"node_modules/pretty-format": {
"version": "3.8.0",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-3.8.0.tgz",
"integrity": "sha512-WuxUnVtlWL1OfZFQFuqvnvs6MiAGk9UNsBostyBOB0Is9wb5uRESevA6rnl/rkksXaGX3GzZhPup5d6Vp1nFew==",
"license": "MIT"
},
"node_modules/prisma": {
"version": "5.19.0",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-5.19.0.tgz",
@@ -4593,6 +4731,22 @@
"react": "^18.3.1"
}
},
"node_modules/react-hot-toast": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/react-hot-toast/-/react-hot-toast-2.4.1.tgz",
"integrity": "sha512-j8z+cQbWIM5LY37pR6uZR6D4LfseplqnuAO4co4u8917hBUvXlEqyP1ZzqVLcqoyUesZZv/ImreoCeHVDpE5pQ==",
"license": "MIT",
"dependencies": {
"goober": "^2.1.10"
},
"engines": {
"node": ">=10"
},
"peerDependencies": {
"react": ">=16",
"react-dom": ">=16"
}
},
"node_modules/react-is": {
"version": "16.13.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
+2
View File
@@ -13,6 +13,8 @@
"@prisma/client": "^5.18.0",
"js-sha256": "^0.11.0",
"next": "^15.0.0-canary.195",
"next-auth": "^5.0.0-beta.22",
"react-hot-toast": "^2.4.1",
"ua-parser-js": "^1.0.38",
"zod": "^3.23.8"
},
+11 -4
View File
@@ -1,8 +1,9 @@
"use server"
import { z } from "zod";
import { set, z } from "zod";
import prisma from "../../prisma/prisma";
import { revalidatePath } from "next/cache";
import toast from "react-hot-toast";
export async function createLinkBlock(prevState: any, formData: FormData) {
@@ -63,7 +64,7 @@ export async function createLinkBlock(prevState: any, formData: FormData) {
});
} catch (e: any) {
return {
message: "Error creating link block",
message: "Error adding link to page",
error: e.message
};
}
@@ -73,7 +74,7 @@ export async function createLinkBlock(prevState: any, formData: FormData) {
revalidatePath(`/${customPage.page.shortcode}`);
return {
message: "Link block created"
message: "Adding link to page"
};
}
@@ -93,16 +94,22 @@ export async function deleteLinkBlock(prevState: any, formData: FormData) {
};
}
try {
await prisma.linkBlock.delete({
where: {
uuid: data.data.uuid
}
});
} catch (e: any) {
return {
message: "Cant delete link from page",
};
}
revalidatePath("/~/dash");
return {
message: "Link block deleted"
message: "Removed link from page"
};
}
+24 -2
View File
@@ -149,13 +149,16 @@ export async function deletePage(prevState: any, fromData: FormData) {
}
export async function pinPageToRoot(prevState: any, fromData: FormData) {
const schema = z.object({
uuid: z.string().uuid()
uuid: z.string().uuid(),
unpin: z.enum(['true', 'false']).transform((value) => value === 'true').optional()
});
const data = schema.safeParse({
uuid: fromData.get("uuid")
uuid: fromData.get("uuid"),
unpin: fromData.get("unpin")
});
if (!data.success) {
@@ -165,6 +168,24 @@ export async function pinPageToRoot(prevState: any, fromData: FormData) {
};
}
console.log(data.data.unpin);
if (data.data.unpin) {
console.log
await prisma.pinnedPage.delete({
where: {
id: 1
}
});
revalidatePath("/~/dash");
revalidatePath("/");
return {
message: "Page unpinned",
};
} else {
await prisma.pinnedPage.upsert({
where: {
id: 1
@@ -184,5 +205,6 @@ export async function pinPageToRoot(prevState: any, fromData: FormData) {
return {
message: "Page pinned",
};
}
}
+2
View File
@@ -0,0 +1,2 @@
import { handlers } from "@/auth" // Referring to the auth.ts we just created
export const { GET, POST } = handlers
+61 -7
View File
@@ -2,17 +2,16 @@
@tailwind components;
@tailwind utilities;
body {
@apply text-gray-300 bg-gray-900
@apply text-gray-300 bg-gray-900;
}
h1 {
@apply text-5xl font-bold mb-6
@apply text-5xl font-bold mb-6;
}
h2 {
@apply text-2xl font-bold mb-4
@apply text-2xl font-bold mb-4;
}
.form-group {
@@ -44,13 +43,68 @@ label {
}
button {
@apply rounded bg-green-600 hover:bg-green-700 text-white py-2 px-4 disabled:opacity-50
@apply rounded bg-green-600 hover:bg-green-700 text-white py-2 px-4 disabled:opacity-50;
}
.button-icon {
@apply h-5 w-5 inline-block align-middle;
}
.zebra {
@apply odd:bg-slate-800 p-2 rounded
@apply odd:bg-slate-800 p-2 rounded;
}
.common-red {
@apply text-red-500
@apply text-red-500;
}
@keyframes slide-in {
0% {
transform: translateY(-30px);
opacity: 0;
}
100% {
transform: translateY(0);
opacity: 1;
}
}
.slide-animated {
animation: slide-in 0.7s ease forwards;
opacity: 0;
}
@keyframes zoom-in {
0% {
transform: scale(0.7);
opacity: 0;
}
100% {
transform: scale(1);
opacity: 1;
}
}
.zoom-animated {
animation: zoom-in 1.5s ease forwards;
transform: scale(0.7);
}
@keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.fade-animated {
animation: fade-in 0.7s ease forwards;
opacity: 0;
}
.vcenter {
@apply flex items-center flex-col justify-center mt-5;
min-height: 80vh;
}
+6 -4
View File
@@ -12,11 +12,13 @@ export default async function Home() {
}
return (
(<>
<h1>Home</h1>
(<div className="vcenter">
<h1>Welcome</h1>
<p>This is a url shortner</p>
<Link href="~/dash">Dashboard</Link>
</>
<p>Currently there is no default root page defined</p>
<Link href="/~/dash" className="mt-5 hover:underline">Set pin a page in the dashboard</Link>
<Link href="https:github.com/traberph/short" className="hover:underline">Or check out the project page</Link>
</div>
)
);
}
+3 -2
View File
@@ -7,6 +7,7 @@ import CustomPageDashboard from "@/components/CustomPageDashboard";
import LinkFavicon from "@/components/LinkFavicon";
import { hashToColor } from "@/utils";
import PinPageToRootForm from "@/components/forms/pinPageToRootForm";
import { ArrowTopRightOnSquareIcon } from "@heroicons/react/24/solid";
interface PageProps {
params: Promise<{
@@ -90,10 +91,10 @@ export default async function DetailedStats({ params } : PageProps) {
{page.pinnedPage ? <p className="common-red">Pinned to root</p> : ""}
</div>
<div className="ml-auto">
<Link href={`/${page.shortcode}`}><button>View</button></Link>
<Link href={`/${page.shortcode}`}><button><ArrowTopRightOnSquareIcon className="button-icon" /></button></Link>
</div>
<div className="ml-5">
<PinPageToRootForm pageUuid={page.uuid} disabled={!!page.pinnedPage} />
<PinPageToRootForm pageUuid={page.uuid} pinned={!!page.pinnedPage} />
</div>
<div className="ml-5">
<DeletePageForm uuid={page.uuid} />
+23 -1
View File
@@ -2,14 +2,36 @@
import Link from 'next/link';
import React, { ReactNode } from 'react';
import { Toaster } from "react-hot-toast";
import { auth } from "@/auth";
import { redirect } from 'next/navigation';
import SignOutButton from '@/components/forms/signOutButton';
export default function Dashboard({ children }: { children: ReactNode }) {
export default async function Dashboard({ children }: { children: ReactNode }) {
const session = await auth();
const awSession = await session;
const user = awSession?.user;
if (!user) {
redirect("/~/");
}
return (
<div className='p-10 max-w-7xl mx-auto'>
<div className='flex justify-top'>
<Link href="/~/dash/">
<h1>Dashboard</h1>
</Link>
<div className="ml-auto flex flex-col">
{user?.name ? <>
<div>{user?.name || user.email}</div>
<SignOutButton /></> : ""}
</div>
</div>
<Toaster position='top-right' />
{children}
</div>
);
+1
View File
@@ -20,6 +20,7 @@ export default async function Dashboard() {
const redirectPages = pages.filter((page) => page.redirectPage.length > 0);
const customPages = pages.filter((page) => page.customPage.length > 0);
const pinnedPage = await prisma.pinnedPage.findFirst({ where: { id: 1 } });
return (<>
<h2>Redirect Pages</h2>
+24
View File
@@ -0,0 +1,24 @@
import { auth } from "@/auth";
import SignInButton from "@/components/forms/signInButton";
import Link from "next/link";
import { redirect } from "next/navigation";
export default async function AdminRoot() {
const session = await auth();
const awSession = await session;
const user = awSession?.user;
if (user) {
redirect("/~/dash/");
}
return (<div className="vcenter">
<Link href="https://github.com/traberph/short"><h1>traberph/short</h1></Link>
<SignInButton />
<Link href="/" className="mt-3 hover:underline">or back to root</Link>
</div>
)
}
+6
View File
@@ -0,0 +1,6 @@
import NextAuth from "next-auth"
import Authentik from "next-auth/providers/authentik"
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [Authentik],
})
+16 -7
View File
@@ -19,20 +19,24 @@ export default async function CustomPageComponent(params: { customPage: CustomPa
});
return (
<div className="flex flex-col items-center justify-center min-h-screen">
<div className="vcenter">
<Image
src={`data:image/png;base64,${customPage.image}`}
alt=" "
width={100}
height={100}
className="rounded-full"
className="rounded-full zoom-animated"
/>
<h1 className="mt-5 text-center">{customPage.title}</h1>
<div className="w-full max-w-96 mb-40 p-2">
<h1 className="mt-5 text-center zoom-animated">{customPage.title}</h1>
<div className="w-full max-w-96 p-2">
{linkBlocks.map((block) => (
<button key={block.uuid} className="bg-slate-600 hover:bg-slate-500 w-full my-2">
{linkBlocks.map((block, index) => (
<button
key={block.uuid}
className="bg-slate-600 hover:bg-slate-500 w-full my-2 slide-animated"
style={{ animationDelay: `${1.5 + index * 0.1}s` }}
>
<a href={`/${block.redirectPage.page.shortcode}`}>
<div className="relative flex items-center">
<div className="flex-shrink-0"><LinkFavicon url={block.redirectPage.dest} /></div>
@@ -42,7 +46,12 @@ export default async function CustomPageComponent(params: { customPage: CustomPa
</button>
))}
</div>
<div className="text-slate-700"><a className="underline" target="blank" href="https://github.com/traberph/short">traberph/short</a> by me running on <a className="underline" href="https://k3s.io/" target="blank">K3s</a></div>
<div
className="text-slate-700 mt-10 fade-animated"
style={{ animationDelay: `${1.5 + linkBlocks.length * 0.1}s`, opacity: 0 }}
>
<a className="underline" target="blank" href="https://github.com/traberph/short">traberph/short</a> by me running on <a className="underline" href="https://k3s.io/" target="blank">K3s</a>
</div>
</div>
);
}
+3 -2
View File
@@ -2,6 +2,7 @@ import { CustomPage } from "@prisma/client"
import prisma from "../../prisma/prisma"
import CreateLinkBlockForm from "./forms/createLinkBlockForm"
import DeleteLinkBlockForm from "./forms/deleteLinkBlockForm"
import { ArrowTopRightOnSquareIcon, ArrowTrendingUpIcon, ArrowUpCircleIcon, ArrowUpRightIcon } from "@heroicons/react/24/solid"
export default async function CustomPageDashboard(params: { customPage: CustomPage }) {
@@ -30,12 +31,12 @@ export default async function CustomPageDashboard(params: { customPage: CustomPa
<div>connected link page: <span className="font-bold">{block.redirectPage.page.shortcode}</span></div>
<div className="ml-auto">
<button>
<a href={`/${block.redirectPage.page.shortcode}`} className="btn btn-primary">Visit</a>
<a href={`/${block.redirectPage.page.shortcode}`} className="btn btn-primary"><ArrowTopRightOnSquareIcon className="button-icon" /></a>
</button>
</div>
<div className="ml-5">
<button>
<a href={`/~/dash/${block.redirectPage.page.uuid}`} className="btn btn-primary">Details</a>
<a href={`/~/dash/${block.redirectPage.page.uuid}`} className="btn btn-primary"><ArrowTrendingUpIcon className="button-icon" /></a>
</button>
</div>
<div className="ml-5">
@@ -1,7 +1,8 @@
"use client"
import { createCustomPage } from "@/actions/page_actions";
import { useActionState } from "react";
import { useActionState, useEffect } from "react";
import SubmitButton from "../SubmitButton";
import { handleToast } from "@/utils";
export default function CreateCustomPageForm() {
@@ -13,6 +14,8 @@ export default function CreateCustomPageForm() {
const [state, formAction] = useActionState(createCustomPage, initialState);
useEffect(() => { handleToast(state); }, [state]);
return (
<form action={formAction}>
<div className="form-group">
+7 -3
View File
@@ -1,7 +1,10 @@
"use client"
import { createLinkBlock } from "@/actions/block_actions"
import { useActionState } from "react";
import { useActionState, useEffect } from "react";
import SubmitButton from "../SubmitButton";
import toast from "react-hot-toast";
import { handleToast } from "@/utils";
import { LinkIcon } from "@heroicons/react/24/solid";
export default function CreateLinkBlockForm({ customPage, shortcodes }: { customPage: string, shortcodes: string[] }) {
@@ -13,6 +16,8 @@ export default function CreateLinkBlockForm({ customPage, shortcodes }: { custom
const [state, formAction] = useActionState(createLinkBlock, initialState);
useEffect(() => { handleToast(state); }, [state]);
return (<form action={formAction}>
<div className="form-group">
<label htmlFor="redirectPageShortcode">Redirect Page</label>
@@ -32,8 +37,7 @@ export default function CreateLinkBlockForm({ customPage, shortcodes }: { custom
<p className="form-label-error">{state.error?.order}</p>
</div>
<input type="hidden" name="customPageUuid" value={customPage} />
<p className="mb-2">{state.message}</p>
<SubmitButton>add to page</SubmitButton>
<SubmitButton>add to page <LinkIcon className="ml-2 button-icon" /></SubmitButton>
</form>
);
@@ -1,8 +1,10 @@
"use client"
import { createRedirectPage } from "@/actions/page_actions";
import { useActionState } from "react";
import { useActionState, useEffect } from "react";
import SubmitButton from "../SubmitButton";
import toast from "react-hot-toast";
import { handleToast } from "@/utils";
export default function CreateRedirectPageForm() {
const initialState = {
@@ -11,6 +13,8 @@ export default function CreateRedirectPageForm() {
};
const [state, formAction] = useActionState(createRedirectPage, initialState);
useEffect(() => { handleToast(state); }, [state]);
return (
<form action={formAction}>
<div className="form-group">
@@ -23,7 +27,6 @@ export default function CreateRedirectPageForm() {
<input type='text' name='dest' id='dest' className={state.error?.dest ? "border-red-400" : ""} />
<p className="form-label-error">{state.error?.dest}</p>
</div>
<p>{state.message}</p>
<SubmitButton />
</form>
);
+8 -3
View File
@@ -1,8 +1,10 @@
"use client"
import { deleteLinkBlock } from "@/actions/block_actions";
import { useActionState } from "react";
import { useActionState, useEffect } from "react";
import SubmitButton from "../SubmitButton";
import { handleToast } from "@/utils";
import { LinkSlashIcon } from "@heroicons/react/24/solid";
export default function DeleteLinkBlockForm({ linkBlockUuid }: { linkBlockUuid: string }) {
@@ -13,10 +15,13 @@ export default function DeleteLinkBlockForm({linkBlockUuid}: {linkBlockUuid:stri
const [state, formAction] = useActionState(deleteLinkBlock, initialState);
return (<form action={formAction}>
useEffect(() => { handleToast(state); }, [state]);
return (<form action={formAction}>
<input type="hidden" name="uuid" id="uuid" value={linkBlockUuid} />
<SubmitButton>unlink</SubmitButton>
<SubmitButton>
<LinkSlashIcon className="button-icon" />
</SubmitButton>
</form>
)
+7 -3
View File
@@ -1,9 +1,11 @@
"use client"
import { deletePage } from "@/actions/page_actions";
import { useActionState } from "react";
import { useActionState, useEffect } from "react";
import SubmitButton from "../SubmitButton";
import { redirect } from "next/navigation";
import toast from "react-hot-toast";
import { handleToast } from "@/utils";
import { TrashIcon } from "@heroicons/react/24/solid";
export default function DeletePageForm({ uuid }: { uuid: string }) {
@@ -13,10 +15,12 @@ export default function DeletePageForm({uuid}: {uuid: string}) {
};
const [state, formAction] = useActionState(deletePage, initialState);
useEffect(() => { handleToast(state); }, [state]);
return (
<form action={formAction}>
<input type="hidden" name="uuid" value={uuid} />
<SubmitButton>Delete</SubmitButton>
<SubmitButton><TrashIcon className="button-icon" /></SubmitButton>
</form>
);
+14 -5
View File
@@ -1,9 +1,11 @@
"use client"
import { pinPageToRoot } from "@/actions/page_actions";
import { BookmarkIcon } from "@heroicons/react/24/solid";
import { useActionState, useState } from "react";
import { handleToast } from "@/utils";
import { BookmarkIcon, BookmarkSlashIcon } from "@heroicons/react/24/solid";
import { use, useActionState, useEffect, useState } from "react";
import { toast } from 'react-hot-toast';
export default function PinPageToRootForm({ pageUuid, disabled }: { pageUuid: string, disabled: boolean }) {
export default function PinPageToRootForm({ pageUuid, pinned }: { pageUuid: string, pinned: boolean }) {
const initialState = {
message: "",
@@ -12,10 +14,17 @@ export default function PinPageToRootForm({ pageUuid, disabled }: { pageUuid: st
const [state, formAction] = useActionState(pinPageToRoot, initialState);
useEffect(() => { handleToast(state); }, [state]);
return (<form action={formAction}>
<input type="hidden" name="uuid" value={pageUuid} />
{/* <p>{state.message}</p> */}
<button type="submit" className="btn btn-primary" disabled={disabled}><BookmarkIcon className="h-5 w-5 inline-block align-middle"/></button>
<input type="hidden" name="unpin" value={"" + pinned} />
<button type="submit" className="btn btn-primary">
{pinned ?
<BookmarkSlashIcon className="h-5 w-5 inline-block align-middle" />
:
<BookmarkIcon className="h-5 w-5 inline-block align-middle" />
}
</button>
</form>)
}
+15
View File
@@ -0,0 +1,15 @@
import { signIn } from "@/auth"
export default function SignInButton() {
return (<div>
<form
action={async () => {
"use server"
await signIn("authentik")
}}
>
<button type="submit">Sign In</button>
</form>
</div>
)
}
+15
View File
@@ -0,0 +1,15 @@
import { signOut } from "@/auth"
export default function SignOutButton() {
return (
<form
className="flex"
action={async () => {
"use server"
await signOut()
}}
>
<button className='ml-auto bg-transparent p-1 hover:underline hover:bg-transparent' type="submit">logout</button>
</form>
)
}
+1
View File
@@ -0,0 +1 @@
export { auth as middleware } from "@/auth"
+13
View File
@@ -1,3 +1,5 @@
import toast from "react-hot-toast";
export function hashToColor(hash: string | null): string {
if (!hash) {
return "black";
@@ -8,3 +10,14 @@ export function hashToColor(hash: string|null): string {
const color = `rgb(${r}, ${g}, ${b})`;
return color;
}
export function handleToast(state: { message: string, error?: any }) {
if (state.message) {
console.log(state);
if (state.error) {
toast.error(state.message);
} else {
toast.success(state.message);
}
}
}