@devmoods/express-extras 0.44.0 → 0.44.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/auth.js +20 -7
  2. package/package.json +1 -1
package/dist/auth.js CHANGED
@@ -170,7 +170,7 @@ export function createAuth({ getUserByToken, filterUser, redis, providers, login
170
170
  await redis.del(token);
171
171
  }
172
172
  setAccessTokenCookie(res, null);
173
- res.redirect('/');
173
+ res.redirect(loginUrl);
174
174
  }));
175
175
  /**
176
176
  * Check that a reset token is valid.
@@ -230,25 +230,32 @@ export function createAuth({ getUserByToken, filterUser, redis, providers, login
230
230
  throw new InvalidToken('token expired');
231
231
  }
232
232
  const decoded = decodeAuthToken(token);
233
+ let user;
233
234
  try {
234
- const user = await getUserByToken(decoded);
235
- if (options.isAuthorized && !(await options.isAuthorized(user))) {
236
- throw Forbidden('Permission denied');
237
- }
238
- setCurrentUser(user);
235
+ user = await getUserByToken(decoded);
239
236
  }
240
237
  catch {
241
238
  throw new InvalidToken('User not found');
242
239
  }
240
+ if (options.isAuthorized && !(await options.isAuthorized(user))) {
241
+ throw new PermissionDenied('Permission denied');
242
+ }
243
+ setCurrentUser(user);
243
244
  next();
244
245
  }
245
246
  catch (error) {
246
247
  logger.error(error);
248
+ if (error instanceof PermissionDenied) {
249
+ if (req.accepts('html')) {
250
+ return res.redirect(loginUrl);
251
+ }
252
+ return next(Forbidden('Permission denied'));
253
+ }
247
254
  if (error instanceof InvalidToken) {
248
255
  if (req.accepts('html')) {
249
256
  return res.redirect(loginUrl);
250
257
  }
251
- next(Unauthorized('Invalid token'));
258
+ return next(Unauthorized('Invalid token'));
252
259
  }
253
260
  next(error);
254
261
  }
@@ -347,6 +354,12 @@ class InvalidToken extends Error {
347
354
  this.name = 'Invalid Token';
348
355
  }
349
356
  }
357
+ class PermissionDenied extends Error {
358
+ constructor(message) {
359
+ super(message);
360
+ this.name = 'Permission Denied';
361
+ }
362
+ }
350
363
  /**
351
364
  * Compare two password hashes using bcrypt.
352
365
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devmoods/express-extras",
3
- "version": "0.44.0",
3
+ "version": "0.44.1",
4
4
  "description": "Kubernetes-ready utilities for rapid Node.js application development",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",