0
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?

More than 1 year has passed since last update.

【Auth0】生成されたJWTにプロパティを追加したい場合のまとめ

Last updated at Posted at 2022-10-05

概要

HasuraPermissionでテーブルへのアクセスや処理の可否を切り分けたかったがAuth0が用意してくれているDBだけでは切り分けが難しかったのでJWTにプロパティを追加した。

公式を見てみるとAuctionsを利用するといいよ!とのことでしたので利用した

RestAPIからデータを取得して必要なデータをJWTに詰め込む想定となっています。

ログイン時の処理にアクションを追加するのでクリック

スクリーンショット 2022-10-05 21.29.01.png

Add Actionのプラスボタン > Build Customをクリック

必須項目を入力して作成する
スクリーンショット 2022-10-05 21.31.09.png

アクションの定義

定義が完了したら「Save Draft」で期待する結果を得られることを確認しDeployを行う

const axios = require('axios');

exports.onExecutePostLogin = async (event, api) => {
  const {user_id} = event.user
  const namespace = "https://hasura.io/jwt/claims";
  
  // アクセストークンに必要な要素を追加していく
  api.accessToken.setCustomClaim(namespace, {
    'x-hasura-hogege': 'standard_user',
      'x-hasura-default-role': 'users',
      'x-hasura-allowed-roles': ['users'],
      'x-hasura-user-id': user_id
  })
};

/**
 * Handler that will be invoked when this action is resuming after an external redirect. If your
 * onExecutePostLogin function does not perform a redirect, this function can be safely ignored.
 *
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */
// exports.onContinuePostLogin = async (event, api) => {
// };

アクションを適用する

スクリーンショット 2022-10-05 21.34.18.png

最後に

ここまでの一連の流れが完了したらログイン時に発行されるJWTを複合すると追加したものが反映されています

0
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
0
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?