-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #821 from undb-xyz/develop
- Loading branch information
Showing
787 changed files
with
10,994 additions
and
4,239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ node_modules | |
*.log | ||
docker | ||
out | ||
.ego | ||
.undb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,4 @@ yarn-error.log* | |
|
||
.pnpm-store/ | ||
|
||
.ego/ | ||
.undb/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["@egodb/eslint-config"], | ||
extends: ['@undb/eslint-config'], | ||
parserOptions: { | ||
emitDecoratorMetadata: true, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Body, Controller, Get, HttpCode, HttpStatus, Post, Request, UseGuards } from '@nestjs/common' | ||
import { JwtService } from '@nestjs/jwt' | ||
import { AuthService } from './auth.service.js' | ||
import { JwtAuthGuard } from './jwt-auth.guard.js' | ||
import { LocalAuthGuard } from './local-auth.guard.js' | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService, private jwtService: JwtService) {} | ||
|
||
@HttpCode(HttpStatus.OK) | ||
@Post('register') | ||
async register(@Body() body: { password: string; email: string }) { | ||
return this.authService.register(body.email, body.password) | ||
} | ||
|
||
@HttpCode(HttpStatus.OK) | ||
@UseGuards(LocalAuthGuard) | ||
@Post('login') | ||
async login(@Request() req: Express.Request) { | ||
const payload = await this.authService.login(req.user as any) | ||
return { access_token: this.jwtService.sign(payload) } | ||
} | ||
|
||
@HttpCode(HttpStatus.OK) | ||
@UseGuards(JwtAuthGuard) | ||
@Get('me') | ||
getProfile(@Request() req: Express.Request) { | ||
return this.authService.me(req.user as any) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Module } from '@nestjs/common' | ||
import { ConfigType } from '@nestjs/config' | ||
import { CqrsModule } from '@nestjs/cqrs' | ||
import { JwtModule } from '@nestjs/jwt' | ||
import { PassportModule } from '@nestjs/passport' | ||
import { authConfig } from '../configs/auth.config.js' | ||
import { UserModule } from '../modules/user/user.module.js' | ||
import { AuthController } from './auth.controller.js' | ||
import { AuthService } from './auth.service.js' | ||
import { NestLgoinCommandHandler, NestRegisterCommandHandler } from './commands/index.js' | ||
import { JwtStrategy } from './jwt.strategy.js' | ||
import { LocalStrategy } from './local.strategy.js' | ||
import { NestGetMeQueryHandler } from './queries/index.js' | ||
|
||
const CommandHandlers = [NestLgoinCommandHandler, NestRegisterCommandHandler] | ||
const QueryHandlers = [NestGetMeQueryHandler] | ||
|
||
@Module({ | ||
imports: [ | ||
CqrsModule, | ||
UserModule, | ||
PassportModule, | ||
JwtModule.registerAsync({ | ||
useFactory: (config: ConfigType<typeof authConfig>) => ({ | ||
secret: config.jwt.secret, | ||
signOptions: { | ||
expiresIn: '20d', | ||
}, | ||
}), | ||
inject: [authConfig.KEY], | ||
}), | ||
], | ||
providers: [AuthService, LocalStrategy, JwtStrategy, ...CommandHandlers, ...QueryHandlers], | ||
controllers: [AuthController], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { CommandBus, QueryBus } from '@nestjs/cqrs' | ||
import { IQueryUser, WithUserEmail } from '@undb/core' | ||
import { GetMeQuery, LoginCommand, RegisterCommand } from '@undb/cqrs' | ||
import * as bcrypt from 'bcrypt' | ||
import { UserService } from '../modules/user/user.service.js' | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private usersService: UserService, | ||
private readonly commandBus: CommandBus, | ||
private readonly queryBus: QueryBus, | ||
) {} | ||
|
||
async validateUser(email: string, pass: string): Promise<IQueryUser | null> { | ||
const user = await this.usersService.findOne(WithUserEmail.fromString(email)) | ||
if (!user) return null | ||
|
||
const isPasswordMatch = await bcrypt.compare(pass, user.password) | ||
if (isPasswordMatch) return user.toQuery() | ||
return null | ||
} | ||
|
||
async register(email: string, password: string) { | ||
const hashedPassword = await bcrypt.hash(password, 10) | ||
return this.commandBus.execute(new RegisterCommand({ email, password: hashedPassword })) | ||
} | ||
|
||
async login(user: IQueryUser) { | ||
return this.commandBus.execute(new LoginCommand({ user })) | ||
} | ||
|
||
async me(user: IQueryUser) { | ||
return this.queryBus.execute(new GetMeQuery({ me: user })) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './login.command.handler.js' | ||
export * from './register.command.handler.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { CommandHandler } from '@nestjs/cqrs' | ||
import { LoginCommand, LoginCommandHandler } from '@undb/cqrs' | ||
|
||
@CommandHandler(LoginCommand) | ||
export class NestLgoinCommandHandler extends LoginCommandHandler {} |
Oops, something went wrong.