2

My question is based on this question: Programmatically User login not working i dont have this clear, to login an user (create an active session) only need use User::Load()? i'm triying make this on my custom module, due to i need an independent login and register form, but dont work. Someone can help me? i want create a user session and let the user navigate for all sections that your role have assigned, but without pass by "user/login" route. Thanks in advance. On resume i want emulate the login controller shipped with drupal 8.

5
  • 1
    User::load() loads information about an account, it does not inject or alter the current session in any way. user_login_finalize() is one of the key functions.
    – Kevin
    Feb 28, 2019 at 20:14
  • 1
    Ok, how i can create a session for an user programatically? can you help me?
    – Jjsg08
    Feb 28, 2019 at 20:16
  • 1
    Ohh!!! ok, i get it, user_login_finalize() open a session for the provided user. Thanksa a lot!!! :D. Please put the comment in an answer for select as the correct answer.
    – Jjsg08
    Feb 28, 2019 at 20:20
  • 3
    Possible duplicate of Programmatically User login not working
    – leymannx
    Mar 2, 2019 at 12:41
  • 1
    Is the same article that i refer on my question, but for novices as me, is not obvious the response in the related topic, due to acepted response says: Delete user_login_finalize($user); and this line of code is just the line that log an user.
    – Jjsg08
    Mar 2, 2019 at 14:57

1 Answer 1

5

User::load() loads information about an account, it does not inject or alter the current session in any way. user_login_finalize() is one of the key functions when doing programatic user logins because it will create and set a session for the current user.

See more: https://api.drupal.org/api/drupal/core%21modules%21user%21user.module/function/user_login_finalize/8.7.x

1
  • 2
    Sorry, forgot to link to it. Updated.
    – Kevin
    Feb 28, 2019 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.