Skip to content

Commit ba1fc34

Browse files
authored
Merge pull request #4 from eaudeweb/permissions_in_next-auth_jwe
permissions in next auth jwe
2 parents ab39882 + 3e44087 commit ba1fc34

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

packages/next-drupal/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"html-react-parser": "^5.1.10",
3232
"isomorphic-dompurify": "^2.7.0",
3333
"jsona": "^1.12.1",
34+
"jwt-decode": "^4.0.0",
3435
"lodash": "^4.17.21",
3536
"next": "^13.5.6",
3637
"next-auth": "4.24.7",
@@ -41,6 +42,7 @@
4142
},
4243
"devDependencies": {
4344
"@csstools/postcss-global-data": "^2.1.1",
45+
"@edw/config-release-it": "workspace:*",
4446
"@release-it/conventional-changelog": "^8.0.1",
4547
"@testing-library/dom": "^10.0.0",
4648
"@testing-library/jest-dom": "^6.4.2",
@@ -63,7 +65,6 @@
6365
"release-it": "^17.2.1",
6466
"sass": "^1.75.0",
6567
"tsconfig": "workspace:*",
66-
"@edw/config-release-it": "workspace:*",
6768
"typescript": "5.4.5"
6869
}
6970
}

packages/next-drupal/src/@types/next-auth.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Session as NextAuthSession, User as NextAuthUser } from 'next-auth'
1+
import type {
2+
Session as NextAuthSession,
3+
User as NextAuthUser,
4+
} from 'next-auth'
25

36
declare module 'next-auth' {
47
interface User extends NextAuthUser {
@@ -14,6 +17,12 @@ declare module 'next-auth' {
1417
message: string
1518
statusCode: number
1619
}
20+
user?: {
21+
email?: null | string
22+
image?: null | string
23+
name?: null | string
24+
permissions?: Record<string, boolean>
25+
}
1726
}
1827
}
1928

packages/next-drupal/src/lib/auth/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable turbo/no-undeclared-env-vars */
22
import type { NextAuthOptions, Session } from 'next-auth'
33

4+
import { jwtDecode } from 'jwt-decode'
45
import NextAuth from 'next-auth'
56
import { JWT } from 'next-auth/jwt'
67
import {
@@ -95,7 +96,6 @@ async function refreshAccessToken(token: JWT) {
9596
const data = await response.json()
9697

9798
if (!response.ok) {
98-
console.log(response)
9999
return {
100100
error: {
101101
message: response.statusText,
@@ -116,21 +116,32 @@ async function refreshAccessToken(token: JWT) {
116116
export const authOptions = {
117117
callbacks: {
118118
async jwt({ account, token, user }) {
119+
let data: any = {}
120+
try {
121+
data = jwtDecode(user?.access_token || token?.access_token || '')
122+
} catch (e) {
123+
console.log(e)
124+
}
119125
// Initial sign in
120126
if (account && user) {
121127
return {
122128
access_token: user.access_token,
123-
email: user.email,
129+
email: data.mail || user.email,
124130
expires_in: Date.now() + (user.expires_in || 0) * 1000,
125131
// expires_in: Date.now() + 5 * 1000,
126132
name: user.name,
133+
permissions: data.permissions,
127134
refresh_token: user.refresh_token,
128135
}
129136
}
130137

131138
// Return previous token if the access token has not expired yet
132139
if (Date.now() < (token.expires_in || 0)) {
133-
return token
140+
return {
141+
...token,
142+
email: data.mail,
143+
permissions: data.permissions,
144+
}
134145
}
135146

136147
// Access token has expired, try to update it
@@ -144,9 +155,11 @@ export const authOptions = {
144155
error: token.error,
145156
}
146157
}
158+
147159
session.user = {
148160
email: token.email,
149161
name: token.name,
162+
permissions: (token.permissions || {}) as Record<string, boolean>,
150163
}
151164
session.access_token = token.access_token
152165
session.access_token_expires = token.expires_in

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)