Skip to content

Commit

Permalink
[@dhealthdapps/frontend] feat(state): add refresh interceptor for act…
Browse files Browse the repository at this point in the history
…ive sessions
  • Loading branch information
kravchenkodhealth authored and evias committed Jan 17, 2023
1 parent b16a942 commit f85f974
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion runtime/backend/src/common/modules/AuthModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { QueryModule } from "../modules/QueryModule";
import { LogModule } from "../modules/LogModule";
import { AuthService } from "../services/AuthService";
import { AuthStrategy } from "../traits/AuthStrategy";
import { RefreshStrategy } from "../traits/RefreshStrategy";
import { AuthController } from "../routes/AuthController";
import { ValidateChallengeScheduler } from "../schedulers/ValidateChallengeScheduler";
import {
Expand All @@ -41,7 +42,6 @@ import { AuthGateway } from "../gateways/AuthGateway";

// configuration resources
import securityConfigLoader from "../../../config/security";
import { RefreshStrategy } from "../traits";
const auth = securityConfigLoader().auth;

/**
Expand Down Expand Up @@ -82,6 +82,7 @@ const auth = securityConfigLoader().auth;
AuthGateway,
ValidateChallengeScheduler,
SchedulerRegistry,
RefreshStrategy,
],
exports: [AuthService, CipherService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ import { ConfigService } from "@nestjs/config";
// internal dependencies
import { AccessTokenRequest } from "../requests/AccessTokenRequest";
import { AccountsService } from "../services/AccountsService";
import {
Account,
AccountDocument,
AccountQuery,
} from "../models/AccountSchema";
import { AuthService } from "../services/AuthService";
import { LogService } from "../services/LogService";
import { OnAuthCompleted } from "../events/OnAuthCompleted";
Expand Down
33 changes: 33 additions & 0 deletions runtime/dapp-frontend-vue/src/kernel/remote/HttpRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@ import axios, { AxiosResponse } from "axios";
// internal dependencies
import { RequestHandler } from "./RequestHandler";

// refresh flag allows to avoid infinite loop
// in case /refresh fails with 401 unauthorized
let refresh = false;
// set interceptor that will call /refresh in case of status === 401
axios.interceptors.response.use(
(req) => {
return req;
},
async (err) => {
const originalConfig = err.config;

if (
originalConfig.url !== `${process.env.VUE_APP_BACKEND_URL}/auth/token` &&
err.response
) {
if (err.response.status === 401 && !refresh) {
refresh = true;

try {
await axios.get(`${process.env.VUE_APP_BACKEND_URL}/auth/refresh`, {
withCredentials: true,
});
} catch (err) {
console.log("Refresh interceptor: ", err);
}
}
}

refresh = false;
return err;
}
);

/**
* @class HttpRequestHandler
* @description This class implements a handler for API calls
Expand Down

0 comments on commit f85f974

Please sign in to comment.