实现auth模块的登录
This commit is contained in:
@@ -1,38 +1,105 @@
|
|||||||
|
import { createHash } from 'crypto';
|
||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||||
import { LoginDto } from './dto/login.dto';
|
import { LoginDto } from './dto/login.dto';
|
||||||
|
import { UserService } from 'src/user/user.service';
|
||||||
|
import { User } from 'src/user/entities/user.entity';
|
||||||
|
import { JwtService } from '@nestjs/jwt';
|
||||||
|
import { UserSessionService } from 'src/user/services/user-session.service';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly userService: UserService,
|
||||||
|
private readonly jwtService: JwtService,
|
||||||
|
private readonly userSessionService: UserSessionService,
|
||||||
|
) { }
|
||||||
|
|
||||||
async loginWithPassword(loginDto: LoginDto) {
|
async loginWithPassword(loginDto: LoginDto) {
|
||||||
const { account, password } = loginDto;
|
const { account, password } = loginDto;
|
||||||
// 依次使用账号、手机号、邮箱登录
|
// 依次使用邮箱登录、手机号、账号
|
||||||
|
const user = await this.userService.findOne([
|
||||||
|
{ email: account },
|
||||||
|
{ phone: account },
|
||||||
|
{ username: account },
|
||||||
|
]);
|
||||||
|
|
||||||
return { message: 'Logged in with password', data: loginDto };
|
if (user === null || !user.password_hash || !user.salt) {
|
||||||
|
throw new BadRequestException('账户或密码错误');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断密码是否正确
|
||||||
|
const hashedPassword = this.hashPassword(password, user.salt);
|
||||||
|
if (hashedPassword !== user.password_hash) {
|
||||||
|
throw new BadRequestException('账户或密码错误');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 登录成功,颁发token
|
||||||
|
return {
|
||||||
|
token: await this.generateToken(user),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async loginWithPhone(loginDto: LoginDto) {
|
async loginWithPhone(loginDto: LoginDto) {
|
||||||
const { phone, code } = loginDto;
|
const { phone, code } = loginDto;
|
||||||
// 先判断验证码是否正确
|
// 先判断验证码是否正确
|
||||||
|
// TODO
|
||||||
|
|
||||||
// 判断用户是否存在,若不存在则进行注册
|
// 判断用户是否存在,若不存在则进行注册
|
||||||
|
let user = await this.userService.findOne({ phone });
|
||||||
|
if (!user) {
|
||||||
|
// 执行注册操作
|
||||||
|
user = await this.userService.create({ phone: phone });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user || !user.userId) {// 注册失败或用户信息错误
|
||||||
|
throw new BadRequestException('请求失败,请稍后再试');
|
||||||
|
}
|
||||||
|
|
||||||
// 登录,颁发token
|
// 登录,颁发token
|
||||||
|
return {
|
||||||
return { message: 'Logged in with phone', data: loginDto };
|
token: await this.generateToken(user),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async loginWithEmail(loginDto: LoginDto) {
|
async loginWithEmail(loginDto: LoginDto) {
|
||||||
const { email, code } = loginDto;
|
const { email, code } = loginDto;
|
||||||
// 先判断验证码是否正确
|
// 先判断验证码是否正确
|
||||||
|
// TODO
|
||||||
|
|
||||||
// 判断用户是否存在,若不存在则进行注册
|
// 判断用户是否存在,若不存在则进行注册
|
||||||
|
let user = await this.userService.findOne({ email });
|
||||||
|
if (!user) {
|
||||||
|
// 执行注册操作
|
||||||
|
user = await this.userService.create({ email: email });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user || !user.userId) {// 注册失败或用户信息错误
|
||||||
|
throw new BadRequestException('请求失败,请稍后再试');
|
||||||
|
}
|
||||||
|
|
||||||
// 登录,颁发token
|
// 登录,颁发token
|
||||||
|
return {
|
||||||
return { message: 'Logged in with email', data: loginDto };
|
token: await this.generateToken(user),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private hashPassword(password: string, salt: string): string {
|
||||||
|
return createHash('sha256').update(`${password}${salt}`).digest('hex');
|
||||||
|
}
|
||||||
|
|
||||||
|
private async generateToken(user: User) {
|
||||||
|
const payload = {
|
||||||
|
userId: user.userId,
|
||||||
|
sessionId: uuidv4(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存储
|
||||||
|
await this.userSessionService.createSession(payload.userId, payload.sessionId);
|
||||||
|
|
||||||
|
// 颁发token
|
||||||
|
return this.jwtService.sign(payload);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user