Skip to content

Commit

Permalink
feat: support role
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy committed Feb 16, 2024
1 parent f8bf3fc commit 3731665
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 44 deletions.
1 change: 1 addition & 0 deletions e2e/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default async function globalSetup(config: FullConfig) {
name: NAME,
image: IMAGE,
email: EMAIL,
role: "user",
sessions: {
create: {
expires: new Date(
Expand Down
49 changes: 28 additions & 21 deletions prisma/ERD.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ erDiagram
"accounts" {
String id "🗝️"
String user_id
String type
String provider
String provider_account_id
String user_id
String type
String provider
String provider_account_id
String refresh_token "❓"
String access_token "❓"
Int expires_at "❓"
String token_type "❓"
String scope "❓"
String id_token "❓"
String session_state "❓"
DateTime created_at
DateTime updated_at
}
"sessions" {
String id "🗝️"
String session_token
String userId
DateTime expires
String session_token
String user_id
DateTime expires
DateTime created_at
DateTime updated_at
}
"users" {
String id "🗝️"
Expand All @@ -32,26 +36,29 @@ erDiagram
DateTime email_verified "❓"
String image "❓"
String website "❓"
DateTime created_at "❓"
DateTime updated_at "❓"
DateTime created_at
DateTime updated_at
String role "❓"
}
"verificationtokens" {
String identifier
String token
DateTime expires
String identifier
String token
DateTime expires
DateTime created_at
DateTime updated_at
}
"items" {
String id "🗝️"
DateTime createdAt
DateTime updatedAt
String content
String userId
String content
String userId
DateTime created_at
DateTime updated_at
}
"accounts" o|--|| "users" : "user"
"sessions" o|--|| "users" : "user"
"users" o{--}o "accounts" : "accounts"
Expand Down
41 changes: 25 additions & 16 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@ generator client {
}

generator erd {
provider = "prisma-erd-generator"
theme = "forest"
output = "ERD.md"
provider = "prisma-erd-generator"
theme = "forest"
output = "ERD.md"
includeRelationFromFields = true
}

// https://next-auth.js.org/v3/adapters/prisma#setup
model Account {
id String @id @default(cuid())
userId String @map("user_id")
id String @id @default(cuid())
userId String @map("user_id")
type String
provider String
providerAccountId String @map("provider_account_id")
refresh_token String? @db.Text
access_token String? @db.Text
providerAccountId String @map("provider_account_id")
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@unique([provider, providerAccountId])
@@map("accounts")
Expand All @@ -40,9 +42,11 @@ model Account {
model Session {
id String @id @default(cuid())
sessionToken String @unique @map("session_token")
userId String
userId String @map("user_id")
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@map("sessions")
}
Expand All @@ -54,10 +58,12 @@ model User {
emailVerified DateTime? @map("email_verified")
image String?
website String?
created_at DateTime?
updated_at DateTime?
accounts Account[]
sessions Session[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
// https://authjs.dev/guides/basics/role-based-access-control
role String?
items Item[]
@@map("users")
Expand All @@ -67,18 +73,21 @@ model VerificationToken {
identifier String
token String @unique
expires DateTime
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@unique([identifier, token])
@@map("verificationtokens")
}

// example model
model Item {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
content String
content String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@map("items")
}
17 changes: 11 additions & 6 deletions src/app/_clients/nextAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export const options: NextAuthOptions = {
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID || "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
profile(profile) {
return {
id: profile.sub,
name: profile.name,
email: profile.email,
image: profile.picture,
role: profile.role ?? "user",
};
},
}),
],
callbacks: {
Expand All @@ -22,14 +31,10 @@ export const options: NextAuthOptions = {
session: async ({ session, token, user, trigger, newSession }) => {
if (session?.user) {
session.user.id = user.id;
session.user.role = user.role;
}

return {
...session,
user: {
...session.user,
},
};
return session;
},
},
};
7 changes: 6 additions & 1 deletion src/app/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ declare global {
}

declare module "next-auth" {
interface User {
id: string;
role: "user" | "admin";
}

interface Session {
user?: DefaultUser & { id: string };
user: User;
}
}

0 comments on commit 3731665

Please sign in to comment.