mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
Merge pull request #594 from benphelps/fix-591
Fix: Add token expiration, tweak error handling for npm
This commit is contained in:
commit
df5ff48000
@ -20,12 +20,18 @@ async function login(loginUrl, username, password) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const status = authResponse[0];
|
const status = authResponse[0];
|
||||||
const data = JSON.parse(Buffer.from(authResponse[2]).toString());
|
let data = authResponse[2];
|
||||||
|
|
||||||
|
try {
|
||||||
|
data = JSON.parse(Buffer.from(authResponse[2]).toString());
|
||||||
|
|
||||||
if (status === 200) {
|
if (status === 200) {
|
||||||
cache.put(tokenCacheKey, data.token);
|
const expiration = new Date(data.expires) - Date.now();
|
||||||
|
cache.put(tokenCacheKey, data.token, expiration - (5 * 60 * 1000)); // expiration -5 minutes
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logger.error(`Error ${status} logging into npm`, authResponse[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [status, data.token ?? data];
|
return [status, data.token ?? data];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,8 +57,8 @@ export default async function npmProxyHandler(req, res) {
|
|||||||
if (!token) {
|
if (!token) {
|
||||||
[status, token] = await login(loginUrl, widget.username, widget.password);
|
[status, token] = await login(loginUrl, widget.username, widget.password);
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
logger.debug(`HTTTP ${status} logging into npm api: ${data}`);
|
logger.debug(`HTTTP ${status} logging into npm api: ${token}`);
|
||||||
return res.status(status).send(data);
|
return res.status(status).send(token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user