cvkmohan

cvkmohan

The phx.gen.auth module has been super helpful for both plug-and-play authentication in the application as well as learning through code.
This is what I found in user_auth.ex in the controller folder -

@doc """
  Used for routes that require the user to be authenticated.

  If you want to enforce the user email is confirmed before
  they use the application at all, here would be a good place.
  """
  def require_authenticated_user(conn, _opts) do
    if conn.assigns[:current_user] do
      conn
    else
      conn
      |> put_flash(:error, "You must log in to access this page.")
      |> maybe_store_return_to()
      |> redirect(to: Routes.user_session_path(conn, :new))
      |> halt()
    end
  end

Look at how beautifully it is documented that - this is a good place if you want to allow only confirmed users to login.
The above function is a plug - and - is using conn which is not available in LiveView. So, modifying the code here would mean it would work only for regular views. For a LiveView, should we duplicate the code? Or is there any best practice available?

First 2 of 2 Posts Switch mode

llama

llama

Finally a question (I think) I know the answer to!

The answer is yes, you have to put any verification or initialization you want to do to the socket into the mount function. Alternatively, you can use the on_mount function to bundle up things you want done. More at link.

cvkmohan

cvkmohan OP

Thanks @llama. I have seen this document earlier as well. My concern is - we have to right the same logic twice - once in the plug - and one more time in the on_mount hook.
I thought there might be a way in which you only write it at one place inside the phx.gen.auth generated files. For example in the login function - or - may be get_user_by_password in the accounts context. I wanted to know the best way or what is the general practice.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement