-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding strategy for jwt validation (#1)
- Loading branch information
1 parent
52a340c
commit 9c4f629
Showing
17 changed files
with
710 additions
and
19 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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
PORT= | ||
DATABASE_URL= | ||
DATABASE_URL="mongodb://user:password@mongo:27017/morgoth?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.2.6" | ||
|
||
MONGO_INITDB_ROOT_USERNAME="user" | ||
MONGO_INITDB_ROOT_PASSWORD="password" | ||
MONGO_INITDB_ROOT_DATABASE="morgoth" |
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,13 @@ | ||
services: | ||
mongo: | ||
image: mongo | ||
restart: unless-stopped | ||
env_file: | ||
- .env.production.local | ||
ports: | ||
- '27017:27017' | ||
healthcheck: | ||
test: [ 'CMD', 'mongo', 'admin', '--port', '27017', '--eval', "db.adminCommand('ping').ok" ] | ||
interval: 5s | ||
timeout: 2s | ||
retries: 20 |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Empty file.
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,3 @@ | ||
import { Reflector } from '@nestjs/core'; | ||
|
||
export const Roles = Reflector.createDecorator<string[]>(); |
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,19 @@ | ||
import { | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') { | ||
canActivate(context: ExecutionContext) { | ||
return super.canActivate(context); | ||
} | ||
|
||
handleRequest(err: Error, user: any) { | ||
if (err || !user) throw err || new UnauthorizedException(); | ||
|
||
return user; | ||
} | ||
} |
Empty file.
Empty file.
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,29 @@ | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
|
||
import { env } from '~/env'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: env.SECRET_KEY, | ||
}); | ||
} | ||
|
||
async validate(payload: Record<string, string>) { | ||
// const user = await this.prisma.user.findUnique({ | ||
// where: { id: payload.user }, | ||
// select: { | ||
// id: true, | ||
// }, | ||
// }); | ||
|
||
// if (user) return user; | ||
|
||
throw new UnauthorizedException(); | ||
} | ||
} |
Empty file.
Empty file.
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,14 @@ | ||
import { createLogger, format, transports } from 'winston'; | ||
import { join } from 'node:path'; | ||
|
||
export const logger = createLogger({ | ||
format: format.json(), | ||
transports: [ | ||
new transports.Console(), | ||
new transports.File({ | ||
level: 'error', | ||
filename: join(__dirname, 'logs', 'error.log'), | ||
}), | ||
new transports.File({ filename: join(__dirname, 'logs', 'debug.log') }), | ||
], | ||
}); |
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,10 +1,34 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { ValidationPipe } from '@nestjs/common'; | ||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; | ||
|
||
import { env } from '~/env'; | ||
|
||
import { AppModule } from '~/app/app.module'; | ||
|
||
import 'reflect-metadata'; | ||
import { logger } from '~/logger'; | ||
|
||
(async () => { | ||
const app = await NestFactory.create(AppModule); | ||
await app.listen(env.PORT); | ||
const app = await NestFactory.create(AppModule, { }); | ||
app.useGlobalPipes( | ||
new ValidationPipe({ | ||
transform: true, | ||
}), | ||
); | ||
|
||
const config = new DocumentBuilder() | ||
.setTitle('Morgoth') | ||
.setDescription('Docs') | ||
.setVersion('1.0') | ||
.build(); | ||
|
||
app.setGlobalPrefix('api'); | ||
|
||
const document = SwaggerModule.createDocument(app, config); | ||
SwaggerModule.setup('api/docs', app, document); | ||
|
||
await app.listen(env.PORT, () => | ||
logger.error('app running in http://localhost:'.concat(env.PORT)), | ||
); | ||
})(); |