passport icon indicating copy to clipboard operation
passport copied to clipboard

On registration, how to make it so a session automatically is entered?

Open stochastic-thread opened this issue 10 years ago • 4 comments
trafficstars

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?

stochastic-thread avatar Jun 19 '15 14:06 stochastic-thread

+1 on this

Anonyfox avatar Aug 12 '15 17:08 Anonyfox

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.

acconrad avatar Oct 09 '15 05:10 acconrad

+1

tensiondriven avatar May 26 '16 00:05 tensiondriven

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

tensiondriven avatar May 26 '16 01:05 tensiondriven