Skip to content

Commit d5e5d9e

Browse files
committed
chore(dev): Improve edge compatibility and some other small changes
- Split configs for edge and node specific configs - Send nodemailer emails using payload email adapter - Add payload email adapter - Add discord provider - Reject users with expired session in authorized callback - Remove access_token from account - Add session createdAtField depending on authjs session strategy
1 parent 4b66142 commit d5e5d9e

File tree

15 files changed

+175
-90
lines changed

15 files changed

+175
-90
lines changed

packages/dev/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ AUTH_KEYCLOAK_ISSUER=http://localhost:8080/realms/myrealm
1313
AUTH_KEYCLOAK_ID=client-id
1414
AUTH_KEYCLOAK_SECRET=
1515

16-
EMAIL_SERVER=
17-
EMAIL_FROM=
16+
AUTH_DISCORD_ID=
17+
AUTH_DISCORD_SECRET=

packages/dev/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"private": true,
66
"type": "module",
77
"scripts": {
8+
"dev": "next dev -p 5000",
89
"dev:turbo": "next dev -p 5000 --turbopack",
910
"payload": "payload",
1011
"generate:types": "payload generate:types",

packages/dev/project.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
"sourceRoot": "packages/dev/src",
44
"projectType": "application",
55
"targets": {
6-
"dev": {
7-
"options": {
8-
"port": 5000
9-
}
10-
},
116
"start": {
127
"options": {
138
"port": 5000

packages/dev/src/auth.config.ts renamed to packages/dev/src/auth/base.config.ts

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ import jwt from "jsonwebtoken";
22
import type { NextAuthConfig } from "next-auth";
33
// eslint-disable-next-line @typescript-eslint/no-unused-vars
44
import type { JWT } from "next-auth/jwt";
5-
import github from "next-auth/providers/github";
6-
import keycloak from "next-auth/providers/keycloak";
7-
import nodemailer from "next-auth/providers/nodemailer";
85
import type { PayloadAuthjsUser } from "payload-authjs";
9-
import type { User as PayloadUser } from "./payload-types";
6+
import type { User as PayloadUser } from "../payload-types";
107

118
declare module "next-auth" {
129
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
@@ -27,68 +24,16 @@ declare module "next-auth/jwt" {
2724
> {}
2825
}
2926

27+
export const SESSION_STRATEGY: NonNullable<NonNullable<NextAuthConfig["session"]>["strategy"]> =
28+
"jwt";
29+
3030
export const authConfig: NextAuthConfig = {
3131
theme: { logo: "https://authjs.dev/img/logo-sm.png" },
32-
providers: [
33-
github({
34-
allowDangerousEmailAccountLinking: true,
35-
/**
36-
* Add additional fields to the user on first sign in
37-
*/
38-
profile(profile) {
39-
return {
40-
// Default fields (@see https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/github.ts#L176)
41-
id: profile.id.toString(),
42-
name: profile.name ?? profile.login,
43-
email: profile.email,
44-
image: profile.avatar_url,
45-
// Custom fields
46-
additionalUserDatabaseField: `Create by github provider profile callback at ${new Date().toISOString()}`,
47-
};
48-
},
49-
account(tokens) {
50-
return {
51-
...tokens,
52-
additionalAccountDatabaseField: `Create by github provider profile callback at ${new Date().toISOString()}`,
53-
};
54-
},
55-
}),
56-
keycloak({
57-
allowDangerousEmailAccountLinking: true,
58-
/**
59-
* Add additional fields to the user on first sign in
60-
*/
61-
profile(profile) {
62-
return {
63-
// Default fields
64-
id: profile.sub,
65-
name: profile.name,
66-
email: profile.email,
67-
image: profile.picture,
68-
// Custom fields
69-
locale: profile.locale,
70-
additionalUserDatabaseField: `Create by keycloak provider profile callback at ${new Date().toISOString()}`,
71-
};
72-
},
73-
account(tokens) {
74-
return {
75-
...tokens,
76-
additionalAccountDatabaseField: `Create by keycloak provider profile callback at ${new Date().toISOString()}`,
77-
};
78-
},
79-
}),
80-
nodemailer({
81-
server: process.env.EMAIL_SERVER,
82-
from: process.env.EMAIL_FROM,
83-
sendVerificationRequest: ({ url }) => {
84-
console.log("nodemailer:", url);
85-
},
86-
}),
87-
],
32+
providers: [],
8833
session: {
89-
strategy: "jwt",
90-
//maxAge: 60 * 2 + 30, // 2.5 minutes
91-
//updateAge: 60, // 1 minute
34+
strategy: SESSION_STRATEGY,
35+
maxAge: 60 * 15, // 15 minutes
36+
updateAge: 60, // 1 minute
9237
},
9338
callbacks: {
9439
jwt: ({ token, user, account, trigger }) => {
@@ -164,8 +109,17 @@ export const authConfig: NextAuthConfig = {
164109
return session;
165110
},
166111
authorized: ({ auth }) => {
167-
// Logged in users are authenticated, otherwise redirect to login page
168-
return !!auth;
112+
// User is authenticated
113+
if (!auth?.user) {
114+
return false;
115+
}
116+
117+
// Session in not expired
118+
if (new Date() >= new Date(auth.expires)) {
119+
return false;
120+
}
121+
122+
return true;
169123
},
170124
},
171125
};

packages/dev/src/auth/edge.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { NextAuthConfig } from "next-auth";
2+
import { authConfig } from "./base.config";
3+
import { githubProvider } from "./providers/github";
4+
import { keycloakProvider } from "./providers/keycloak";
5+
import { discordProvider } from "./providers/discord";
6+
7+
/**
8+
* Edge compatible auth config
9+
*
10+
* @see https://authjs.dev/guides/edge-compatibility
11+
*/
12+
export const edgeAuthConfig: NextAuthConfig = {
13+
...authConfig,
14+
providers: [githubProvider, keycloakProvider, discordProvider],
15+
};

packages/dev/src/auth.ts renamed to packages/dev/src/auth/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import payloadConfig from "@payload-config";
22
import NextAuth from "next-auth";
3+
import { getPayload } from "payload";
34
import { withPayload } from "payload-authjs";
4-
import { authConfig } from "./auth.config";
5+
import { nodeAuthConfig } from "./node.config";
56

67
export const { handlers, signIn, signOut, auth } = NextAuth(
7-
withPayload(authConfig, {
8-
payloadConfig,
8+
withPayload(nodeAuthConfig, {
9+
payload: getPayload({ config: payloadConfig }),
910
events: {
1011
/**
1112
* Update user on every sign in

packages/dev/src/auth/node.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { NextAuthConfig } from "next-auth";
2+
import { authConfig } from "./base.config";
3+
import { discordProvider } from "./providers/discord";
4+
import { githubProvider } from "./providers/github";
5+
import { keycloakProvider } from "./providers/keycloak";
6+
import { nodemailerProvider } from "./providers/nodemailer";
7+
8+
export const nodeAuthConfig: NextAuthConfig = {
9+
...authConfig,
10+
providers: [githubProvider, keycloakProvider, discordProvider, nodemailerProvider],
11+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import discord from "next-auth/providers/discord";
2+
3+
export const discordProvider = discord({
4+
allowDangerousEmailAccountLinking: true,
5+
/**
6+
* Add additional fields to the user on first sign in
7+
*/
8+
async profile(profile, tokens) {
9+
return {
10+
// Default fields from discord provider
11+
...(await discord({}).profile!(profile, tokens)),
12+
// Custom fields
13+
additionalUserDatabaseField: `Create by discord provider profile callback at ${new Date().toISOString()}`,
14+
};
15+
},
16+
account(tokens) {
17+
return {
18+
...tokens,
19+
additionalAccountDatabaseField: `Create by discord provider profile callback at ${new Date().toISOString()}`,
20+
};
21+
},
22+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import github from "next-auth/providers/github";
2+
3+
export const githubProvider = github({
4+
allowDangerousEmailAccountLinking: true,
5+
/**
6+
* Add additional fields to the user on first sign in
7+
*/
8+
profile(profile) {
9+
return {
10+
// Default fields (@see https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/github.ts#L176)
11+
id: profile.id.toString(),
12+
name: profile.name ?? profile.login,
13+
email: profile.email,
14+
image: profile.avatar_url,
15+
// Custom fields
16+
additionalUserDatabaseField: `Create by github provider profile callback at ${new Date().toISOString()}`,
17+
};
18+
},
19+
account(tokens) {
20+
return {
21+
...tokens,
22+
additionalAccountDatabaseField: `Create by github provider profile callback at ${new Date().toISOString()}`,
23+
};
24+
},
25+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import keycloak from "next-auth/providers/keycloak";
2+
3+
export const keycloakProvider = keycloak({
4+
allowDangerousEmailAccountLinking: true,
5+
/**
6+
* Add additional fields to the user on first sign in
7+
*/
8+
profile(profile) {
9+
return {
10+
// Default fields
11+
id: profile.sub,
12+
name: profile.name,
13+
email: profile.email,
14+
image: profile.picture,
15+
// Custom fields
16+
locale: profile.locale,
17+
additionalUserDatabaseField: `Create by keycloak provider profile callback at ${new Date().toISOString()}`,
18+
};
19+
},
20+
account(tokens) {
21+
return {
22+
...tokens,
23+
additionalAccountDatabaseField: `Create by keycloak provider profile callback at ${new Date().toISOString()}`,
24+
};
25+
},
26+
});

0 commit comments

Comments
 (0)