localhostで開発する際にgetTokenSilently()が動作しないのでexpressをhttps通信にする方法 – 【Auth0】

localhostで開発する際にgetTokenSilently()が動作しないのでexpressをhttps通信にする方法 – 【Auth0】

getTokenSilently()でaudienceを指定してもJWTが取得できませんでした。

http://localhostだとサードパーティクッキーとして判断されるらしく、https://xxx.comみたいにしないとgetTokenSilently()でJWTが取得できないようです。

SSL化

WSL2にapcheをインストールしてSSL化しても良いですが、ちょっと面倒なのでexpressのSSL化を行います。

privatekey.pemとcert.pemを作成します。

$ openssl req -x509 -newkey rsa:2048 -keyout privatekey.pem -out cert.pem -nodes -days 365

server.js

const express = require("express");
const cors = require('cors');
const { join } = require("path");
const app = express();
app.use(cors({origin: true})) // cors ok

app.get("/*", (_, res) => {
  res.sendFile(join(__dirname, "index.html"));
}); // Serve the index page for all other requests
// https
const fs = require('fs');
const server = require('https').createServer(
  {
    key: fs.readFileSync('./privatekey.pem'),
    cert: fs.readFileSync('./cert.pem'),
  },
  app
);
server.listen(4000, () => console.log("Application running on port 4000"));

hosts

C:\Windows\System32\drivers\etc\hostsに以下1行追加します。

dev.com    127.0.0.1

これでlocalhost:4000からdev.com:4000に変更OKです。

テスト

これでnode server.jsを実行します。

https://dev.com:4000/でアクセス可能になります。

参考サイト

How to getTokenSilently without consent on localhost
Hi there, I am currently running through the following example: SPA accessing multiple APIs. I want to request multiple ...
User Consent and Third-Party Applications
Learn how to decouple APIs from applications that consume them and define third-party apps that you don't control or may...
@auth0/auth0-spa-js
Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE. Latest version: 2.1.1, last publis...

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました