I'm currently implementind OpenID/OAuth authorization in my project and using openid-client package for that.
In this package we initialize an openid client with the following code:
const { Issuer } = require('openid-client');
Issuer.discover('https://accounts.google.com') // => Promise
.then(function (googleIssuer) {
console.log('Discovered issuer %s %O', googleIssuer.issuer, googleIssuer.metadata);
});
const client = new googleIssuer.Client({
client_id: 'zELcpfANLqY7Oqas',
client_secret: 'TQV5U29k1gHibH5bx1layBo0OSAvAbRT3UYW3EWrSYBB5swxjVfWUa1BS8lqzxG/0v9wruMcrGadany3',
redirect_uris: ['http://localhost:3000/cb'],
response_types: ['code'],
// id_token_signed_response_alg (default "RS256")
// token_endpoint_auth_method (default "client_secret_basic")
}); // => Client
How we can implement a singleton client logic? To create a client only once and reuse it all over the application?
I've tried to create a separate class for that but not sure if it is correct:
import { Issuer } from 'openid-client';
export class OpenIdClient {
async createOpenIdClient() {
const issuer = await Issuer.discover(encodeURI(`http://localhost:3101/.well-knownendpoint`));
const client = await new issuer.Client({
client_id: 'clientId',
client_secret: 'clientSecret'
})
return client;
}
}