使用 Phoenix 进行身份验证

Phoenix 是一个基于 Elixir 的 Web 开发框架,它使用服务器端模型视图控制器 (MVC) 设计模式。它的实现(组件和构造)可与其他知名框架相媲美,包括 Ruby on Rails、Node 的 Express.js 和 Python 的 Django。Phoenix 利用众所周知的 Web 开发结构,修复 Discord Krisp 无法正常工作的 9 种解决方案如果您已经在该领域工作过,则可以轻松学习。

在本教程中,我们将学习如何使用生成器向如何修复请等待当前程序卸载错误 Phoenix 应用程序添加身份验证。该 生成器因其灵活性、网站和网络应用程序的区别在哪里?安全性及其对 Elixir 最佳实践的实施而被广泛使用。修复 Google Chrome 状态无效图像错误包含在 Phoenix 框架中,是向 Phoenix 应用程序添加身份验证的推荐方法。phx.gen.authphx.gen.authPhx.gen.auth

我们将涵盖:

  • 开始使用 Phoenix

  • 创建 Phoenix 应用程序

  • 更新数据库凭据

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

  • 身份验证phx.gen.auth

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

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

先决条件

  • 长生不老药

  • 数据库

要在您的系统上安装 Elixir,修复 Kodi 无法连接到网络服务器请访问Elixir 文档并按照适用于您的操作系统的安装说明进行操作。

开始使用 Phoenix

运行以下命令在您的系统上安装 Phoenix:

mix archive.install hex phx_new

上面的命令指示Mix安装 Phoenix 框架。如何在 Windows 10 中查看主板型号与其他主流编程语言类似,Elixir 提供了 Mix 构建工具来帮助编译、创建和测试应用程序,以及安装、更新和卸载依赖项。如果没有像 Mix 这样的工具,修复 Windows 10 映射驱动器未显示在程序中使用 Elixir 是不可想象的。

创建 Phoenix 应用程序

通过在命令中提供标志,修复 Skype 在来电时不响铃可以在没有数据库的情况下创建 Phoenix 应用程序,但是因为我们需要数据持久性,所以我们将使用数据库。本课选择的数据库是 PostgreSQL,这是 Phoenix 应用程序的标准选择。修复 Yahoo Mail 停止显示图片--no-ectophx.new

要安装 PostgreSQL,请参阅文档,或者您可以访问本教程以使用 Docker 安装和运行:

mix phx.new todo_app

这里,todo是我们希望创建的应用程序的名称。修复暴雪 Battle.NET 错误代码 2当出现提示时,选择。获取依赖项后,修复 Windows 10 或 11 上的系统服务异常我们还有几个步骤来启动和运行我们的应用程序。Fetch and install dependencies? [Yn]

首先,我们要进入我们新建的app目录:

cd todo_app

更新数据库凭据

接下来,我们必须配置我们的数据库,修复 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

猜你喜欢

转载自blog.csdn.net/weixin_47967031/article/details/130172277