I am using the "Secure" module from play framework and its mostly working great. I can login a user by posting a from the the /login route in my application.
However, when I "create/signup" a new user, I would like for that user to be automatically logged in after user creation. This, I can't seem to get working programmatically, i.e., by not having to post a form to login.
My current code for my user create action is like:
public static void create(@Required @Email String email, @Required String password, @Required @Equals("password") String passwordConfirmation) {
User user = new User(email);
user.password = new Crypto().encryptAES(password);
user.save();
try {
Secure.authenticate(email, password, false); // <== this is the action in secure module mapped to /login
} catch(Throwable t) {
t.printStackTrace();
}
}
So I manually call Secure.authentication(username, password, remember)
method, but this just redirects me to the login page and not my target page.
I have a feeling that a cookie or something is not being set because I am calling Secure.authenticate directly and not via a request/response, but I'm just not sure.
Any ideas?
One idea could be to set the username in the session (after user signed up successfully)
session.put("username", username);
This will make sure Secure.connected() return as expected.
if you need 'remember me',
if(remember){
response.setCookie("rememberme", Crypto.sign(username) + "-" + username, "30d");
}
Actually this is what Secure module does after successful authentication. Check Secure module
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With