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
    Commented Feb 28, 2019 at 20:14
  • 1
    Ok, how i can create a session for an user programatically? can you help me?
    – Jjsg08
    Commented 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
    Commented Feb 28, 2019 at 20:20
  • 3
    Possible duplicate of Programmatically User login not working
    – leymannx
    Commented 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
    Commented 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
    Commented Feb 28, 2019 at 21:53

Your Answer

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

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