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 ...
https://auth0.com/docs/get-started/applications/confidential-and-public-applications/user-consent-and-third-party-applications#skip-consent-for-first-party-applications

@auth0/auth0-spa-js
Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE. Latest version: 2.3.0, last publis...

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^


コメント