1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🚀 JWTを使用したログイン・ログアウト機能のPostmanによる検証手順 🚀

Posted at

概要

本記事では、JWT(JSON Web Token)を使用した認証システムにおいて、PostmanでのAPI検証方法を解説します。特に、Cookieベースでアクセストークンを管理するシステムにおける、ログインからログアウトまでの一連の流れを説明します。

構成

  • 認証方式: JWT (Cookie-based)
  • テストツール: Postman

注:今回の記事には refresh_token の記載もありますが、無視でOKです!

検証環境のセットアップ

1. Postman環境変数の作成

新しい環境を作成し、以下の変数を準備します:

  • base_url: http://localhost:8000
  • access_token: (空の状態で作成)

Cursor.png

ログイン機能の検証

1. ログインリクエストの設定

POST {{base_url}}/api/auth/login
Content-Type: application/json

{

    "email": "test@example.com",
    "password": "test"
}

2. テストスクリプトの設定

ログインリクエストのスクリプトタブに以下のスクリプトを追加します:

// Cookieからトークンを取得
const getCookieValue = (cookieName) => {
    const cookies = pm.cookies.get(cookieName);
    return cookies || null;
};

// access_tokenとrefresh_tokenを取得
const accessToken = getCookieValue('access_token');
const refreshToken = getCookieValue('refresh_token');

// 環境変数に保存
if (accessToken) {
    pm.environment.set('access_token', accessToken);
    console.log('Access token saved:', accessToken);
}

if (refreshToken) {
    pm.environment.set('refresh_token', refreshToken);
    console.log('Refresh token saved:', refreshToken);
}

image.png

3. ログイン成功の確認

  • ステータスコード: 200 OK
  • レスポンスヘッダー: Set-Cookieにアクセストークンが含まれていることを確認
  • Postman環境変数: access_tokenが自動的に設定されていることを確認

ログアウト機能の検証

1. ログアウトリクエストの設定

POST {{base_url}}/api/auth/logout
Authorization: Bearer {{access_token}}

2. 正常系の確認

認可タブで Auth Type をBearer トークンに指定します
トークンの値には {{access_token}} として、環境の値を参照するようにします
image.png

  • ステータスコード: 204 No Content
  • Cookie: access_tokenが削除されていることを確認

まとめ

この手順に従うことで、JWT認証を使用したAPIのログイン・ログアウト機能を効率的にテストすることができます🚀

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?