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
+16 -9
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) {
};
}
await prisma.linkBlock.delete({
where: {
uuid: data.data.uuid
}
});
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"
};
}
+42 -20
View File
@@ -49,7 +49,7 @@ export async function createCustomPage(prevState: any, fromData: FormData) {
message: "Shortcode already exists",
error: {
shortcode: ["Shortcode already exists"]
}
};
}
@@ -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,24 +168,43 @@ export async function pinPageToRoot(prevState: any, fromData: FormData) {
};
}
await prisma.pinnedPage.upsert({
where: {
id: 1
},
update: {
pageUuid: data.data.uuid
},
create: {
id: 1,
pageUuid: data.data.uuid
}
});
revalidatePath("/~/dash");
revalidatePath("/");
return {
message: "Page pinned",
};
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
},
update: {
pageUuid: data.data.uuid
},
create: {
id: 1,
pageUuid: data.data.uuid
}
});
revalidatePath("/~/dash");
revalidatePath("/");
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
+62 -8
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;
}
+7 -5
View File
@@ -5,18 +5,20 @@ import { redirect } from "next/navigation";
export default async function Home() {
const pinnedPage = await prisma.pinnedPage.findFirst({where: {id: 1}, include: {page: true}});
const pinnedPage = await prisma.pinnedPage.findFirst({ where: { id: 1 }, include: { page: true } });
if (pinnedPage) {
redirect(pinnedPage.page.shortcode);
}
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>
)
);
}
+6 -5
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<{
@@ -14,8 +15,8 @@ interface PageProps {
}>
}
export default async function DetailedStats({ params } : PageProps) {
export default async function DetailedStats({ params }: PageProps) {
const { uuid } = await params;
const page = await prisma.page.findUnique({
@@ -87,13 +88,13 @@ export default async function DetailedStats({ params } : PageProps) {
<p><a className="underline" href={page.redirectPage[0].dest}>{page.redirectPage[0].dest}</a></p> : ""}
</div>
<div className="ml-5">
{page.pinnedPage ? <p className="common-red">Pinned to root</p> : ""}
{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} />
+26 -4
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'>
<Link href="/~/dash/">
<h1>Dashboard</h1>
</Link>
<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>
);
+11 -6
View File
@@ -1,10 +1,12 @@
"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}) {
export default function DeleteLinkBlockForm({ linkBlockUuid }: { linkBlockUuid: string }) {
const initialState = {
message: "",
@@ -13,11 +15,14 @@ export default function DeleteLinkBlockForm({linkBlockUuid}: {linkBlockUuid:stri
const [state, formAction] = useActionState(deleteLinkBlock, initialState);
return (<form action={formAction}>
useEffect(() => { handleToast(state); }, [state]);
<input type="hidden" name="uuid" id="uuid" value={linkBlockUuid}/>
<SubmitButton>unlink</SubmitButton>
return (<form action={formAction}>
<input type="hidden" name="uuid" id="uuid" value={linkBlockUuid} />
<SubmitButton>
<LinkSlashIcon className="button-icon" />
</SubmitButton>
</form>
)
}
+10 -6
View File
@@ -1,22 +1,26 @@
"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}) {
export default function DeletePageForm({ uuid }: { uuid: string }) {
const initialState = {
message: "",
error: {},
};
};
const [state, formAction] = useActionState(deletePage, initialState);
useEffect(() => { handleToast(state); }, [state]);
return (
<form action={formAction}>
<input type="hidden" name="uuid" value={uuid}/>
<SubmitButton>Delete</SubmitButton>
<input type="hidden" name="uuid" value={uuid} />
<SubmitButton><TrashIcon className="button-icon" /></SubmitButton>
</form>
);
+15 -6
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}>
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"
+14 -1
View File
@@ -1,4 +1,6 @@
export function hashToColor(hash: string|null): string {
import toast from "react-hot-toast";
export function hashToColor(hash: string | null): string {
if (!hash) {
return "black";
}
@@ -7,4 +9,15 @@ export function hashToColor(hash: string|null): string {
const b = parseInt(hash.slice(4, 6), 16);
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);
}
}
}