m

    Mr Chill

    4 months ago
    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

    4 months ago
    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

    4 months ago
    thanks, this would be nice, I use ThirdPartyMailPassword, Sessions and userMetaData
    r

    rp

    4 months ago
    hey @Mr Chill
    You can make your own session verification middleware like this:
    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:
    UserMetadata.updateUserMetadata(userId, {
                        timeActive: Date.now()
                    });
    Then whenever you want, you can get the last active time of a user by querying:
    UserMetadata.getUserMetadata(userId).then(r => {
        let lastActiveTime = r.metadata.timeActive
    });
    m

    Mr Chill

    4 months ago
    hey @rp thanks for the example i will implement it soon