Skip to content

Commit

Permalink
fix: org guard did not have user org profile + missing db read in sel…
Browse files Browse the repository at this point in the history
…ectedCalendarRepo (calcom#14303)

* fix: user did not contain profile with next-auth-guard

* fix: missing dbread instance
  • Loading branch information
ThyMinimalDev authored Apr 3, 2024
1 parent cdf0593 commit c3b1de4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions apps/api/v2/src/lib/passport/strategies/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { UserWithProfile } from "@/modules/users/users.repository";

export class BaseStrategy {
success!: (user: unknown) => void;
error!: (error: Error) => void;
}

export class NextAuthPassportStrategy {
success!: (user: UserWithProfile) => void;
error!: (error: Error) => void;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseStrategy } from "@/lib/passport/strategies/types";
import { NextAuthPassportStrategy } from "@/lib/passport/strategies/types";
import { UsersRepository } from "@/modules/users/users.repository";
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
Expand All @@ -7,7 +7,7 @@ import type { Request } from "express";
import { getToken } from "next-auth/jwt";

@Injectable()
export class NextAuthStrategy extends PassportStrategy(BaseStrategy, "next-auth") {
export class NextAuthStrategy extends PassportStrategy(NextAuthPassportStrategy, "next-auth") {
constructor(private readonly userRepository: UsersRepository, private readonly config: ConfigService) {
super();
}
Expand All @@ -25,7 +25,7 @@ export class NextAuthStrategy extends PassportStrategy(BaseStrategy, "next-auth"
throw new UnauthorizedException("Email not found in the authentication token.");
}

const user = await this.userRepository.findByEmail(payload.email);
const user = await this.userRepository.findByEmailWithProfile(payload.email);
if (!user) {
throw new UnauthorizedException("User associated with the authentication token email not found.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
import { Injectable } from "@nestjs/common";

@Injectable()
export class SelectedCalendarsRepository {
constructor(private readonly dbWrite: PrismaWriteService) {}
constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {}

createSelectedCalendar(externalId: string, credentialId: number, userId: number, integration: string) {
return this.dbWrite.prisma.selectedCalendar.create({
Expand Down
11 changes: 11 additions & 0 deletions apps/api/v2/src/modules/users/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ export class UsersRepository {
});
}

async findByEmailWithProfile(email: string) {
return this.dbRead.prisma.user.findUnique({
where: {
email,
},
include: {
movedToProfile: true,
},
});
}

async findByUsername(username: string) {
return this.dbRead.prisma.user.findFirst({
where: {
Expand Down

0 comments on commit c3b1de4

Please sign in to comment.