在凤凰城混合 phx.gen.auth您可以轻松创建身份验证系统并将其合并到现有系统中。在本文中,我想看看创建了什么样的身份验证系统以及如何使用它。
混合 phx.gen.auth 文档

由于我在互联网早期运行博客系统,所以在尝试使用 PHP、Nodejs、Meteor、Django、AWS JavaScript 等的 Web 应用程序时,我总是创建一个身份验证系统。当然,前阵子凤凰也是一样。这就是为什么我要衡量语言和框架的易用性。那是现在混合 phx.gen.auth能够用一个命令来完成它有点令人印象深刻。

【相关文章】
Phoenix Authenticator - 混合 phx.gen.auth
Phoenix LiveView 基本设置 - 混合 phx.gen.schema
Phoenix 1.6 的基本机制 - mix phx.gen.html
Elixir/Phoenix 简单认证 auth_plug

1. 准备

创建一个项目。

 mix phx.new auth_test
cd auth_test

发出以下命令:帐户上下文模块什么时候Accounts.用户模式模块,最后是模式模块的复数形式用户表将被创建。此外,还生成了认证系统所需的一组文件。

mix phx.gen.auth Accounts User users

使用以下命令设置生成的文件。

mix deps.get
mix ecto.setup

准备工作现已完成。

2. 将页面合并到您的身份验证系统中

phx.gen.auth将新生成的页面添加到生成的认证系统中路由器.ex内置并查看身份验证系统如何工作。路由器.ex就好像插头被创建并添加。

  • fetch_current_user- 如果可能,获取当前用户信息
  • require_authenticated_user- 使用 fetch_current_user 获取的用户必须存在并经过身份验证
  • redirect_if_user_is_authenticated- 页面不适用于经过身份验证的用户

每个定义都是user_auth.ex在可以确认。

lib/auth_test_web/controllers/user_auth.ex
  ---
  @doc """
  Authenticates the user by looking into the session
  and remember me token.
  """
  def fetch_current_user(conn, _opts) do
    {user_token, conn} = ensure_user_token(conn)
    user = user_token && Accounts.get_user_by_session_token(user_token)
    assign(conn, :current_user, user)
  end

  defp ensure_user_token(conn) do
    if user_token = get_session(conn, :user_token) do
      {user_token, conn}
    else
      conn = fetch_cookies(conn, signed: [@remember_me_cookie])

      if user_token = conn.cookies[@remember_me_cookie] do
        {user_token, put_session(conn, :user_token, user_token)}
      else
        {nil, conn}
      end
    end
  end

  @doc """
  Used for routes that require the user to not be authenticated.
  """
  def redirect_if_user_is_authenticated(conn, _opts) do
    if conn.assigns[:current_user] do
      conn
      |> redirect(to: signed_in_path(conn))
      |> halt()
    else
      conn
    end
  end

  @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
  ---

然后新你好.html.heex并创建路由器.ex我们将把它融入到每个部分,看看效果。

lib/auth_test_web/templates/page/hello.html.heex
<h1>Hello World !!!</h1>
<%= if @current_user do %>
  <h2><%= @current_user.email %></h2>
<% else %>
  <h2>Guest</h2>
<% end %>

在进行以下实验之前,请先注册为用户。

2-1. pipe_through :browser

路由器.ex将“/hello”路径插入

lib/auth_test_web/router.ex
  scope "/", AuthTestWeb do
    pipe_through :browser
    ---
    get "/hello", PageController, :hello  # ここに挿入
  end

未登录处于http://localhost:4000/hello访问。显示客人。

Phoenix 認証システム - mix phx.gen.auth

登录处于http://localhost:4000/hello访问。显示您的电子邮件地址。

Phoenix 認証システム - mix phx.gen.auth

2-2. pipe_through [:browser, :redirect_if_user_is_authenticated]

路由器.ex将“/hello”路径插入

lib/auth_test_web/router.ex
  scope "/", AuthTestWeb do
    pipe_through [:browser, :redirect_if_user_is_authenticated]
    ---
    get "/hello", PageController, :hello  # ここに挿入
  end

未登录处于http://localhost:4000/hello访问。显示客人。

Phoenix 認証システム - mix phx.gen.auth

登录处于http://localhost:4000/hello访问。到首页重定向它完成了。

Phoenix 認証システム - mix phx.gen.auth

2-3. pipe_through [:browser, :require_authenticated_user]

路由器.ex将“/hello”路径插入

lib/auth_test_web/router.ex
  scope "/", AuthTestWeb do
    pipe_through [:browser, :require_authenticated_user]
    ---
    get "/hello", PageController, :hello  # ここに挿入
  end

未登录处于http://localhost:4000/hello访问。我被重定向到登录屏幕。

Phoenix 認証システム - mix phx.gen.auth

登录处于http://localhost:4000/hello访问。显示您的电子邮件地址。

Phoenix 認証システム - mix phx.gen.auth

这次结束了。

Phoenix的好处是它以自定义和使用生成的模板为前提,所以我觉得制作日文字符和自定义它们很容易。


原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308631003.html

相关文章:

  • 2022-01-08
  • 2021-07-21
  • 2021-04-29
  • 2022-12-23
  • 2021-11-28
  • 2021-09-26
  • 2021-12-20
  • 2021-05-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2021-05-17
相关资源
相似解决方案