I've written a singleton class in Typescript to avoid having the keep declaring/creating the same OktaAuth
object namely: new OktaAuth({ issuer: appConfig.OKTA_ORG_URL })
.
(By the way I did this as I assume this the best way to write such things)
So at the moment my code looks like
export class OktaAuthorisation {
private static instance: OktaAuthorisation;
private constructor() {
}
static getInstance(): OktaAuthorisation {
if (!OktaAuthorisation.instance) {
OktaAuthorisation.instance = new OktaAuthorisation();
}
return OktaAuthorisation.instance;
}
private authClient = new OktaAuth({ issuer: appConfig.OKTA_ORG_URL });
async accessToken(): Promise<OktaAccessToken> {
return await this.authClient.tokenManager.get("accessToken");
}
async getOktaUserEmail() {
let oktaUser: OktaUser = await this.authClient.token.getUserInfo(await this.accessToken());
return oktaUser.email
}
}
My question is where should the line private authClient = new OktaAuth({ issuer: appConfig.OKTA_ORG_URL });
live? Where it is in the above or should I be setting that in the constructor?
I'm not sure whether it would be recreated each time I use it - which I do like:
const oktaAuth = OktaAuthorisation.getInstance();
const myUser = await oktaAuth.getOktaUserEmail()
Also, in general is the tidiest way to go about this? And if I don't use the constructor for anything I guess I can remove it?
Thanks.