From b7fc3344e3f3c898388831dbfad084cebdd64bb3 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Mon, 5 Feb 2024 17:07:32 +0000 Subject: Init NextAuth --- .env.sample | 8 ++ .gitignore | 3 + app/api/auth/[...nextauth]/route.tsx | 21 ++++ app/page.tsx | 112 ++------------------- bun.lockb | Bin 144265 -> 149654 bytes components/auth/login.tsx | 17 ++++ components/auth/logout.tsx | 12 +++ lib/config.ts | 20 ++++ package.json | 2 + .../20240205153748_add_users/migration.sql | 56 +++++++++++ prisma/migrations/migration_lock.toml | 3 + prisma/schema.prisma | 45 +++++++++ 12 files changed, 194 insertions(+), 105 deletions(-) create mode 100644 .env.sample create mode 100644 app/api/auth/[...nextauth]/route.tsx create mode 100644 components/auth/login.tsx create mode 100644 components/auth/logout.tsx create mode 100644 lib/config.ts create mode 100644 prisma/migrations/20240205153748_add_users/migration.sql create mode 100644 prisma/migrations/migration_lock.toml diff --git a/.env.sample b/.env.sample new file mode 100644 index 00000000..a48054f0 --- /dev/null +++ b/.env.sample @@ -0,0 +1,8 @@ +DATABASE_URL="file:./dev.db" +NEXTAUTH_URL= +NEXTAUTH_SECRET= + +# Oauth +AUTHENTIK_ID= +AUTHENTIK_SECRET= +AUTHENTIK_ISSUER= diff --git a/.gitignore b/.gitignore index fd3dbb57..5bcde103 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +# The sqlite database +prisma/*dev.db* diff --git a/app/api/auth/[...nextauth]/route.tsx b/app/api/auth/[...nextauth]/route.tsx new file mode 100644 index 00000000..b9decb30 --- /dev/null +++ b/app/api/auth/[...nextauth]/route.tsx @@ -0,0 +1,21 @@ +import NextAuth from "next-auth" +import { PrismaAdapter } from "@next-auth/prisma-adapter" +import AuthentikProvider from "next-auth/providers/authentik"; +import { PrismaClient } from "@prisma/client" +import serverConfig from "@/lib/config"; + +const prisma = new PrismaClient() + +let providers = []; + +if (serverConfig.auth.authentik) { + providers.push(AuthentikProvider(serverConfig.auth.authentik)); +} + +const handler = NextAuth({ + // Configure one or more authentication providers + adapter: PrismaAdapter(prisma), + providers: providers, +}); + +export { handler as GET, handler as POST } diff --git a/app/page.tsx b/app/page.tsx index dc191aa8..2df40508 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,112 +1,14 @@ -import Image from "next/image"; +import { LoginButton } from "../components/auth/login"; +import { LogoutButton } from "../components/auth/logout"; export default function Home() { return (
-
-

- Get started by editing  - app/page.tsx -

-
- - By{" "} - Vercel Logo - -
-
- -
- Next.js Logo -
- -
- -

- Docs{" "} - - -> - -

-

- Find in-depth information about Next.js features and API. -

-
- - -

- Learn{" "} - - -> - -

-

- Learn about Next.js in an interactive course with quizzes! -

-
- - -

- Templates{" "} - - -> - -

-

- Explore starter templates for Next.js. -

-
- - -

- Deploy{" "} - - -> - -

-

- Instantly deploy your Next.js site to a shareable URL with Vercel. -

-
+
+ +
+
+
); diff --git a/bun.lockb b/bun.lockb index 5b2333a2..d92fb9ed 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/components/auth/login.tsx b/components/auth/login.tsx new file mode 100644 index 00000000..4cd55546 --- /dev/null +++ b/components/auth/login.tsx @@ -0,0 +1,17 @@ +"use client"; +import { signIn } from "next-auth/react"; + +export const LoginButton = () => { + return ( + + ); +}; diff --git a/components/auth/logout.tsx b/components/auth/logout.tsx new file mode 100644 index 00000000..87391c84 --- /dev/null +++ b/components/auth/logout.tsx @@ -0,0 +1,12 @@ +"use client"; +import { signOut } from "next-auth/react"; + +export const LogoutButton = () => { + return ( + + ); +}; diff --git a/lib/config.ts b/lib/config.ts new file mode 100644 index 00000000..ef86cb5a --- /dev/null +++ b/lib/config.ts @@ -0,0 +1,20 @@ +function buildAuthentikConfig() { + let {id, secret, issuer} = process.env; + if (!id || !secret || !issuer) { + return undefined; + } + + return { + clientId: id, + clientSecret: secret, + issuer: issuer, + }; +} + +const serverConfig = { + auth: { + authentik: buildAuthentikConfig(), + } +}; + +export default serverConfig; diff --git a/package.json b/package.json index ae7deb01..02b42959 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,9 @@ "lint": "next lint" }, "dependencies": { + "@next-auth/prisma-adapter": "^1.0.7", "next": "14.1.0", + "next-auth": "^4.24.5", "prisma": "^5.9.1", "react": "^18", "react-dom": "^18" diff --git a/prisma/migrations/20240205153748_add_users/migration.sql b/prisma/migrations/20240205153748_add_users/migration.sql new file mode 100644 index 00000000..cbf47073 --- /dev/null +++ b/prisma/migrations/20240205153748_add_users/migration.sql @@ -0,0 +1,56 @@ +-- CreateTable +CREATE TABLE "Account" ( + "id" TEXT NOT NULL PRIMARY KEY, + "userId" TEXT NOT NULL, + "type" TEXT NOT NULL, + "provider" TEXT NOT NULL, + "providerAccountId" TEXT NOT NULL, + "refresh_token" TEXT, + "access_token" TEXT, + "expires_at" INTEGER, + "token_type" TEXT, + "scope" TEXT, + "id_token" TEXT, + "session_state" TEXT, + CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateTable +CREATE TABLE "Session" ( + "id" TEXT NOT NULL PRIMARY KEY, + "sessionToken" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "expires" DATETIME NOT NULL, + CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL PRIMARY KEY, + "name" TEXT, + "email" TEXT, + "emailVerified" DATETIME, + "image" TEXT +); + +-- CreateTable +CREATE TABLE "VerificationToken" ( + "identifier" TEXT NOT NULL, + "token" TEXT NOT NULL, + "expires" DATETIME NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId"); + +-- CreateIndex +CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken"); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token"); + +-- CreateIndex +CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token"); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 00000000..e5e5c470 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "sqlite" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e0020544..0789cab8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -9,3 +9,48 @@ datasource db { provider = "sqlite" url = env("DATABASE_URL") } + +model Account { + id String @id @default(cuid()) + userId String + type String + provider String + providerAccountId String + refresh_token String? + access_token String? + expires_at Int? + token_type String? + scope String? + id_token String? + session_state String? + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + @@unique([provider, providerAccountId]) +} + +model Session { + id String @id @default(cuid()) + sessionToken String @unique + userId String + expires DateTime + user User @relation(fields: [userId], references: [id], onDelete: Cascade) +} + +model User { + id String @id @default(cuid()) + name String? + email String? @unique + emailVerified DateTime? + image String? + accounts Account[] + sessions Session[] +} + +model VerificationToken { + identifier String + token String @unique + expires DateTime + + @@unique([identifier, token]) +} -- cgit v1.2.3-70-g09d2