2021-07-26 16:02:47 +00:00
|
|
|
import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
|
2021-07-11 05:39:55 +00:00
|
|
|
import { UsersService } from './users.service';
|
2021-07-19 05:42:16 +00:00
|
|
|
import { OrganizationsService } from './organizations.service';
|
2021-07-08 07:39:07 +00:00
|
|
|
import { JwtService } from '@nestjs/jwt';
|
2021-07-11 05:13:51 +00:00
|
|
|
import { User } from '../entities/user.entity';
|
2021-07-19 05:42:16 +00:00
|
|
|
import { OrganizationUsersService } from './organization_users.service';
|
2021-07-26 14:30:12 +00:00
|
|
|
import { EmailService } from './email.service';
|
2021-07-08 07:39:07 +00:00
|
|
|
const bcrypt = require('bcrypt');
|
2021-09-21 13:48:28 +00:00
|
|
|
const uuid = require('uuid');
|
2021-07-08 07:39:07 +00:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class AuthService {
|
|
|
|
|
constructor(
|
|
|
|
|
private usersService: UsersService,
|
2021-07-19 05:42:16 +00:00
|
|
|
private jwtService: JwtService,
|
|
|
|
|
private organizationsService: OrganizationsService,
|
2021-07-26 14:30:12 +00:00
|
|
|
private organizationUsersService: OrganizationUsersService,
|
2021-09-21 13:48:28 +00:00
|
|
|
private emailService: EmailService
|
|
|
|
|
) {}
|
2021-07-08 07:39:07 +00:00
|
|
|
|
|
|
|
|
async validateUser(email: string, password: string): Promise<User> {
|
|
|
|
|
const user = await this.usersService.findByEmail(email);
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (!user) return null;
|
2021-07-10 07:01:13 +00:00
|
|
|
|
|
|
|
|
const isVerified = await bcrypt.compare(password, user.password);
|
2021-07-08 07:39:07 +00:00
|
|
|
|
|
|
|
|
return isVerified ? user : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async login(params: any) {
|
|
|
|
|
const user = await this.validateUser(params.email, params.password);
|
|
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
|
const payload = { username: user.id, sub: user.email };
|
|
|
|
|
|
|
|
|
|
return {
|
2021-07-10 13:54:32 +00:00
|
|
|
auth_token: this.jwtService.sign(payload),
|
2021-08-09 12:10:44 +00:00
|
|
|
email: user.email,
|
|
|
|
|
first_name: user.firstName,
|
2021-09-10 09:40:23 +00:00
|
|
|
last_name: user.lastName,
|
2021-09-21 13:48:28 +00:00
|
|
|
role: user.role,
|
2021-07-08 07:39:07 +00:00
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw new UnauthorizedException('Invalid credentials');
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-19 05:42:16 +00:00
|
|
|
|
|
|
|
|
async signup(params: any) {
|
|
|
|
|
// Check if the installation allows user signups
|
2021-09-21 13:48:28 +00:00
|
|
|
if (process.env.DISABLE_SIGNUPS === 'true') {
|
|
|
|
|
return {};
|
2021-07-19 05:42:16 +00:00
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2021-07-19 05:42:16 +00:00
|
|
|
const { email } = params;
|
|
|
|
|
const organization = await this.organizationsService.create('Untitled organization');
|
2021-07-19 09:36:34 +00:00
|
|
|
const user = await this.usersService.create({ email }, organization);
|
2021-09-21 13:48:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2021-07-19 05:42:16 +00:00
|
|
|
const organizationUser = await this.organizationUsersService.create(user, organization, 'admin');
|
|
|
|
|
|
2021-07-26 14:30:12 +00:00
|
|
|
this.emailService.sendWelcomeEmail(user.email, user.firstName, user.invitationToken);
|
|
|
|
|
|
2021-07-19 05:42:16 +00:00
|
|
|
return user;
|
2021-07-26 16:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async forgotPassword(email: string) {
|
|
|
|
|
const user = await this.usersService.findByEmail(email);
|
|
|
|
|
const forgotPasswordToken = uuid.v4();
|
2021-07-26 17:14:14 +00:00
|
|
|
this.usersService.update(user.id, { forgotPasswordToken });
|
2021-07-26 16:02:47 +00:00
|
|
|
this.emailService.sendPasswordResetEmail(email, forgotPasswordToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async resetPassword(token: string, password: string) {
|
|
|
|
|
const user = await this.usersService.findByPasswordResetToken(token);
|
2021-09-21 13:48:28 +00:00
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException('Invalid token');
|
2021-07-26 16:02:47 +00:00
|
|
|
} else {
|
2021-07-26 17:14:14 +00:00
|
|
|
this.usersService.update(user.id, { password, forgotPasswordToken: null });
|
2021-07-26 16:02:47 +00:00
|
|
|
}
|
2021-07-19 05:42:16 +00:00
|
|
|
}
|
2021-07-08 07:39:07 +00:00
|
|
|
}
|