メインコンテンツまでスキップ

概要

Zeltはauthentication(ユーザーは誰か?)とauthorization(そのユーザーは何ができるか?)を分離した、柔軟な認証システムを提供します。

authenticationとauthorizationの違い

概念問いZelt API
Authenticationユーザーは誰か?setUser(), currentUser()
Authorization何ができるか?@Authorized(), currentRoles()

authenticationが先に行われ(通常はmiddlewareで)、その後、保護されたルートでauthorizationのチェックが実行されます。

戦略を選ぶ

Zeltは複数の認証戦略をサポートしています。あなたのアーキテクチャに合うものを選んでください。

戦略向いている用途パッケージ
JWTSPA、モバイルアプリ、API@zeltjs/auth-jwt
Sessionsサーバーレンダリングアプリ、伝統的なWebアプリ@zeltjs/auth-session
CustomAPIキー、OAuth、その他の方式組み込みprimitive

選択の指針

認証の流れ

クイックスタート

1. パッケージをインストールする(または組み込みprimitiveを使う)

# JWT認証の場合
pnpm add @zeltjs/auth-jwt

# セッション認証の場合
pnpm add @zeltjs/auth-session @zeltjs/kv

2. middlewareを登録する

const app = createApp([http({
    controllers: [UserController],
    middlewares: [JwtMiddleware],
  })], { configs: [JwtConfig] });

3. ルートを保護する

@Controller('/dashboard')
class DashboardController {
  @Authorized()
  @Get('/')
  index() {
    const user = currentUser();
    return { message: `Hello, ${user?.name}` };
  }
}

次のステップ