How to use play frameworks Secure module to login a user after that user has been created

陌路散爱 提交于 2020-02-21 10:23:55

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7103544/how-to-use-play-frameworks-secure-module-to-login-a-user-after-that-user-has-bee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!