Authenticating with Phoenix

Phoenix is ​​an Elixir-based web development framework that uses the server-side Model View Controller (MVC) design pattern. Its implementation (components and structure) is comparable to other well-known frameworks, including Ruby on Rails, Node's Express.js, and Python's Django. Phoenix 9 Solutions to Fix Discord Krisp Not Working Using Well-Known Web Development Structures It's easy to learn if you've already worked in the field.

In this tutorial we will learn how to add authentication to the How to fix Please wait for the current program to uninstall error Phoenix application using the generator . What is the difference between the builder for its flexibility, website and web application? It is widely used for security and its implementation of Elixir best practices. Fixing Google Chrome state invalid image error is included in the Phoenix framework and is the recommended way to add authentication to Phoenix applications. phx.gen.authphx.gen.authPhx.gen.auth

We will cover:

  • Get started with Phoenix

  • Create a Phoenix application

  • Update database credentials

  • Generate MVCphx.gen.html for HTML resources using

  • authentication-phx.gen.auth

  • Explore the file phx.gen.auth created by

  • Add authentication/tasks to our route

prerequisites

  • Elixir of life

  • database

To install Elixir on your system, fix Kodi cannot connect to webserver visit the Elixir documentation and follow the installation instructions for your operating system.

Get started with Phoenix

Run the following command to install Phoenix on your system:

mix archive.install hex phx_new

The above command instructs Mix to install the Phoenix framework. How to View Motherboard Models in Windows 10 Similar to other mainstream programming languages, Elixir provides the Mix build tool to help compile, create, and test applications, as well as install, update, and uninstall dependencies. Fix Windows 10 mapped drive not showing up using Elixir in the program is unthinkable without a tool like Mix .

Create a Phoenix application

Fix Skype not ringing for incoming calls by providing the flag in the command The Phoenix application can be created without a database, but since we need data persistence we will use a database. The database of choice for this lesson is PostgreSQL, which is the standard choice for Phoenix applications. Fix Yahoo Mail stops displaying images --no-ectophx.new

To install PostgreSQL, refer to the documentation, or you can visit this tutorial to install and run with Docker:

mix phx.new todo_app

Here, todo is the name of the application we wish to create. Fix Blizzard Battle.NET Error Code 2 When prompted, select Yes . After getting dependencies, fixing system service exceptions on Windows 10 or 11 We have a few more steps to get our application up and running. Fetch and install dependencies? [Yn]

First, we need to enter our newly created app directory:

cd todo_app

Update database credentials

接下来,我们必须配置我们的数据库,修复 NVIDIA GeForce 在 Windows 10 上不兼容找到并 更新以下行以反映我们的 Postgres 数据库和:config/dev.exs usernamepassword

username: "postgres",

password: "postgres",

然后,运行以下命令来创建数据库:

mix ecto.create

最后,修复您无法发送消息的 Microsoft Teams 错误我们可以使用以下命令运行我们的服务器:

mix phx.server

访问以查看您的 Phoenix 应用程序:http://localhost:4000

使用 为 HTML 资源生成 MVCphx.gen.html

Phoenix 为我们提供了一种为 HTML 资源生成控制器、修复 Halo Infinite No Ping 到数据中心检测到的错误视图和上下文的方法:

mix phx.gen.html Todo Task tasks name:string completed:boolean

我们为生成器提供了几个参数,第一个是上下文模块,然后是模式模块的名称,最后是模式的复数名称(用作模式表名称的名称)。修复 Roblox 错误代码 524和是将在表中创建的两个字段。phx.gen.htmlname:stringcompleted:booleantasks

查看终端,我们可以看到Phoenix提供的指令:

首先,我们必须将 复制到我们的文件中:resources "/tasks", TaskControllerlib/todo_app_web/router.ex

defmodule TodoAppWeb.Router do

use TodoAppWeb, :router

...

scope "/", TodoAppWeb do

pipe_through(:browser)

get("/", PageController, :index)

resources "/tasks", TaskController #updated

end

...

代表resources不同的 HTTP 方法——Phoenix 提供了resources.

然后,我们必须更新我们的数据库以更新由以下人员所做的更改:phx.gen.html

mix ecto.migrate

最后,要访问生成的任务路由,修复 Netflix 无法在 Virgin Media 上运行的 17 种方法请访问:http://localhost:4000/tasks

在上面的截图中,我添加了几个未完成的任务。随意做同样的事情并尝试一下应用程序。Phoenix 为这些生成的资源提供开箱即用的 CRUD 功能,因此无需任何额外的代码,我们就可以从数据库中创建、更新和删除任务。

