Last logged-in date and change session expire date

Hello,

Iā€™m using Moralis-v1 on my Project (itā€™s built with ReactJS). I have two questions;

  1. How can I get user last logged date? (I checked the DB. But only userā€™s updatedAt field changed. I canā€™t follow this, because this field changing when any field updated.)

  2. How can I forge user to login again everyday? (I read the documentation. There is a note ā€œYou can set your session expire date in Dashboard settingsā€. But it is not exist.)

Thanks & Best regards

there is a Session table where you have that information, when a user authenticates automatically a row in Session table will be created

1 Like

Thanks for the answer.

This might help my first question. I started getting it in the createdAt field.

Do you have an answer to my second question?

Thanks again.

you can make a script that authenticates automatically if you want, you will need a private key to sign a message

I think I explained it wrong. Since the session duration is 1 year, when a person opens the project the next day, the person is automatically logged in. I want to force that person to log in again if the session duration has passed 1 day.

ok, this is easy then, you can use an afterSave hook for Session table and change the expiration date for a session to 1 day from 1 year

I tried your suggestion with the code below. However, a single line does not appear in the log. Do you have any idea where I am doing wrong?

My cloud code;

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.afterSave("_Session", async (request) => {
  logger.error("Session.afterSave ???");
});

Note: Iā€™m using cloud functions for different processes. Those are working correctly. (like; afterSave User, afterSave BscNFTTransfers)

Note: My server version v0.0.383

Try logger.info, also try to remove the _ from that table name.

I tried these codes. But nothing happened.

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.afterSave("Session", async (request) => {
  try {
    logger.info(
      "afterSave Session -> request.object :: " + JSON.stringify(request.object)
    );
  } catch (error) {
    logger.info("afterSave Session -> error :: " + error);
  }
});

Moralis.Cloud.afterSave("_Session", async (request) => {
  try {
    logger.info(
      "afterSave _Session -> request.object :: " +
        JSON.stringify(request.object)
    );
  } catch (error) {
    logger.info("afterSave _Session -> error :: " + error);
  }
});

Moralis.Cloud.afterSave(Moralis.Session, async (request) => {
  try {
    logger.info(
      "afterSave Moralis.Session -> request.object :: " +
        JSON.stringify(request.object)
    );
  } catch (error) {
    logger.info("afterSave Moralis.Session -> error :: " + error);
  }
});

Before save works?

Did you make a change in the database after that?

beforeSave gave no results either.

No, I did not make any changes to the database. ACL General for this table: Read, Write, Add Field. The session table works fine, but the cloud functions just donā€™t give any results for this table.

I mean, if you try to edit a row in session table then that cloud function should trigger

Are you sure that the cloud code works fine now?

If you call another simple function then it works?

Yes, my cloud codes works fine. No error appears. I am using these 2 functions at the same time;

Moralis.Cloud.afterSave("BscNFTTransfers", (request) => { ... });
Moralis.Cloud.afterSave(Moralis.User, (request) => { ... });

strange, the server seems to work

you can try to make a cloud function, that reads all the data from session table, and check the expiration time, if it is more than 1 day to update it, and you can run that cloud function daily