nestjs-basic-auth
v0.0.3NESTJS BASIC AUTH RECIPE
xnex install nestjs-basic-auth GitHub repo NestJS Basic Auth
JWT tabanlı, minimal ve genişletilebilir kimlik doğrulama modülü içeren bir NestJS başlangıç projesi. JwtSetupModule herhangi bir NestJS projesine plug-in olarak eklenebilecek şekilde tasarlanmıştır.
Özellikler
- JWT ile kimlik doğrulama —
@nestjs/jwtüzerinden token üretimi ve doğrulaması - Guard tabanlı koruma —
NexAuthGuardile route/controller seviyesinde erişim kontrolü - Ortam değişkeni tabanlı config —
@nestjs/configileregisterAskullanılarak izole JWT ayarları - Bearer token çözümleme —
Authorization: Bearer <token>header'ından otomatik token çıkarımı - Global config modülü —
ConfigModule.forRoot({ isGlobal: true })ile tek noktadan yapılandırma
Proje Yapısı
src/
├── common/
│ └── guards/
│ └── auth.guard.ts # NexAuthGuard - token doğrulama guard'ı
├── configs/
│ └── jwt.config.ts # JWT secret & sign options config
├── modules/
│ └── jwt/
│ └── jwt.module.ts # JwtSetupModule - JWT + Config birleşik modül
├── app.module.ts
└── main.ts
Kurulum
xnex install -r nestjs-basic-auth
.env dosyası oluşturup aşağıdaki değişkenleri tanımlayın (opsiyonel — tanımlanmazsa varsayılan değerler kullanılır):
JWT_SECRET=your-secret-key
EXPIRES=1d
Not:
JWT_SECRETprodüksiyon ortamında mutlaka ayarlanmalıdır. Varsayılan değer (S3C43T-53Y) yalnızca geliştirme amaçlıdır.
Çalıştırma
# geliştirme modu (watch)
bun run start:dev
# production build
bun run build
bun run start:prod
Kullanım
Guard'ı bir controller'a uygulama
import { UseGuards } from '@nestjs/common';
import { NexAuthGuard } from './common/guards/auth.guard';
@UseGuards(NexAuthGuard)
@Controller('protected')
export class ProtectedController {
// ...
}
ÖRNEK KULLANIM CONTROLLER
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Request } from 'express';
import { NexAuthGuard } from 'src/common/guards/auth.guard';
@Controller('jwt')
export class JWTController {
constructor(private readonly jwtService: JwtService) { }
@Post('create')
createJwt(@Body() body: any) {
return {
message: "Add this token to the 'Authorization' header when submitting your 'POST / me' request.",
access: this.jwtService.sign(body)
}
}
@Post('me')
@UseGuards(NexAuthGuard)
me(@Req() req: Request) {
return req['user']
}
}
İstek örneği
GET /protected
Authorization: Bearer <jwt_token>
Token geçerliyse request.user alanına decode edilmiş payload atanır ve istek işleme devam eder. Token eksik, geçersiz veya süresi dolmuşsa 401 Unauthorized döner.
Modül Detayları
JwtSetupModule
ConfigModule ve JwtModule'ü birlikte kurar; jwtConfig üzerinden asenkron olarak secret ve signOptions değerlerini enjekte eder. Projeye eklemek için AppModule'ün imports dizisine eklemeniz yeterlidir:
@Module({
imports: [JwtSetupModule],
})
export class AppModule {}
NexAuthGuard
CanActivate arayüzünü implemente eder. Authorization header'ından Bearer token'ı çıkarır, JwtService.verifyAsync ile doğrular ve başarılıysa isteğe user alanını ekler.
Gereksinimler
- Node.js
- NestJS 11.x
Lisans
UNLICENSED — özel/dahili kullanım içindir.
setup.files
Files that will be integrated into your project
- recipes
- nestjs-basic-auth
- README.md → recipes/nestjs-basic-auth
-
-
- src
- common
- guards
- auth.guard.ts → src/common/guards
-
-
- configs
- jwt.config.ts → src/configs
-
- modules
- jwt
- jwt.module.ts → src/modules/jwt
-
-
-
setup.scripts
Commands executed during recipe integration
These commands run automatically during integration — after your explicit approval via the Nex CLI.
- 1 Auto-run script
bun install @nestjs/config @nestjs/jwt
nexconfig
Full recipe configuration
{
"name": "nestjs-basic-auth",
"version": "0.0.3",
"description": "NESTJS BASIC AUTH RECIPE",
"keywords": [
"nestjs",
"nex",
"auth",
"recipe"
],
"files": [
"src/common",
"src/configs",
"src/modules",
"recipes/nestjs-basic-auth"
],
"setup": {
"scripts": [
"bun install @nestjs/config @nestjs/jwt"
],
"files": {
"src/common": {
"guards": {
"auth.guard.ts": "src/common/guards"
}
},
"src/configs": {
"jwt.config.ts": "src/configs"
},
"src/modules": {
"jwt": {
"jwt.module.ts": "src/modules/jwt"
}
},
"recipes/nestjs-basic-auth": {
"README.md": "recipes/nestjs-basic-auth"
}
},
"variants": [
{
"name": "jwt test controller",
"description": "add simple jwt token test controller",
"setup": {
"files": {
"src/modules/jwt/jwt.controller.ts": "src/modules/jwt"
}
}
}
]
}
}
jwt test controller
add simple jwt token test controller
Files
- src
- modules
- jwt
- jwt.controller.ts → src/modules/jwt
-
-
-
`jwt test controller` raw config
Full `jwt test controller` variant configuration
{
"name": "jwt test controller",
"description": "add simple jwt token test controller",
"setup": {
"files": {
"src/modules/jwt/jwt.controller.ts": "src/modules/jwt"
}
}
}
You might also be interested in these
nestjs
NESTJS EMPTY TEMPLATE
nestjs-logging
NESTJS REQUEST LOGGING MIDDLEWARE RECIPE
spring-logger
Basic logger middleware recipe
basic-doc
A dependency-free page explaining what a Nex recipe is and how it works
axum-log
Basic log middleware for axum api application.