init, auth-ui, auth-api, auth-core

This commit is contained in:
echo committed 2025-03-24 00:47:33 +01:00
1 parent 094cdb12f8
commit c527aa7bb7
92 files changed
+21484

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
import { DynamicModule, Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { JwtStrategy } from './strategies/jwt.strategy';
import { LocalStrategy } from './strategies/local.strategy';
export interface AuthModuleOptions {
jwtSecret: string;
jwtExpiresIn?: string;
}
@Module({})
export class AuthModule {
static register(options: AuthModuleOptions): DynamicModule {
return {
module: AuthModule,
imports: [
PassportModule,
JwtModule.register({
secret: options.jwtSecret,
signOptions: { expiresIn: options.jwtExpiresIn || '1h' },
}),
],
providers: [
AuthService,
LocalStrategy,
JwtStrategy,
{
provide: 'JWT_SECRET',
useValue: options.jwtSecret,
},
],
exports: [AuthService],
};
}
}
+30
View File
@@ -0,0 +1,30 @@
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { User, AuthTokens } from '@mono/auth-core';
import * as bcrypt from 'bcryptjs';
@Injectable()
export class AuthService {
constructor(private jwtService: JwtService) {}
async validatePassword(password: string, hashedPassword: string): Promise<boolean> {
return bcrypt.compare(password, hashedPassword);
}
async hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, 10);
}
async generateTokens(user: User): Promise<AuthTokens> {
const payload = { sub: user.id, email: user.email };
return {
accessToken: this.jwtService.sign(payload),
refreshToken: this.jwtService.sign(payload, { expiresIn: '7d' }),
};
}
async verifyToken(token: string): Promise<any> {
return this.jwtService.verify(token);
}
}
+4
View File
@@ -0,0 +1,4 @@
export * from './auth.module';
export * from './auth.service';
export * from './strategies/jwt.strategy';
export * from './strategies/local.strategy';
@@ -0,0 +1,18 @@
import { Injectable, Inject } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(@Inject('JWT_SECRET') jwtSecret: string) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: jwtSecret,
});
}
async validate(payload: any) {
return { id: payload.sub, email: payload.email };
}
}
@@ -0,0 +1,15 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor() {
super({ usernameField: 'email' });
}
async validate(email: string, password: string): Promise<any> {
// This method should be overridden in the implementing application
throw new UnauthorizedException('Strategy not implemented');
}
}