-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.service.ts
53 lines (47 loc) · 1.43 KB
/
auth.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { AuthSchProfile } from '@kir-dev/passport-authsch'
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'
@Injectable()
export class AuthService {
private readonly logger = new Logger(AuthService.name)
constructor(
private readonly usersService: UsersService,
private readonly jwtService: JwtService,
) {}
async findOrCreateUser(oAuthUser: AuthSchProfile): Promise<UserEntity> {
try {
const user = await this.usersService.findByAuthSchId(oAuthUser.authSchId)
if (user) {
return user
}
const newUser = await this.usersService.create({
authSchId: oAuthUser.authSchId,
firstName: oAuthUser.firstName,
fullName: oAuthUser.fullName,
email: oAuthUser.email,
})
this.logger.log(`User #${newUser.id} created`)
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 } {
return {
jwt: this.jwtService.sign(user, {
secret: process.env.JWT_SECRET,
expiresIn: '2 days',
}),
}
}
}