Skip to content

Commit

Permalink
fix edge compatibility problem
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdev03 committed Nov 16, 2024
1 parent c27a767 commit 9d63bf2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
5 changes: 4 additions & 1 deletion apps/dashboard/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { auth as middleware } from "@/server/auth";
import NextAuth from "next-auth";
import authConfig from "./server/auth/middleware-config";

export const { auth: middleware } = NextAuth(authConfig)

export const config = { matcher: ["/process", "/settings", "/", "/user"] };
31 changes: 3 additions & 28 deletions apps/dashboard/server/auth/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Credentials from "next-auth/providers/credentials";
import connectDB from "@/server/db/mongodb";
import { fetchSettings } from "@/server/db/settings";
import { AuthErrors } from "@/utils/auth-errors";
import middlewareConfig from "./middleware-config";

/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
Expand Down Expand Up @@ -55,10 +56,8 @@ declare module "next-auth/jwt" {
* @see https://next-auth.js.org/configuration/options
*/
export const authConfig = {
pages: {
signIn: "/login",
error: "/login",
},
// Reuse pages, callbacks from middleware auth config
...middlewareConfig,
providers: [
Credentials({
credentials: {
Expand Down Expand Up @@ -147,30 +146,6 @@ export const authConfig = {
},
}),
],
callbacks: {
async jwt({ token, account, user }) {
if (account && account.access_token) token.accessToken = account.access_token;

if (user) {
token.acl = user.acl;
token.oauth2 = user.oauth2;
if (user.id) token.id = user.id;
}
return token;
},
async session({ session, token }: { session: Session; token: JWT }) {
// Send properties to the client, like an access_token and user id from a provider.
session.accessToken = token.accessToken;
session.user.id = token.id;
session.user.acl = token.acl;
session.user.oauth2 = token.oauth2;

return session;
},
async authorized({ auth }) {
return !!auth?.user && !!auth.user.id;
},
},
} satisfies NextAuthConfig;

export class LoginError extends CredentialsSignin {
Expand Down
37 changes: 37 additions & 0 deletions apps/dashboard/server/auth/middleware-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextAuthConfig, Session } from "next-auth";
import { JWT } from "next-auth/jwt";
import Credentials from "next-auth/providers/credentials";

// TODO: Currently next.js middleware only supports edge runtime
// This can be removed once next.js provides a option to switch to node.js runtime
export default {
pages: {
signIn: "/login",
error: "/login",
},
providers: [Credentials],
callbacks: {
async jwt({ token, account, user }) {
if (account && account.access_token) token.accessToken = account.access_token;

if (user) {
token.acl = user.acl;
token.oauth2 = user.oauth2;
if (user.id) token.id = user.id;
}
return token;
},
async session({ session, token }: { session: Session; token: JWT }) {
// Send properties to the client, like an access_token and user id from a provider.
session.accessToken = token.accessToken;
session.user.id = token.id;
session.user.acl = token.acl;
session.user.oauth2 = token.oauth2;

return session;
},
async authorized({ auth }) {
return !!auth?.user && !!auth.user.id;
},
},
} satisfies NextAuthConfig;

0 comments on commit 9d63bf2

Please sign in to comment.