@frontegg/client
Advanced tools
Comparing version 1.0.17 to 1.0.18
@@ -7,3 +7,3 @@ export declare class FronteggAuthenticator { | ||
private authenticate; | ||
private refreshAuthentication; | ||
refreshAuthentication(): Promise<void>; | ||
} |
@@ -79,3 +79,3 @@ "use strict"; | ||
e_1 = _b.sent(); | ||
console.error('failed to authenticate with frontegg - ', e_1); | ||
console.error('failed to authenticate with frontegg - ', e_1.message); | ||
return [2 /*return*/]; | ||
@@ -82,0 +82,0 @@ case 4: |
@@ -43,3 +43,23 @@ "use strict"; | ||
var authenticator = new authenticator_1.FronteggAuthenticator(); | ||
var MAX_RETRIES = 3; | ||
function proxyRequest(req, res, context) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, proxy.web(req, res, { | ||
target: target, | ||
headers: { | ||
'x-access-token': authenticator.accessToken, | ||
'frontegg-tenant-id': context ? context.tenantId : '' | ||
} | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
function frontegg(options) { | ||
var _this = this; | ||
console.log('you got to my frontegg middleware'); | ||
@@ -59,7 +79,44 @@ if (!options) { | ||
authenticator.init(options.clientId, options.apiKey); | ||
proxy.on('proxyRes', function (proxyRes, req, res) { | ||
if (proxyRes.statusCode && proxyRes.statusCode >= 400) { | ||
console.error('proxy failed - ', proxyRes.statusMessage); | ||
} | ||
}); | ||
proxy.on('error', function (err, req, res, target) { return __awaiter(_this, void 0, void 0, function () { | ||
var context; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.error("Failed proxy request to " + req.url + " - ", err); | ||
req.frontegg.retryCount++; | ||
console.info("retry count of " + req.url + " - ", req.frontegg.retryCount); | ||
if (req.frontegg.retryCount >= MAX_RETRIES) { | ||
res.writeHead(500).end('Frontegg request failed'); | ||
return [2 /*return*/]; | ||
} | ||
return [4 /*yield*/, options.contextResolver(req)]; | ||
case 1: | ||
context = _a.sent(); | ||
proxyRequest(req, res, context); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
proxy.on('proxyRes', function (proxyRes, req, res) { return __awaiter(_this, void 0, void 0, function () { | ||
var context; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!(proxyRes.statusCode === 401)) return [3 /*break*/, 3]; | ||
req.frontegg.retryCount = req.frontegg.retryCount + 1; | ||
console.log(req.url + " failed with authentication error from proxy - retryCount - ", req.frontegg.retryCount); | ||
if (!(req.frontegg.retryCount <= MAX_RETRIES)) return [3 /*break*/, 3]; | ||
console.warn('going to refresh authentication'); | ||
return [4 /*yield*/, authenticator.refreshAuthentication()]; | ||
case 1: | ||
_a.sent(); | ||
console.warn('refreshed authentication'); | ||
return [4 /*yield*/, options.contextResolver(req)]; | ||
case 2: | ||
context = _a.sent(); | ||
return [2 /*return*/, proxyRequest(req, res, context)]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
return function (req, res) { | ||
@@ -73,10 +130,7 @@ return __awaiter(this, void 0, void 0, function () { | ||
context = _a.sent(); | ||
console.log('resolved context - ', context); | ||
proxy.web(req, res, { | ||
target: target, | ||
headers: { | ||
'x-access-token': authenticator.accessToken, | ||
'frontegg-tenant-id': context ? context.tenantId : '' | ||
} | ||
}); | ||
if (!req.frontegg) { | ||
req.frontegg = {}; | ||
} | ||
req.frontegg.retryCount = 0; | ||
proxyRequest(req, res, context); | ||
return [2 /*return*/]; | ||
@@ -83,0 +137,0 @@ } |
{ | ||
"name": "@frontegg/client", | ||
"version": "1.0.17", | ||
"version": "1.0.18", | ||
"description": "Frontegg Javascript Library for backend", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -32,3 +32,3 @@ import axios, { AxiosResponse } from 'axios'; | ||
} catch (e) { | ||
console.error('failed to authenticate with frontegg - ', e) | ||
console.error('failed to authenticate with frontegg - ', e.message) | ||
return | ||
@@ -51,5 +51,5 @@ } | ||
private async refreshAuthentication() { | ||
public async refreshAuthentication() { | ||
await this.authenticate(); | ||
} | ||
} |
@@ -7,4 +7,16 @@ import { FronteggAuthenticator } from '../authenticator' | ||
const MAX_RETRIES = 3; | ||
declare type fronteggContextResolver = (req: Request) => Promise<{ tenantId: string, userEmail: string }>; | ||
async function proxyRequest(req, res, context) { | ||
await proxy.web(req, res, { | ||
target, | ||
headers: { | ||
'x-access-token': authenticator.accessToken, | ||
'frontegg-tenant-id': context ? context.tenantId : '' | ||
} | ||
}) | ||
} | ||
export function frontegg(options: { clientId: string, apiKey: string, contextResolver: fronteggContextResolver }) { | ||
@@ -20,19 +32,38 @@ console.log('you got to my frontegg middleware'); | ||
proxy.on('proxyRes', (proxyRes, req, res) => { | ||
if (proxyRes.statusCode && proxyRes.statusCode >= 400) { | ||
console.error('proxy failed - ', proxyRes.statusMessage) | ||
proxy.on('error', async (err, req: any, res, target) => { | ||
console.error(`Failed proxy request to ${req.url} - `, err); | ||
req.frontegg.retryCount++; | ||
console.info(`retry count of ${req.url} - `, req.frontegg.retryCount); | ||
if (req.frontegg.retryCount >= MAX_RETRIES) { | ||
res.writeHead(500).end('Frontegg request failed'); | ||
return; | ||
} | ||
// Get the context again | ||
const context = await options.contextResolver(req) | ||
proxyRequest(req, res, context); | ||
}) | ||
proxy.on('proxyRes', async (proxyRes, req: any, res) => { | ||
if (proxyRes.statusCode === 401) { | ||
req.frontegg.retryCount = req.frontegg.retryCount + 1; | ||
console.log(`${req.url} failed with authentication error from proxy - retryCount - `, req.frontegg.retryCount); | ||
if (req.frontegg.retryCount <= MAX_RETRIES) { | ||
console.warn('going to refresh authentication') | ||
await authenticator.refreshAuthentication(); | ||
console.warn('refreshed authentication') | ||
const context = await options.contextResolver(req) | ||
return proxyRequest(req, res, context) | ||
} | ||
} | ||
}) | ||
return async function (req, res) { | ||
const context = await options.contextResolver(req) | ||
console.log('resolved context - ', context) | ||
proxy.web(req, res, { | ||
target, | ||
headers: { | ||
'x-access-token': authenticator.accessToken, | ||
'frontegg-tenant-id': context ? context.tenantId : '' | ||
} | ||
}) | ||
if (!req.frontegg) { req.frontegg = {} } | ||
req.frontegg.retryCount = 0; | ||
proxyRequest(req, res, context); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58972
1229