Hey, I need from the users when they last logged i...
# general
m
Hey, I need from the users when they last logged in. About getUser i get only "timeJoined". Is this stored in the database of the core at all or do I have to store it myself via "updateUserMetadata"?
n
Hi, We currently dont store this information, you would have to handle that yourself
@Mr Chill I can add more information here if you like, what recipe are you using?
m
thanks, this would be nice, I use ThirdPartyMailPassword, Sessions and userMetaData
r
hey @Mr Chill
You can make your own session verification middleware like this:
Copy code
ts
import { SessionRequest } from "../../framework/express"
import UserMetadata from "../../recipe/usermetadata";

function customVerifySession(options: Session.VerifySessionOptions) {
    return (req: SessionRequest, res, next) => {
        verifySession(options)(req, res, (err) => {
            if (err) {
                return next(err);
            }
            if (req.session !== undefined) {
                let userId = req.session.getUserId();

                // we do this asynchronously...
                UserMetadata.updateUserMetadata(userId, {
                    timeActive: Date.now()
                });
            }
            next();
        })
    }
}
And then you can use
customVerifySession
everywhere in your APIs. Furthermore, you might want to override the sign in / up APIs as well to do:
Copy code
UserMetadata.updateUserMetadata(userId, {
                    timeActive: Date.now()
                });
Then whenever you want, you can get the last active time of a user by querying:
Copy code
UserMetadata.getUserMetadata(userId).then(r => {
    let lastActiveTime = r.metadata.timeActive
});
m
hey @rp thanks for the example i will implement it soon
2 Views