Skip to content

Commit

Permalink
chore(authentication): add app route registration timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kon14 committed Jul 21, 2022
1 parent 77c2e4b commit 634e778
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions modules/authentication/src/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default class Authentication extends ManagedModule<Config> {
private userRouter: AuthenticationRoutes;
private database: DatabaseProvider;
private localSendVerificationEmail: boolean = false;
private refreshAppRoutesTimeout: NodeJS.Timeout | null = null;

constructor() {
super('authentication');
Expand All @@ -59,7 +60,6 @@ export default class Authentication extends ManagedModule<Config> {
}

protected registerSchemas() {
// @ts-ignore
const promises = Object.values(models).map(model => {
const modelInstance = model.getInstance(this.database);
return this.database.createSchemaFromAdapter(modelInstance);
Expand Down Expand Up @@ -99,8 +99,8 @@ export default class Authentication extends ManagedModule<Config> {

private async refreshAppRoutes() {
if (this.userRouter) {
await this.userRouter.updateLocalHandlers(this.localSendVerificationEmail);
await this.userRouter.registerRoutes();
this.userRouter.updateLocalHandlers(this.localSendVerificationEmail);
this.scheduleAppRouteRefresh();
return;
}
const self = this;
Expand All @@ -112,13 +112,28 @@ export default class Authentication extends ManagedModule<Config> {
self.grpcSdk,
self.localSendVerificationEmail,
);
return this.userRouter.registerRoutes();
this.scheduleAppRouteRefresh();
})
.catch(e => {
ConduitGrpcSdk.Logger.error(e.message);
});
}

private scheduleAppRouteRefresh() {
if (this.refreshAppRoutesTimeout) {
clearTimeout(this.refreshAppRoutesTimeout);
this.refreshAppRoutesTimeout = null;
}
this.refreshAppRoutesTimeout = setTimeout(async () => {
try {
await this.userRouter.registerRoutes();
} catch (err) {
ConduitGrpcSdk.Logger.error(err as Error);
}
this.refreshAppRoutesTimeout = null;
}, 800);
}

// gRPC Service
// produces login credentials for a user without them having to login
async userLogin(
Expand Down

0 comments on commit 634e778

Please sign in to comment.