From 5c834e269bfff00b92ee63862cbbc7165743de9e Mon Sep 17 00:00:00 2001
From: "Alex Pooley (@zuedev)"
Date: Wed, 13 May 2026 16:36:35 +0100
Subject: add better-auth
---
.../174bg.net/src/app/api/auth/[...all]/route.js | 4 ++
.../red-right-hand/174bg.net/src/app/login/page.js | 51 ++++++++++++++++++++++
.../174bg.net/src/app/secure/page.js | 13 ++++++
communities/red-right-hand/174bg.net/src/auth.js | 20 +++++++++
.../174bg.net/src/lib/auth-client.js | 6 +++
.../red-right-hand/174bg.net/src/middleware.js | 31 +++++++++++++
6 files changed, 125 insertions(+)
create mode 100644 communities/red-right-hand/174bg.net/src/app/api/auth/[...all]/route.js
create mode 100644 communities/red-right-hand/174bg.net/src/app/login/page.js
create mode 100644 communities/red-right-hand/174bg.net/src/app/secure/page.js
create mode 100644 communities/red-right-hand/174bg.net/src/auth.js
create mode 100644 communities/red-right-hand/174bg.net/src/lib/auth-client.js
create mode 100644 communities/red-right-hand/174bg.net/src/middleware.js
(limited to 'communities/red-right-hand/174bg.net/src')
diff --git a/communities/red-right-hand/174bg.net/src/app/api/auth/[...all]/route.js b/communities/red-right-hand/174bg.net/src/app/api/auth/[...all]/route.js
new file mode 100644
index 0000000..2aabedd
--- /dev/null
+++ b/communities/red-right-hand/174bg.net/src/app/api/auth/[...all]/route.js
@@ -0,0 +1,4 @@
+import { auth } from "@/auth";
+import { toNextJsHandler } from "better-auth/next-js";
+
+export const { POST, GET } = toNextJsHandler(auth);
diff --git a/communities/red-right-hand/174bg.net/src/app/login/page.js b/communities/red-right-hand/174bg.net/src/app/login/page.js
new file mode 100644
index 0000000..b2b1ea8
--- /dev/null
+++ b/communities/red-right-hand/174bg.net/src/app/login/page.js
@@ -0,0 +1,51 @@
+"use client";
+
+import { Suspense, useEffect, useState } from "react";
+import { useSearchParams } from "next/navigation";
+import { authClient } from "@/lib/auth-client";
+
+function LoginRedirect() {
+ const searchParams = useSearchParams();
+ const callbackUrl = searchParams.get("callbackUrl") ?? "/secure";
+ const [error, setError] = useState(null);
+
+ const signIn = () => {
+ authClient.signIn
+ .social({
+ provider: "discord",
+ callbackURL: callbackUrl,
+ })
+ .then(({ error }) => {
+ if (error) setError(error.message ?? "Sign-in failed");
+ });
+ };
+
+ useEffect(() => {
+ signIn();
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+
+ if (error) {
+ return (
+
+
Error: {error}
+
+
+ );
+ }
+
+ return (
+
+
Redirecting to Discord...
+
+
+ );
+}
+
+export default function LoginPage() {
+ return (
+ Loading...
}>
+
+
+ );
+}
diff --git a/communities/red-right-hand/174bg.net/src/app/secure/page.js b/communities/red-right-hand/174bg.net/src/app/secure/page.js
new file mode 100644
index 0000000..27312b7
--- /dev/null
+++ b/communities/red-right-hand/174bg.net/src/app/secure/page.js
@@ -0,0 +1,13 @@
+import { auth } from "@/auth";
+import { headers } from "next/headers";
+
+export default async function SecurePage() {
+ const session = await auth.api.getSession({ headers: await headers() });
+
+ return (
+
+ Secure Area
+ Welcome, {session?.user?.name ?? session?.user?.email}!
+
+ );
+}
diff --git a/communities/red-right-hand/174bg.net/src/auth.js b/communities/red-right-hand/174bg.net/src/auth.js
new file mode 100644
index 0000000..b6162f0
--- /dev/null
+++ b/communities/red-right-hand/174bg.net/src/auth.js
@@ -0,0 +1,20 @@
+import { betterAuth } from "better-auth";
+import { Pool } from "pg";
+
+const postgresUser = process.env.POSTGRES_USER || "postgres";
+const postgresPassword = process.env.POSTGRES_PASSWORD || "postgres";
+const postgresHost = process.env.POSTGRES_HOST || "postgres";
+const postgresPort = process.env.POSTGRES_PORT || 5432;
+const postgresDatabase = process.env.POSTGRES_DB || "postgres";
+
+export const auth = betterAuth({
+ database: new Pool({
+ connectionString: `postgresql://${postgresUser}:${postgresPassword}@${postgresHost}:${postgresPort}/${postgresDatabase}`,
+ }),
+ socialProviders: {
+ discord: {
+ clientId: process.env.DISCORD_CLIENT_ID,
+ clientSecret: process.env.DISCORD_CLIENT_SECRET,
+ },
+ },
+});
diff --git a/communities/red-right-hand/174bg.net/src/lib/auth-client.js b/communities/red-right-hand/174bg.net/src/lib/auth-client.js
new file mode 100644
index 0000000..fd0a091
--- /dev/null
+++ b/communities/red-right-hand/174bg.net/src/lib/auth-client.js
@@ -0,0 +1,6 @@
+import { createAuthClient } from "better-auth/react";
+
+export const authClient = createAuthClient({
+ /** The base URL of the server (optional if you're using the same domain) */
+ baseURL: process.env.BETTER_AUTH_URL,
+});
diff --git a/communities/red-right-hand/174bg.net/src/middleware.js b/communities/red-right-hand/174bg.net/src/middleware.js
new file mode 100644
index 0000000..d9b9a6c
--- /dev/null
+++ b/communities/red-right-hand/174bg.net/src/middleware.js
@@ -0,0 +1,31 @@
+import { NextResponse } from "next/server";
+
+export async function middleware(request) {
+ let session = null;
+
+ try {
+ const res = await fetch(
+ new URL("/api/auth/get-session", request.nextUrl.origin),
+ {
+ headers: {
+ cookie: request.headers.get("cookie") ?? "",
+ },
+ },
+ );
+ session = await res.json();
+ } catch {
+ // If session check fails, treat as unauthenticated
+ }
+
+ if (!session?.user) {
+ const loginUrl = new URL("/login", request.url);
+ loginUrl.searchParams.set("callbackUrl", request.nextUrl.pathname);
+ return NextResponse.redirect(loginUrl);
+ }
+
+ return NextResponse.next();
+}
+
+export const config = {
+ matcher: ["/secure/:path*"],
+};
--
cgit v1.2.3