File size: 3,229 Bytes
8268e91
 
 
 
 
 
73746a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f45e448
8268e91
f45e448
8268e91
73746a8
 
 
 
f45e448
73746a8
 
 
 
 
f45e448
73746a8
 
 
 
 
 
 
 
 
 
426f2a4
73746a8
f45e448
 
 
73746a8
 
8268e91
f45e448
8268e91
73746a8
 
 
 
 
 
 
 
f45e448
73746a8
426f2a4
73746a8
 
 
 
 
f45e448
73746a8
 
 
f45e448
 
73746a8
 
 
 
 
 
 
 
 
 
f45e448
73746a8
 
 
 
 
426f2a4
73746a8
 
8268e91
73746a8
8268e91
73746a8
426f2a4
73746a8
8268e91
73746a8
 
 
8268e91
73746a8
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import {
  Injectable,
  UnauthorizedException,
  BadRequestException,
  OnModuleInit,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as bcrypt from 'bcrypt';
import { User, UserRole } from '../entities/user.entity';
import { RegisterDto } from './dto/register.dto';
import { LoginDto } from './dto/login.dto';

@Injectable()
export class AuthService implements OnModuleInit {
  constructor(
    @InjectRepository(User)
    private userRepository: Repository<User>,
    private jwtService: JwtService,
  ) {}

  async onModuleInit() {
    const adminEmail = 'admin@example.com';
    const adminExists = await this.userRepository.findOne({
      where: { email: adminEmail },
    });
    if (!adminExists) {
      const salt = await bcrypt.genSalt();
      const passwordHash = await bcrypt.hash('123456', salt);
      const adminUser = this.userRepository.create({
        email: adminEmail,
        passwordHash,
        nickname: 'Admin',
        role: UserRole.ADMIN,
      });
      await this.userRepository.save(adminUser);
      console.log(`Admin user initialized: ${adminEmail}`);
    } else {
      // Ensure the role is ADMIN and update password if needed
      if (adminExists.role !== UserRole.ADMIN) {
        adminExists.role = UserRole.ADMIN;
        await this.userRepository.save(adminExists);
      }
    }
  }

  async register(registerDto: RegisterDto) {
    const { email, password, emailCode, nickname } = registerDto;

    // In a real app, verify emailCode here
    if (emailCode !== '123456') {
      throw new BadRequestException('Invalid Email code');
    }

    const existingUser = await this.userRepository.findOne({
      where: { email },
    });
    if (existingUser) {
      throw new BadRequestException('User already exists');
    }

    const salt = await bcrypt.genSalt();
    const passwordHash = await bcrypt.hash(password, salt);

    const user = this.userRepository.create({
      email,
      passwordHash,
      nickname: nickname || `User_${email.split('@')[0].slice(0, 6)}`,
      role: UserRole.USER,
    });

    await this.userRepository.save(user);

    return this.login({ email, password });
  }

  async login(loginDto: LoginDto) {
    const { email, password } = loginDto;
    const user = await this.userRepository.findOne({ where: { email } });

    if (!user) {
      throw new UnauthorizedException('Invalid credentials');
    }

    const isMatch = await bcrypt.compare(password, user.passwordHash);
    if (!isMatch) {
      throw new UnauthorizedException('Invalid credentials');
    }

    const payload = { sub: user.id, email: user.email, role: user.role };
    return {
      userId: user.id,
      token: this.jwtService.sign(payload),
      role: user.role,
      nickname: user.nickname,
      isVip: user.isVip,
    };
  }

  async getProfile(userId: number) {
    const user = await this.userRepository.findOne({
      where: { id: userId },
      select: ['id', 'email', 'nickname', 'avatar', 'role', 'isVip', 'createdAt'],
    });

    if (!user) {
      throw new UnauthorizedException('User not found');
    }

    return user;
  }
}