Skip to content

Scheduling infrastructure for absolutely everyone.

fix: add belongsToActiveTeam (#7890)

Udit Takkarfa17139e8f74
diff --git a/packages/features/auth/lib/next-auth-options.ts b/packages/features/auth/lib/next-auth-options.ts
index dd1105aba78..7b72abd7e1c 100644
--- a/packages/features/auth/lib/next-auth-options.ts
+++ b/packages/features/auth/lib/next-auth-options.ts
@@ -1,4 +1,4 @@
-import type { UserPermissionRole } from "@prisma/client";
+import type { UserPermissionRole, Membership, Team } from "@prisma/client";
import { IdentityProvider } from "@prisma/client";
import { readFileSync } from "fs";
import Handlebars from "handlebars";
@@ -61,6 +61,20 @@ const signJwt = async (payload: { email: string }) => {
const loginWithTotp = async (user: { email: string }) =>
`/auth/login?totp=${await signJwt({ email: user.email })}`;
+type UserTeams = {
+ teams: (Membership & {
+ team: Team;
+ })[];
+};
+
+const checkIfUserBelongsToActiveTeam = <T extends UserTeams>(user: T): boolean =>
+ user.teams.filter((m: { team: { metadata: unknown } }) => {
+ if (!IS_TEAM_BILLING_ENABLED) return true;
+ const metadata = teamMetadataSchema.safeParse(m.team.metadata);
+ if (metadata.success && metadata.data?.subscriptionId) return true;
+ return false;
+ }).length > 0;
+
const providers: Provider[] = [
CredentialsProvider({
id: "credentials",
@@ -157,13 +171,7 @@ const providers: Provider[] = [
}
}
// Check if the user you are logging into has any active teams
- const hasActiveTeams =
- user.teams.filter((m: { team: { metadata: unknown } }) => {
- if (!IS_TEAM_BILLING_ENABLED) return true;
- const metadata = teamMetadataSchema.safeParse(m.team.metadata);
- if (metadata.success && metadata.data?.subscriptionId) return true;
- return false;
- }).length > 0;
+ const hasActiveTeams = checkIfUserBelongsToActiveTeam(user);
// authentication success- but does it meet the minimum password requirements?
const validateRole = (role: UserPermissionRole) => {
@@ -391,6 +399,11 @@ export const AUTH_OPTIONS: AuthOptions = {
name: true,
email: true,
role: true,
+ teams: {
+ include: {
+ team: true,
+ },
+ },
},
});
@@ -398,9 +411,14 @@ export const AUTH_OPTIONS: AuthOptions = {
return token;
}
+ // Check if the existingUser has any active teams
+ const belongsToActiveTeam = checkIfUserBelongsToActiveTeam(existingUser);
+ const { teams, ...existingUserWithoutTeamsField } = existingUser;
+
return {
- ...existingUser,
+ ...existingUserWithoutTeamsField,
...token,
+ belongsToActiveTeam,
};
};
if (!user) {