Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session expired after closing the application , even if does not cross 2hrs session expiration time #46

Closed
wrteam-priyansh opened this issue Dec 10, 2020 · 6 comments

Comments

@wrteam-priyansh
Copy link

Session expires after closing the application and also CubeChatConnection.instance.isAuthenticated() become false even after login..Is this expected behaviour or i am missing something

This is my login function..
//To login into connnectyCube
static Future loginToConnectyCube(BuildContext context) async {
//Checking if session is valid or not
CubeSession cubeSession = CubeSessionManager.instance.activeSession;

if (CubeSessionManager.instance.isSessionExpired(cubeSession)) {
  print("Session is expired");
  //Id for connectyCube
  int id;
  //Current user's email id
  String userEmail = Constant.session.getData(UserSessionManager.KEY_EMAIL);

  //Fetching connectyCube user Id
  if (Constant.session.getIntData(UserSessionManager.KEY_CONNECTYCUBE_ID) !=
      0) {
    print("Getting connectyCube id from local storage");
    id =
        Constant.session.getIntData(UserSessionManager.KEY_CONNECTYCUBE_ID);
  } else {
    //check if user is register or not

    //Create session
    cubeSession = await createSession();
    print("Session created");
    CubeUser checkCubeUserExist =
        await checkUserAlreadyExistInConnectyCube();

    if (checkCubeUserExist == null) {
      print("Registering the user");

      CubeUser registeredCubeUser = await registerUserToConnectyCube(
          userid: Constant.session.getData(UserSessionManager.KEY_ID),
          email: userEmail,
          firstName: Constant.session.getData(UserSessionManager.KEY_FNAME),
          lastName: Constant.session.getData(UserSessionManager.KEY_LNAME),
          phone: Constant.session.getData(UserSessionManager.KEY_PHONE));
      id = registeredCubeUser.id;

      Constant.session.setIntData(
          UserSessionManager.KEY_CONNECTYCUBE_ID, registeredCubeUser.id);
    } else {
      //if user is already registered
      print("User is already registered");
      id = checkCubeUserExist.id;
      Constant.session.setIntData(
          UserSessionManager.KEY_CONNECTYCUBE_ID, checkCubeUserExist.id);
    }
  }
  print("Current user's connecty cube Id : $id");
  CubeUser loginUser = CubeUser(
      email: Constant.session.getData(UserSessionManager.KEY_EMAIL),
      login: connectyCubeLoginId(),
      id: id,
      password: ConnectyCube_DEFAULT_PASS);

  if (cubeSession == null) {
    //if session is not created before
    cubeSession = await createSession(loginUser);
    await CubeChatConnection.instance.login(loginUser);
  } else {
    await signIn(loginUser);
    await CubeChatConnection.instance.login(loginUser);
  }
} else {
  print("Session is valid");
}

}

@TatankaConCube
Copy link
Contributor

@wrteam-priyansh first of all, sorry for a longtime answer. It happens because we don't save session data to storage only to variables and they clean by the system after the app destroy. You have the right logic for session managing, but your code can be simplified by using callback restoreSession which will be called if our SDK needs a session with a user. More by link.

@wrteam-priyansh
Copy link
Author

@TatankaConCube Thanks for replying , i have used this code in order to restore session...
onSessionRestore: () async {
if (Constant.session.getBoolData(UserSessionManager.IS_USER_LOGIN)) {
print("User is logged in");
CubeUser cubeUser = CubeUser(
id: Constant.session
.getIntData(UserSessionManager.KEY_CONNECTYCUBE_ID),
email: Constant.session.getData(UserSessionManager.KEY_EMAIL),
login: Constant.session
.getData(UserSessionManager.KEY_CONNECTYCUBE_LOGIN),
password: Constant.ConnectyCube_DEFAULT_PASS);

    return createSession(cubeUser);
  } else {
    print("User is logged not in");
  }
  return null;
}

but still i have to follow the login process after closing the app.
Can you please tell me what is wrong in above mention code

@TatankaConCube
Copy link
Contributor

It is expected behavior when we create a new session after app closing. If you don't want to recreate the session after app closing, you can save the active session in storage then after app opening set it as an active session via:

CubeSession savedSession; // get saved session
CubeSessionManager.instance.activeSession = savedSession;

@wrteam-priyansh
Copy link
Author

@TatankaConCube Thanks for replying. will try this soon

@wrteam-priyansh
Copy link
Author

@TatankaConCube do we have to follow login process every time opens application?

@TatankaConCube
Copy link
Contributor

It is required if you use API calls. It needs to provide an additional security level for your app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants