Skip to content

Commit

Permalink
allow null as email (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschonti authored May 10, 2024
1 parent c922381 commit 014a7e4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
2 changes: 2 additions & 0 deletions prisma/migrations/20240510184646_nullable_email/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ALTER COLUMN "email" DROP NOT NULL;
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ model User {
authSchId String @unique
fullName String
firstName String
email String @unique
email String? @unique
isAdmin Boolean @default(false)
subscribedSubjects Subject[]
ownedGroups Group[]
Expand Down
42 changes: 28 additions & 14 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Injectable, Logger } from '@nestjs/common'
import {
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { User } from '@prisma/client'
import { UserEntity } from 'src/users/dto/UserEntity.dto'
import { UsersService } from '../users/users.service'
import { OAuthUser } from './oauthuser'

Expand All @@ -13,22 +18,31 @@ export class AuthService {
private readonly jwtService: JwtService,
) {}

async findOrCreateUser(oAuthUser: OAuthUser): Promise<User> {
const user = await this.usersService.findByAuthSchId(oAuthUser.internal_id)
if (user) {
return user
}
async findOrCreateUser(oAuthUser: OAuthUser): Promise<UserEntity> {
try {
const user = await this.usersService.findByAuthSchId(
oAuthUser.internal_id,
)
if (user) {
return user
}

const newUser = await this.usersService.create({
authSchId: oAuthUser.internal_id,
firstName: oAuthUser.givenName,
fullName: oAuthUser.displayName,
email: oAuthUser.mail,
})
const newUser = await this.usersService.create({
authSchId: oAuthUser.internal_id,
firstName: oAuthUser.givenName,
fullName: oAuthUser.displayName,
email: oAuthUser.mail,
})

this.logger.log(`User #${newUser.id} created`)
this.logger.log(`User #${newUser.id} created`)

return newUser
return newUser
} catch (e) {
this.logger.error('Unexpected error during user creation', e)
throw new InternalServerErrorException(
'Unexpected error during user creation. Please contact Kir-Dev.',
)
}
}

login(user: User): { jwt: string } {
Expand Down
4 changes: 2 additions & 2 deletions src/auth/authsch.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpService } from '@nestjs/axios'
import { Injectable, Logger } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { User } from '@prisma/client'
import { Strategy } from 'passport-oauth2'
import { firstValueFrom } from 'rxjs'
import { UserEntity } from 'src/users/dto/UserEntity.dto'
import { AuthService } from './auth.service'
import { OAuthUser } from './oauthuser'

Expand All @@ -26,7 +26,7 @@ export class AuthschStrategy extends PassportStrategy(Strategy, 'authsch') {
})
}

async validate(accessToken: string): Promise<User> {
async validate(accessToken: string): Promise<UserEntity> {
const { data: oAuthUser } = await firstValueFrom(
this.httpService.get<OAuthUser>(
`${AUTHSCH_HOST}/api/profile?access_token=${accessToken}`,
Expand Down
5 changes: 3 additions & 2 deletions src/auth/oauthuser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsEmail, IsNotEmpty, IsUUID } from 'class-validator'
import { IsEmail, IsNotEmpty, IsOptional, IsUUID } from 'class-validator'

export class OAuthUser {
@IsNotEmpty()
Expand All @@ -11,7 +11,8 @@ export class OAuthUser {
internal_id: string

@IsEmail()
mail: string
@IsOptional()
mail?: string

constructor(partial: Partial<OAuthUser>) {
Object.assign(this, partial)
Expand Down
1 change: 1 addition & 0 deletions src/mailing/mailing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export class MailingService {
this.sendMail(
[...request.supporters, request.initializer]
.filter((u) => {
if (!u.email) return false
const ability = this.caslFactory.createForConsultationRead(u)
return ability.can(
Permissions.Read,
Expand Down
3 changes: 2 additions & 1 deletion src/users/dto/UserEntity.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export class UserEntity {
@IsNotEmpty()
fullName: string

@IsOptional()
@IsEmail()
@ApiProperty({ example: '[email protected]' })
email: string
email?: string

@IsBoolean()
@IsOptional()
Expand Down

0 comments on commit 014a7e4

Please sign in to comment.