ToolJet/server/src/services/auth.service.ts

82 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-07-26 16:02:47 +00:00
import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
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';
import { EmailService } from './email.service';
2021-07-08 07:39:07 +00:00
const bcrypt = require('bcrypt');
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,
private organizationUsersService: OrganizationUsersService,
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);
if (!user) return null;
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),
email: user.email,
first_name: user.firstName,
last_name: user.lastName,
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
if (process.env.DISABLE_SIGNUPS === 'true') {
return {};
2021-07-19 05:42:16 +00:00
}
2021-07-19 05:42:16 +00:00
const { email } = params;
const organization = await this.organizationsService.create('Untitled organization');
const user = await this.usersService.create({ email }, organization);
// 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');
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);
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
}