Skip to content
This repository has been archived by the owner on Dec 22, 2020. It is now read-only.

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

Open
stochastic-thread opened this issue Jun 19, 2015 · 4 comments

Comments

@stochastic-thread
Copy link

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?

@Anonyfox
Copy link

+1 on this

@acconrad
Copy link

acconrad commented Oct 9, 2015

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.

@tensiondriven
Copy link

+1

@tensiondriven
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants