flmel

flmel

Pow Controllers and LiveView

Hello Everyone,
I’m trying to make Pow register form validation trough LiveView which I more or less succeeded at. However, upon user creation pow normally would log the user trough the plug:
from pow docs

 def create(conn, %{"user" => user_params}) do
    conn
    |> Pow.Plug.create_user(user_params)
    |> case do
      {:ok, user, conn} ->
        conn
        |> put_flash(:info, "Welcome!")
        |> redirect(to: Routes.page_path(conn, :index))
       ...
   end

My LiveView event currently looks like this (i had to create a custom function create_user in order to escape the conn needed for the Plug but now Im trying to figure out how to log in the user on creation…

def handle_event("save", %{"user" => user_params}, socket) do
    case Accounts.create_user(user_params) do
      {:ok, _user} ->
        # Form is valid
        {:stop,
         socket
         |> put_flash(:info, "user created")  
         |> redirect(to: Routes.live_path(socket, MyApp.SearchLive))}
..

So basically is there a way to pass the conn trough my liveview or i shall copy/write the entire functionality myself.

@danschultzer somewhere in the forum you’ve mentioned that you would put a demo with liveview did you ever get around to do that ?

Thanks for your time

Marked As Solved

chrismccord

chrismccord

Creator of Phoenix

This is one of the usecases I have highlighted all along for where LV shines. Imagine a registration form where you necessarily want to improve UX because it directly increases conversions. You want to give the user feedback as soon as possible when they are trying to sign up, so realtime validations are essential, but LV allows you to add it without bringing int a client side solution and all the baggage that comes with it. The outstanding issues with a full LiveView signin or registration form is the cookie session writing on login or sign up. This can’t happen over websockts so folks have two options.

  • Use phx-change on the form, but allow the regular form post to hit a controller that performs put_session
  • Use LV for phx-change and phx-submit, and perform a redirect to a session controller, with a signed token which is used to call put_session to write the session

So LV is can be used here without any of the mentioned security issues. You are of course free to use regular controllers if that’s a better fit :slight_smile:

13
Post #8

Also Liked

danschultzer

danschultzer

Pow Core Team

You can’t set cookies in a WebSocket. You have to pass the session id value to the client and set up some custom JS client side that will set it as a cookie. There’s some security issues too. I would recommend that you only deal with auth in HTTP.

Haven’t gotten to it yet. I’m trying out a Pow implementation with Phoenix.Socket though, hopefully can take what I learn from that and add it to the Pow docs/code :slight_smile:

andreaseriksson

andreaseriksson

Thanks.

I can’t say for sure its a bad idea to have a live validation on the registration (or login) form but it seems overly complicated and could be a security risk. Especially if you check for if email is already used. I just can’t see the upside other than learning how it works behind the scenes.

Schultzer

Schultzer

I don’t want to ruin your learning experience but take a look at the pow/lib/pow/phoenix/controllers/registration_controller.ex at main · pow-auth/pow · GitHub

I would also go through this issue Instructions for WebSocket usage (e.g. Phoenix Channels and LiveView) · Issue #271 · pow-auth/pow · GitHub

The last thing you want to figure out is the differences between session and token based authentications and how to handle session based authentication in a LiveView socket.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement