build: add TypeScript configuration and generate declaration files
- Add tsconfig.json for TypeScript compilation with declaration and source map generation - Generate .d.ts declaration files for all modules, services, controllers, and models - Update package.json with NestJS dependencies and TypeScript development tools - Include database files in the distribution output for persistence
This commit is contained in:
13
src/auth/auth.module.ts
Normal file
13
src/auth/auth.module.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { UsersModule } from '../users/users.module';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { LocalStrategy } from './local.strategy';
|
||||
import { SessionSerializer } from './session.serializer';
|
||||
|
||||
@Module({
|
||||
imports: [UsersModule, PassportModule.register({ session: true })],
|
||||
providers: [AuthService, LocalStrategy, SessionSerializer],
|
||||
exports: [AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
17
src/auth/auth.service.ts
Normal file
17
src/auth/auth.service.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UsersService } from '../users/users.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(private usersService: UsersService) {}
|
||||
|
||||
async validateUser(username: string, pass: string): Promise<any> {
|
||||
const user = await this.usersService.findOne(username);
|
||||
if (user && user.validPassword(pass)) {
|
||||
// Return user object without password? Or just the user model.
|
||||
// Passport serializer will handle the rest.
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
9
src/auth/guards/authenticated.guard.ts
Normal file
9
src/auth/guards/authenticated.guard.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AuthenticatedGuard implements CanActivate {
|
||||
async canActivate(context: ExecutionContext) {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
return request.isAuthenticated();
|
||||
}
|
||||
}
|
||||
12
src/auth/guards/local-auth.guard.ts
Normal file
12
src/auth/guards/local-auth.guard.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class LocalAuthGuard extends AuthGuard('local') {
|
||||
async canActivate(context: ExecutionContext) {
|
||||
const result = (await super.canActivate(context)) as boolean;
|
||||
const request = context.switchToHttp().getRequest();
|
||||
await super.logIn(request);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
19
src/auth/local.strategy.ts
Normal file
19
src/auth/local.strategy.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Strategy } from 'passport-local';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Injectable()
|
||||
export class LocalStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private authService: AuthService) {
|
||||
super();
|
||||
}
|
||||
|
||||
async validate(username: string, pass: string): Promise<any> {
|
||||
const user = await this.authService.validateUser(username, pass);
|
||||
if (!user) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
19
src/auth/session.serializer.ts
Normal file
19
src/auth/session.serializer.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { PassportSerializer } from '@nestjs/passport';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UsersService } from '../users/users.service';
|
||||
|
||||
@Injectable()
|
||||
export class SessionSerializer extends PassportSerializer {
|
||||
constructor(private readonly usersService: UsersService) {
|
||||
super();
|
||||
}
|
||||
|
||||
serializeUser(user: any, done: Function) {
|
||||
done(null, user.id);
|
||||
}
|
||||
|
||||
async deserializeUser(userId: any, done: Function) {
|
||||
const user = await this.usersService.findById(userId);
|
||||
done(null, user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user