title | emoji | type | topics | published | ||||
---|---|---|---|---|---|---|---|---|
NestJSでJWTを使った認証のシーケンス図 |
🐈⬛ |
tech |
|
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
シーケンス図書くと、整理されるので良き。