Skip to content

Commit

Permalink
Feat: Added custom fields to jwt and session in next-auth.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
joywin2003 committed Sep 10, 2024
1 parent 5aaad2d commit 3301ae4
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
Binary file modified prisma/dev.db
Binary file not shown.
Binary file modified prisma/dev.db-journal
Binary file not shown.
92 changes: 92 additions & 0 deletions prisma/migrations/20240909174414_test/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Warnings:
- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint.
*/
-- 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,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
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,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Referral" (
"id" TEXT NOT NULL PRIMARY KEY,
"code" TEXT NOT NULL,
"createdby_id" TEXT NOT NULL,
"usedby_id" TEXT,
"isUsed" BOOLEAN NOT NULL DEFAULT false,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Referral_createdby_id_fkey" FOREIGN KEY ("createdby_id") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "Referral_usedby_id_fkey" FOREIGN KEY ("usedby_id") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Form" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"usn" TEXT NOT NULL,
"email" TEXT NOT NULL,
"contact" TEXT,
"photo_url" TEXT NOT NULL,
"referral_id" TEXT,
"created_by_id" TEXT NOT NULL,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"college_id_card" TEXT NOT NULL,
"college" TEXT,
CONSTRAINT "Form_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "Form_referral_id_fkey" FOREIGN KEY ("referral_id") REFERENCES "Referral" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_User" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT,
"email" TEXT,
"emailVerified" DATETIME,
"role" TEXT NOT NULL DEFAULT 'PARTICIPANT',
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_User" ("createdAt", "email", "id", "name", "updatedAt") SELECT "createdAt", "email", "id", "name", "updatedAt" FROM "User";
DROP TABLE "User";
ALTER TABLE "new_User" RENAME TO "User";
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

-- 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 "Referral_code_key" ON "Referral"("code");
6 changes: 6 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ datasource db {
url = "file:./dev.db"
}

// enum Role {
// ADMIN
// USER
// MODERATOR
// }

model User {
id String @id @default(cuid())
name String?
Expand Down
6 changes: 5 additions & 1 deletion src/app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import NextAuth from "next-auth"
import NextAuth, { DefaultSession } from "next-auth"
import { type NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import Google, { GoogleProfile } from "next-auth/providers/google";
import prisma from "@/server/db";


const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID || "";
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET || "";




export const authOptions:NextAuthOptions = {
session:{
strategy: "jwt",
Expand Down
29 changes: 29 additions & 0 deletions src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import "next-auth";

declare module "next-auth" {
interface Session {
user: {
id?: string;
role: string;
name: string;
email: string;
} & DefaultSession["session"];
}

interface User {
id?: string;
role: string;
name: string;
email: string;
}
}


declare module "next-auth/jwt" {
interface JWT {
id?: string;
role: string;
name: string;
email: string;
}
}

0 comments on commit 3301ae4

Please sign in to comment.