204 lines
5.3 KiB
TypeScript
204 lines
5.3 KiB
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import { Repository } from 'typeorm';
|
|
import { UserToken, UserTokenType } from './user-token.entity';
|
|
import { User } from './user.entity';
|
|
import { TokenService } from 'src/modules/utility/services/token.service';
|
|
import * as bcrypt from 'bcrypt';
|
|
import { EmailService } from '../email/email.service';
|
|
import { RegistrationEmail } from './email/registration.email';
|
|
import { ForgotPasswordEmail } from './email/forgot-password.email';
|
|
|
|
@Injectable()
|
|
export class UserService {
|
|
constructor(
|
|
@Inject('USER_REPOSITORY')
|
|
private userRepository: Repository<User>,
|
|
@Inject('USER_TOKEN_REPOSITORY')
|
|
private userTokenRepository: Repository<UserToken>,
|
|
private token: TokenService,
|
|
private email: EmailService,
|
|
) {}
|
|
|
|
public async getById(id: number): Promise<User> {
|
|
return this.userRepository.findOne({ id });
|
|
}
|
|
|
|
public async getByUUID(uuid: string): Promise<User> {
|
|
return this.userRepository.findOne({ uuid });
|
|
}
|
|
|
|
public async getByEmail(email: string): Promise<User> {
|
|
return this.userRepository.findOne({ email });
|
|
}
|
|
|
|
public async getByUsername(username: string): Promise<User> {
|
|
return this.userRepository.findOne({ username });
|
|
}
|
|
|
|
public async get(input: string | number): Promise<User> {
|
|
if (typeof input === 'number') {
|
|
return this.getById(input);
|
|
}
|
|
|
|
if (input.includes('@')) {
|
|
return this.getByEmail(input);
|
|
}
|
|
|
|
if (input.length === 36 && input.includes('-')) {
|
|
return this.getByUUID(input);
|
|
}
|
|
|
|
return this.getByUsername(input);
|
|
}
|
|
|
|
public async updateUser(user: User): Promise<User> {
|
|
await this.userRepository.save(user);
|
|
return user;
|
|
}
|
|
|
|
public async comparePasswords(
|
|
hash: string,
|
|
password: string,
|
|
): Promise<boolean> {
|
|
return bcrypt.compare(password, hash);
|
|
}
|
|
|
|
public async hashPassword(password: string): Promise<string> {
|
|
const salt = await bcrypt.genSalt(10);
|
|
return bcrypt.hash(password, salt);
|
|
}
|
|
|
|
public async createUserToken(
|
|
user: User,
|
|
type: UserTokenType,
|
|
expiresAt?: Date,
|
|
): Promise<UserToken> {
|
|
const token = new UserToken();
|
|
token.token = this.token.generateString(64);
|
|
token.user = user;
|
|
token.type = type;
|
|
token.expires_at = expiresAt;
|
|
await this.userTokenRepository.save(token);
|
|
return token;
|
|
}
|
|
|
|
public async getUserToken(
|
|
token: string,
|
|
type: UserTokenType,
|
|
): Promise<UserToken> {
|
|
const foundOne = await this.userTokenRepository.findOne(
|
|
{
|
|
token,
|
|
type,
|
|
},
|
|
{ relations: ['user'] },
|
|
);
|
|
|
|
if (!foundOne) {
|
|
return null;
|
|
}
|
|
|
|
if (foundOne.expires_at < new Date()) {
|
|
await this.userTokenRepository.remove(foundOne);
|
|
return null;
|
|
}
|
|
|
|
return foundOne;
|
|
}
|
|
|
|
public async deleteUserToken(token: UserToken): Promise<void> {
|
|
await this.userTokenRepository.remove(token);
|
|
}
|
|
|
|
public async sendActivationEmail(user: User): Promise<void> {
|
|
const activationToken = await this.createUserToken(
|
|
user,
|
|
UserTokenType.ACTIVATION,
|
|
new Date(Date.now() + 3600 * 1000),
|
|
);
|
|
|
|
try {
|
|
const content = RegistrationEmail(
|
|
user.username,
|
|
`${process.env.BASE_URL}/login/activate?token=${activationToken.token}`,
|
|
);
|
|
await this.email.sendEmailTemplate(
|
|
user.email,
|
|
'Activate your account on Icy Network',
|
|
content,
|
|
);
|
|
} catch (e) {
|
|
await this.userTokenRepository.remove(activationToken);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public async sendPasswordEmail(user: User): Promise<void> {
|
|
const passwordToken = await this.createUserToken(
|
|
user,
|
|
UserTokenType.PASSWORD,
|
|
new Date(Date.now() + 3600 * 1000),
|
|
);
|
|
|
|
try {
|
|
const content = ForgotPasswordEmail(
|
|
user.username,
|
|
`${process.env.BASE_URL}/login/password?token=${passwordToken.token}`,
|
|
);
|
|
await this.email.sendEmailTemplate(
|
|
user.email,
|
|
'Reset your password on Icy Network',
|
|
content,
|
|
);
|
|
} catch (e) {
|
|
await this.userTokenRepository.remove(passwordToken);
|
|
// silently fail
|
|
}
|
|
}
|
|
|
|
public async userPassword(email: string): Promise<void> {
|
|
const user = await this.getByEmail(email);
|
|
if (!user || !user.activated) {
|
|
return;
|
|
}
|
|
|
|
await this.sendPasswordEmail(user);
|
|
}
|
|
|
|
public async userRegistration(newUserInfo: {
|
|
username: string;
|
|
display_name: string;
|
|
email: string;
|
|
password: string;
|
|
}): Promise<User> {
|
|
if (!!(await this.getByEmail(newUserInfo.email))) {
|
|
throw new Error('Email is already in use!');
|
|
}
|
|
|
|
if (!!(await this.getByUsername(newUserInfo.username))) {
|
|
throw new Error('Username is already in use!');
|
|
}
|
|
|
|
const hashword = await this.hashPassword(newUserInfo.password);
|
|
const user = new User();
|
|
user.email = newUserInfo.email;
|
|
user.uuid = this.token.createUUID();
|
|
user.username = newUserInfo.username;
|
|
user.display_name = newUserInfo.display_name;
|
|
user.password = hashword;
|
|
|
|
await this.userRepository.insert(user);
|
|
|
|
try {
|
|
await this.sendActivationEmail(user);
|
|
} catch (e) {
|
|
await this.userRepository.remove(user);
|
|
throw new Error(
|
|
'Failed to send activation email! Please check your email address and try again!',
|
|
);
|
|
}
|
|
|
|
return user;
|
|
}
|
|
}
|