diff --git a/apps/api/src/auth/github.stratergy.ts b/apps/api/src/auth/github.stratergy.ts index 5b0854df..f3dc7518 100644 --- a/apps/api/src/auth/github.stratergy.ts +++ b/apps/api/src/auth/github.stratergy.ts @@ -6,10 +6,13 @@ import { Profile, Strategy } from 'passport-github2' @Injectable() export class GithubStrategy extends PassportStrategy(Strategy, 'github') { constructor(configService: ConfigService) { + const clientID = configService.get('GITHUB_CLIENT_ID') + const clientSecret = configService.get('GITHUB_CLIENT_SECRET') + const callbackURL = configService.get('GITHUB_CALLBACK_URL') super({ - clientID: configService.get('GITHUB_CLIENT_ID'), - clientSecret: configService.get('GITHUB_CLIENT_SECRET'), - callbackURL: configService.get('GITHUB_CALLBACK_URL'), + clientID, + clientSecret, + callbackURL, scope: ['public_profile', 'user:email'] }) } diff --git a/apps/api/src/auth/service/auth.service.ts b/apps/api/src/auth/service/auth.service.ts index 6d1454e1..3e5a6568 100644 --- a/apps/api/src/auth/service/auth.service.ts +++ b/apps/api/src/auth/service/auth.service.ts @@ -29,42 +29,6 @@ export class AuthService { this.logger = new Logger(AuthService.name) } - private async createUserIfNotExists( - email: string, - name?: string, - profilePictureUrl?: string - ) { - let user = await this.findUserByEmail(email) - - // We need to create the user if it doesn't exist yet - if (!user) { - // Create the user - user = await this.prisma.user.create({ - data: { - email, - name, - profilePictureUrl - } - }) - - // Create the user's default workspace - await this.prisma.workspace.create({ - data: { - name: `My Workspace`, - description: 'My default workspace', - isDefault: true, - ownerId: user.id, - lastUpdatedBy: { - connect: { - id: user.id - } - } - } - }) - } - return user - } - async sendOtp(email: string): Promise { if (!email || !email.includes('@')) { this.logger.error(`Invalid email address: ${email}`) @@ -131,9 +95,11 @@ export class AuthService { this.logger.log(`User logged in: ${email}`) + const token = await this.generteToken(user.id) + return { ...user, - token: await this.jwt.signAsync({ id: user.id }) + token } } @@ -149,9 +115,11 @@ export class AuthService { profilePictureUrl ) + const token = await this.generteToken(user.id) + return { ...user, - token: await this.jwt.signAsync({ id: user.id }) + token } } @@ -172,6 +140,54 @@ export class AuthService { } } + private async createUserIfNotExists( + email: string, + name?: string, + profilePictureUrl?: string + ) { + const user = await this.findUserByEmail(email) + + // We need to create the user if it doesn't exist yet + if (!user) { + await this.createUser(email, name, profilePictureUrl) + } + return user + } + + private async createUser( + email: string, + name: string, + profilePictureUrl: string + ) { + // Create the user + const user = await this.prisma.user.create({ + data: { + email, + name, + profilePictureUrl + } + }) + + // Create the user's default workspace + await this.prisma.workspace.create({ + data: { + name: `My Workspace`, + description: 'My default workspace', + isDefault: true, + ownerId: user.id, + lastUpdatedBy: { + connect: { + id: user.id + } + } + } + }) + } + + private async generteToken(id: string) { + return await this.jwt.signAsync({ id }) + } + private async findUserByEmail(email: string) { return await this.prisma.user.findUnique({ where: {