Skip to content

Commit

Permalink
feat(users): log last logged in and last active
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed May 21, 2020
1 parent b1aec03 commit c09c92f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions middlewares/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ exports.maybeAuthorize = async (req, res, next) => {

await req.permissions.fetchCurrentUserPermissions();

await req.user.update({ last_active: new Date() });

return next();
};

Expand Down
5 changes: 5 additions & 0 deletions middlewares/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ module.exports.login = async (req, res) => {
const accessToken = await AccessToken.createForUser(user.id);
const refreshToken = await RefreshToken.createForUser(user.id);

await user.update({
last_logged_in: new Date(),
last_active: new Date()
});

return res.json({
success: true,
access_token: accessToken.value,
Expand Down
11 changes: 11 additions & 0 deletions migrations/20200521124153-add-last-logged-in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn(
'users',
'last_logged_in',
{
type: Sequelize.DATE,
allowNull: true
}
),
down: (queryInterface) => queryInterface.removeColumn('users', 'last_logged_in')
};
11 changes: 11 additions & 0 deletions migrations/20200521124307-add-last-active.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn(
'users',
'last_active',
{
type: Sequelize.DATE,
allowNull: true
},
),
down: (queryInterface) => queryInterface.removeColumn('users', 'last_active')
};
8 changes: 8 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ const User = sequelize.define('user', {
type: Sequelize.TEXT,
allowNull: true,
defaultValue: ''
},
last_logged_in: {
type: Sequelize.DATE,
allowNull: true
},
last_active: {
type: Sequelize.DATE,
allowNull: true
}
}, {
underscored: true,
Expand Down

0 comments on commit c09c92f

Please sign in to comment.