Login in a java action

0
Hi, I need help logging an user in in a java action as the login action is not available in a microflow anymore.   We have custom code on the login screen then I want to sign the anonymous user in server side and not in a nanoflow. I have the user's credentials in the java action and I use the Core.login() function that does create a new session successfully. The session is just not display on the front-end as I am still an anonymous user after the java action processed. I tried to navigate to a page in the microflow after java action and nothing is shown in the front-end and in the logs. I also passed the existing session in and log it out before logging in the user. I also did not get any response.   Does anyone have advice for me how to use the newly created session?
asked
1 answers
1

Hi Thomas,

 

One of the issues we experienced in the past is that the session is not updated or refreshed after signing the user in. In order to refresh the session, you would need to redirect the user in the Java action. Your Java action should look something like this:

ISession session = createSessionForUserAndRedirect(getContext(), getContext().getRuntimeRequest().get(), getContext().getRuntimeResponse().get(), userName, userPassword);
		
return session.getMendixObject();

 

In your extra code, you should have the following:

public static final String XASID_COOKIE = "XASID";
private static final String XAS_SESSION_ID = Core.getConfiguration().getSessionIdCookieName();

public static ISession createSessionForUserAndRedirect(IContext context, IMxRuntimeRequest request, IMxRuntimeResponse response, String userName, String userPassword) throws Exception {
    IUser user = Core.getUser(getContext(), userName);
    if(user!=null && Core.authenticate(getContext(), user, userPassword)) {
	    ISession session = myJavaAction.createSessionForUser(context, response, request, user);
	    return session;
    }
   else {
        return null;
    }
}

private static ISession createSessionForUser(IContext context, IMxRuntimeResponse response, IMxRuntimeRequest request, IUser user) throws Exception {
	final ISession session = Core.initializeSession(user, null);
	final String ua = request.getHeader("User-Agent");

	session.setUserAgent(ua);

	writeSessionCookies(response, session);
		
	return session;
}

private static void writeSessionCookies(IMxRuntimeResponse response, ISession session) {
	response.addCookie(XAS_SESSION_ID, session.getId().toString(), "/", "", -1, true);
	response.addCookie(XASID_COOKIE, "0." + Core.getXASId(), "/", "", -1, true);
}

private static void redirectToIndex(IMxRuntimeRequest request, IMxRuntimeResponse response) {
	response.setStatus(IMxRuntimeResponse.SEE_OTHER);
	response.addHeader("Location", "{yourPageURL}");
}

 

Note that you would need to set the page that you want to redirect to URL

Another note is that myJavaAction (inside createSessionForUserAndRedirect) should be your JavaAction's name

 

Hope it helps!

answered