身份验证phx.gen.auth

Phoenix 提供了一种非常简单的方法来使用生成器向我们的应用程序添加身份验证。让我们看看如何做到这一点。phx.gen.auth

在您的终端中,运行:

mix phx.gen.auth Accounts User users

使用此命令,Accounts使用模式模块创建上下文。最后一个参数是模式模块的复数形式,它创建数据库表名和路由助手。生成器与生成器类似,不同之处在于它不接受要添加到模式的额外字段列表,并且它生成更多的上下文函数。Accounts.Usermix phx.gen.authmix phx.gen.htm``l

查看我们的终端,我们可以看到 Phoenix 生成了几个文件并更新了现有文件。稍后我们将查看其中的一些文件,尤其是文件,但让我们快速运行几个命令。lib/todo_app_web/router.ex

在您的终端中,运行:

mix deps.get

此命令更新您的应用程序依赖项。我们还需要更新我们的数据库以反映生成器所做的更改。phx.gen.auth

现在,运行这个命令:

mix ecto.migrate

如果我们检查在浏览器中运行的应用程序,我们可以看到生成器生成的Register和:Log inphx.gen.auth

在应用程序上注册,您应该会看到“用户创建成功”的弹出消息。

探索由创建的文件phx.gen.auth

让我们看一下由 . 创建的一些文件。phx.gen.auth

路由器文件已经存在,生成器只是向其中添加了几行代码:lib/todo_app_web/router.exphx.gen.auth

...

## Authentication routes

scope "/", TodoAppWeb do

pipe_through [:browser, :redirect_if_user_is_authenticated]

get "/users/register", UserRegistrationController, :new

post "/users/register", UserRegistrationController, :create

get "/users/log_in", UserSessionController, :new

post "/users/log_in", UserSessionController, :create

get "/users/reset_password", UserResetPasswordController, :new

post "/users/reset_password", UserResetPasswordController, :create

get "/users/reset_password/:token", UserResetPasswordController, :edit

put "/users/reset_password/:token", UserResetPasswordController, :update

end

scope "/", TodoAppWeb do

pipe_through [:browser, :require_authenticated_user]

get "/users/settings", UserSettingsController, :edit

put "/users/settings", UserSettingsController, :update

get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email

end

scope "/", TodoAppWeb do

pipe_through [:browser]

delete "/users/log_out", UserSessionController, :delete

get "/users/confirm", UserConfirmationController, :new

post "/users/confirm", UserConfirmationController, :create

get "/users/confirm/:token", UserConfirmationController, :edit

post "/users/confirm/:token", UserConfirmationController, :update

end

...

和称为插头。遵循他们规定的规则后出现的路线。:require_authenticated_user:redirect_if_user_is_authenticated

插件允许我们保护路由不被未经身份验证的用户访问,而插件允许我们防止经过身份验证的用户访问某些路由。:require_authenticated_user:redirect_if_user_is_authenticated

和插头都来自控制器::require_authenticated_user:redirect_if_user_is_authenticatedlib\todo_app_web\controllers\user_auth.ex

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

向我们的路由添加身份验证/tasks

目前,我们的路由不需要用户在访问之前进行身份验证。如果需要,我们可以将路由分成不同的 HTTP 方法。因此,只有一个访问所有方法的行:/tasks/tasksresource

resources "/tasks", TaskController

是相同的:

get "/tasks", TaskController, :index

post "/tasks", TaskController, :create

get "/tasks/new", TaskController, :new

get "/tasks/:id", TaskController, :show

get "/tasks/:id/edit", TaskController, :edit

put "/tasks/:id/update", TaskController, :update

delete "/tasks/:id/delete", TaskController, :delete

为了使这些路由需要用户身份验证,我们所要做的就是把插件放在resources后面::require_authenticated_user

scope "/", TodoAppWeb do

pipe_through [:browser, :require_authenticated_user]

...

resources "/tasks", TaskController

end

注销应用程序,然后尝试访问路由。您将被重定向到登录路由,并显示一条错误消息:http:localhost:4000/tasks

您只能在登录时访问这些路线。仅此而已!我们已经在 Phoenix 应用程序中实现了身份验证。

结论

在本教程中,我们学习了如何通过使用生成器为我们的应用程序生成身份验证来在 Phoenix 应用程序中实现身份验证,并了解它为我们提供的中间件。希望在本教程的帮助下,您可以使用 Phoenix 取得更多成就。phx.gen.auth

Guess you like

Origin blog.csdn.net/weixin_47967031/article/details/130172277