Ana içeriğe geç

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​

MetodAçı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.

AlanTipAçıklama
tokenstringÜretilen access token.
expiresInstring | numberSağ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.

auth.controller.ts
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.

auth.controller.ts
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.

auth.controller.ts
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 verifyAsync tercih edin.
  • Payload gerekiyorsa decodeAsync kullanın; hata fırlatma durumunu unauthorized akışı olarak ele alın.
  • JWT için expiresIn / algorithm gibi sağlayıcı’a özel seçenekler Providers bölümünde anlatılır.