fix: add belongsToActiveTeam (#7890)
* fix: add belongsToActiveTeam Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in> * fix: create a common function Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in> * fix: add suggestion Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in> * chore: use deconstructing Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in> * chore Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in> --------- Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in>pull/7917/head
parent
1cdc97dd37
commit
fa17139e8f
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue