You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 9, 2021. It is now read-only.
Instead of having a script to prepare the plugins, we could have a Next.js Webpack plugin. This has the benefit of easier integration into Next.js.
The plugin would roughly be this
"use strict";constwrite=require("write");constfetch=require("node-fetch");constjwkToPem=require("jwk-to-pem");constregion=process.env.AUTH_AWS_COGNITO_REGION;constuserPoolId=process.env.AUTH_AWS_COGNITO_USER_POOL_ID;constred=(str)=>`\x1b[31m${str}\x1b[0m`;asyncfunctionfetchJwks(){consturl=`https://cognito-idp.${region}.amazonaws.com/${userPoolId}/.well-known/jwks.json`;constjwks=awaitfetch(url).then((res)=>res.json()).catch((error)=>{console.error(red("AwsCognitoPemPlugin"),`Could not fetch jwks.json from Cognito`);console.error(`Tried "${url}", but it does not exist.`);console.error(``);console.error(`Maybe the provided region (${region}) or userPoolId (${userPoolId}) are incorrect?`);returnprocess.exit(1);});if(jwks.message){console.error(red("AwsCognitoPemPlugin"),jwks.message);returnprocess.exit(1);}if(!Array.isArray(jwks.keys)||jwks.keys.length===0){console.error(red("AwsCognitoPemPlugin"),"No keys present in response");console.log(jwks);returnprocess.exit(1);}returnjwks;}// map public-keys to pems, so the client/server don't need to do it// on every requestfunctionmapJwksToPems(jwks){return{
...jwks,keys: jwks.keys.map((key)=>({
...key,pem: jwkToPem(key),})),};}module.exports=classAwsCognitoPemPlugin{constructor(options){if(!options){thrownewError(`Please provide 'options' for the AwsCognitoPemPlugin config`);}// these are the webpack options provided by Next.js in// webpack: (config, options) => {}// which the callees forward to usthis.options=options;// cache to avoid fetching multiple timesthis.pems=null;}apply(compiler){// only run on the server to avoid fetching the pems twice (client & server)if(!this.options.isServer)return;compiler.hooks.emit.tapPromise("AwsCognitoPemPlugin",async(compilation)=>{if(this.pems)returnconstjwks=awaitfetchJwks();this.pems=mapJwksToPems(jwks);write.sync("./pems.json",JSON.stringify(this.pems,null,2));});}};
Instead of having a script to prepare the plugins, we could have a Next.js Webpack plugin. This has the benefit of easier integration into Next.js.
The plugin would roughly be this
And it would be used like that
The plugin would fetch the JWKs from the Cognito user pool, add a
pem
entry to each key and write them out topems.json
.You'd also need to provide environment variables, e.g. in
.env.local
like this:The reason we start with
AUTH_
is that Vercel and other hosting solutions reserve env vars starting withAWS_
, e.g. Vercel's reserved env vars listThe text was updated successfully, but these errors were encountered: