refacor: 重构目录架构

This commit is contained in:
2026-09-16 22:43:21 +08:00
parent b37cdb0d54
commit 2754ed1e45
2762 changed files with 0 additions and 1529 deletions
@@ -0,0 +1,18 @@
import { Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LoginDto } from './auth.dto';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('login')
login(@Body() dto: LoginDto) {
return this.authService.login(dto);
}
@Post('logout')
logout() {
return { success: true };
}
}
@@ -0,0 +1,11 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class LoginDto {
@IsString()
@IsNotEmpty()
username!: string;
@IsString()
@IsNotEmpty()
password!: string;
}
@@ -0,0 +1,23 @@
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LcAdmin } from '../../entities/lc-admin.entity';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [
TypeOrmModule.forFeature([LcAdmin]),
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secret: process.env.JWT_SECRET || 'change-me-in-production',
signOptions: { expiresIn: process.env.JWT_EXPIRES_IN || '8h' },
}),
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy],
exports: [JwtModule],
})
export class AuthModule {}
@@ -0,0 +1,38 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import * as bcrypt from 'bcrypt';
import { Repository } from 'typeorm';
import { LcAdmin } from '../../entities/lc-admin.entity';
import { LoginDto } from './auth.dto';
@Injectable()
export class AuthService {
constructor(
@InjectRepository(LcAdmin)
private readonly adminRepo: Repository<LcAdmin>,
private readonly jwtService: JwtService,
) {}
async login(dto: LoginDto) {
const admin = await this.adminRepo.findOne({ where: { username: dto.username } });
if (!admin || admin.status !== 1) {
throw new UnauthorizedException('用户名或密码错误');
}
const ok = await bcrypt.compare(dto.password, admin.password);
if (!ok) {
throw new UnauthorizedException('用户名或密码错误');
}
const expiresIn = process.env.JWT_EXPIRES_IN || '8h';
const token = this.jwtService.sign(
{ sub: admin.id, username: admin.username },
{ expiresIn },
);
const decoded = this.jwtService.decode(token) as { exp?: number };
return {
token,
username: admin.username,
expiresAt: decoded?.exp ? new Date(decoded.exp * 1000).toISOString() : null,
};
}
}
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}
@@ -0,0 +1,26 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
export interface AdminJwtPayload {
sub: number;
username: string;
}
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: process.env.JWT_SECRET || 'change-me-in-production',
});
}
validate(payload: AdminJwtPayload) {
if (!payload?.sub) {
throw new UnauthorizedException();
}
return { adminId: payload.sub, username: payload.username };
}
}