Auth Servis
Bu sayfa, modülün temel API yüzeyini (özellikle AuthService) dokümante eder.
AuthService, nestjs-auth-module tarafından dışa aktarılan ana servisdir.
Seçilen sağlayıcı’dan bağımsız olarak (ör. JWT), token üretme ve doğrulama için tutarlı bir API sağlar.
Metodlar
| Metod | Açıklama |
|---|---|
generateAsync(payload, options?) | Bir payload ve opsiyonel sağlayıcı seçenekleri ile token üretir. |
verifyAsync(token) | Token geçerliyse true, değilse false döner. |
decodeAsync(token) | Token’ı decode eder ve payload döner. Geçersiz/süresi dolmuşsa hata fırlatır. |
Uygulamada AuthService kullanımı
AuthModule’ü register ettikten sonra (Kurulum sayfasına bakın), AuthService’i kendi servis/controller’larınıza inject edebilirsiniz.
generateAsync(payload, options?)
options parametresi sağlayıcı’a özeldir ve seçilen sağlayıcı implementasyonuna doğrudan iletilir.
Tüm sağlayıcı’lar generateAsync için ortak bir response şekli döndürür.
| Alan | Tip | Açıklama |
|---|---|---|
token | string | Üretilen access token. |
expiresIn | string | number | Sağlayıcı’ın döndürdüğü expiration bilgisi. Mevcut JWT sağlayıcı ISO tarih string’i döndürür. |
Yeni bir token üretmek için kullanılır.
import { Body, Controller, Post } from '@nestjs/common';
import { AuthService } from 'nestjs-auth-module';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('token')
async createToken(@Body() body: { userId: string }) {
// payload provider bağımsızdır; JSON-serializable tutun
return this.authService.generateAsync({ userId: body.userId });
}
}
verifyAsync(token)
Sadece boolean sonuç gerekiyorsa (payload istemiyorsanız) bunu kullanın.
import { Controller, Get, Headers, UnauthorizedException } from '@nestjs/common';
import { AuthService } from 'nestjs-auth-module';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Get('verify')
async verify(@Headers('authorization') authorization?: string) {
const token = authorization?.split(' ')[1];
if (!token) throw new UnauthorizedException('Token yok');
const ok = await this.authService.verifyAsync(token);
return { ok };
}
}
decodeAsync(token)
Payload’a ihtiyacınız varsa bunu kullanın. Geçersiz/süresi dolmuş token için hata fırlatması beklenir.
import { Controller, Get, Headers, UnauthorizedException } from '@nestjs/common';
import { AuthService } from 'nestjs-auth-module';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Get('decode')
async decode(@Headers('authorization') authorization?: string) {
const token = authorization?.split(' ')[1];
if (!token) throw new UnauthorizedException('Token yok');
const payload = await this.authService.decodeAsync(token);
return { payload };
}
}
Notlar
- Sadece boolean gerekiyorsa
verifyAsynctercih edin. - Payload gerekiyorsa
decodeAsynckullanın; hata fırlatma durumunu unauthorized akışı olarak ele alın. - JWT için
expiresIn/algorithmgibi sağlayıcı’a özel seçenekler Providers bölümünde anlatılır.