https://supertokens.com/ logo
m

Mr Chill

05/03/2022, 7:16 AM
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

nkshah2

05/03/2022, 8:22 AM
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

Mr Chill

05/03/2022, 4:14 PM
thanks, this would be nice, I use ThirdPartyMailPassword, Sessions and userMetaData
r

rp

05/04/2022, 4:38 AM
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

Mr Chill

05/05/2022, 9:38 AM
hey @rp thanks for the example i will implement it soon