Skip to content

Commit

Permalink
refactor: service functions as suggested
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshPatel5940 authored and rajdip-b committed Jan 28, 2024
1 parent 7f189dd commit c1ae78a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
9 changes: 6 additions & 3 deletions apps/api/src/auth/github.stratergy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('GITHUB_CLIENT_ID')
const clientSecret = configService.get<string>('GITHUB_CLIENT_SECRET')
const callbackURL = configService.get<string>('GITHUB_CALLBACK_URL')
super({
clientID: configService.get<string>('GITHUB_CLIENT_ID'),
clientSecret: configService.get<string>('GITHUB_CLIENT_SECRET'),
callbackURL: configService.get<string>('GITHUB_CALLBACK_URL'),
clientID,
clientSecret,
callbackURL,
scope: ['public_profile', 'user:email']
})
}
Expand Down
92 changes: 54 additions & 38 deletions apps/api/src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
if (!email || !email.includes('@')) {
this.logger.error(`Invalid email address: ${email}`)
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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: {
Expand Down

0 comments on commit c1ae78a

Please sign in to comment.