passport
passport copied to clipboard
On registration, how to make it so a session automatically is entered?
I've noticed that this intermittently occurs, and I'd be happy to submit a pull request with the functionality.
I was wondering if you could give me some input as to what would be a good way to add this?
+1 on this
Actually should be pretty easy, I think this would work if you change the register/1 function in Passport.RegistrationManager? Haven't tested it:
def register(conn, params) do
changeset = user_model.changeset(user_model.__struct__, params)
changeset = changeset
|> validate_format(:email, ~r/@/)
|> update_change(:email, &String.downcase/1)
|> set_hashed_password
|> unique_constraint(:email)
repo.insert(changeset)
Passport.SessionManager.login(conn, params)
end
you just add the conn as another argument to the function, and then after the changeset is successfully inserted, you call Passport.SessionManager.login/2 to actually log you in upon registering successfully.
+1
I added this line:
put_session(:user_id, user.id)
to web/controllers/registration_controller.ex, so my code for RegistrationController.create is:
def create(conn, %{"user" => registration_params}) do
changeset = User.registration_changeset(%User{}, registration_params)
case Repo.insert(changeset) do
{:ok, user} ->
conn
|> put_session(:user_id, user.id) # sign in user
|> put_flash(:info, "Account created! Click login above.")
|> redirect(to: page_path(conn, :index))
{:error, changeset} ->
conn
|> render(:new, changeset: changeset)
end
end