mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 05:09:43 +02:00
Initial commit
This commit is contained in:
commit
36fe34e8e1
583 changed files with 91539 additions and 0 deletions
56
server/api/hooks/current-user/index.js
Normal file
56
server/api/hooks/current-user/index.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* current-user hook
|
||||
*
|
||||
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
|
||||
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
|
||||
*/
|
||||
|
||||
module.exports = function defineCurrentUserHook(sails) {
|
||||
const TOKEN_PATTERN = /^Bearer /;
|
||||
|
||||
const getUser = async accessToken => {
|
||||
let id;
|
||||
|
||||
try {
|
||||
id = Number(sails.helpers.verifyToken(accessToken));
|
||||
} catch (unusedError) {
|
||||
return;
|
||||
}
|
||||
|
||||
return await sails.helpers.getUser(id);
|
||||
};
|
||||
|
||||
return {
|
||||
/**
|
||||
* Runs when this Sails app loads/lifts.
|
||||
*/
|
||||
|
||||
initialize: async function() {
|
||||
sails.log.info('Initializing custom hook (`current-user`)');
|
||||
},
|
||||
|
||||
routes: {
|
||||
before: {
|
||||
'/*': {
|
||||
fn: async function(req, res, next) {
|
||||
const { authorization: authorizationHeader } = req.headers;
|
||||
|
||||
if (
|
||||
authorizationHeader &&
|
||||
TOKEN_PATTERN.test(authorizationHeader)
|
||||
) {
|
||||
const accessToken = authorizationHeader.replace(
|
||||
TOKEN_PATTERN,
|
||||
''
|
||||
);
|
||||
|
||||
req.currentUser = await getUser(accessToken);
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue