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

+36
View File
@@ -0,0 +1,36 @@
{
"name": "@mono/auth-api",
"version": "0.1.0",
"private": true,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --clean --tsconfig tsconfig.json",
"dev": "tsup src/index.ts --format cjs,esm --dts --watch --clean --tsconfig tsconfig.json",
"clean": "rm -rf dist",
"lint": "eslint src/",
"check-types": "tsc --noEmit"
},
"dependencies": {
"@mono/auth-core": "0.1.0",
"@nestjs/common": "^10.3.3",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"bcryptjs": "^2.4.3",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.6",
"@types/passport-jwt": "^4.0.1",
"@types/passport-local": "^1.0.38",
"tsup": "^8.0.2",
"typescript": "^5.3.3"
},
"peerDependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0"
}
}
+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');
}
}
+20
View File
@@ -0,0 +1,20 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"composite": true,
"incremental": true,
"baseUrl": "../..",
"paths": {
"@mono/auth-core": ["packages/auth-core/src"],
"@mono/auth-core/*": ["packages/auth-core/src/*"]
}
},
"include": ["src"],
"references": [
{ "path": "../auth-core" }
]
}