Skip to content

Latest commit

 

History

History
54 lines (47 loc) · 2.05 KB

0923186ad2b30e.md

File metadata and controls

54 lines (47 loc) · 2.05 KB
title emoji type topics published
NestJSでJWTを使った認証のシーケンス図
🐈‍⬛
tech
nestjs
typescript
nodejs
jwt
true

NestJS では、JWT を使った認証機能が公式で紹介されていて良い感じに実装できる。 Authentication | NestJS - A progressive Node.js framework

ただ、NestJS の Guard って機能が使われていたり、@nestjs/passportの独自仕様的なものがあったりして、動きがわかりずらく初見じゃ辛い。シーケンス図があればいいのになと思ったので書いてみた。

sequenceDiagram
    participant frontend
    participant controller
    participant local.strategy
    participant auth.service
    participant users.service
    participant JwtService
    participant jwt.strategy

    note right of frontend: ログインAPI
    frontend->>controller: username, password
    rect rgb(191, 223, 255)
    note right of controller: 認証(AuthGuard('local'))
    controller->>local.strategy: validate()
    local.strategy->>auth.service: validateUser()
    auth.service->>users.service: findOne()
    users.service-->>auth.service: User
    auth.service-->>local.strategy: User
    local.strategy-->>controller: User
    end
    rect rgb(191, 223, 255)
    note right of controller: トークン取得
    controller->>auth.service: login()
    auth.service->>JwtService: sign()
    JwtService-->>auth.service: access token
    auth.service-->>controller: access token
    end
    controller-->>frontend: access token

    note right of frontend: 認証の必要なAPI
    frontend ->> controller: access token
    note right of controller: JWTトークンの評価(AuthGuard('jwt'))
    controller ->> jwt.strategy: validate()
    jwt.strategy -->> controller: userid, username
    note right of controller: アクセスするAPIのメインの処理
    controller -->> frontend: Response

シーケンス図書くと、整理されるので良き。