Я использую OAuth2 example (с Google) для аутентификации моего веб-приложения Phoenix. Хотя мой сайт требует, чтобы пользователь всегда регистрировался или аутентифицировался. Нет регистрации пользователя или анонимного доступа.Как требовать логина в приложении Elixir Phoenix?
У меня есть часть OAuth2, работающая с использованием Google в качестве поставщика. Хотя я не уверен, как требовать, чтобы пользователь перенаправлялся на страницу входа в систему, если они не вошли в систему.
Может ли кто-нибудь представить пример рабочего кода?
Вот мой web\router.ex
:
defmodule MyApp.Router do
use MyApp.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :assign_current_user
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", MyApp do
pipe_through :browser # Use the default browser stack
get "/users", UserController, :index
get "https://stackoverflow.com/users/:id", UserController, :show
get "/", PageController, :index
end
scope "/auth", MyApp do
pipe_through :browser # Use the default browser stack
get "/:provider", AuthController, :index
get "/:provider/callback", AuthController, :callback
delete "/logout", AuthController, :delete
end
# Other scopes may use custom stacks.
# scope "/api", MyApp do
# pipe_through :api
# end
# Fetch the current user from the session and add it to `conn.assigns`. This
# will allow you to have access to the current user in your views with
# `@current_user`.
defp assign_current_user(conn, _) do
assign(conn, :current_user, get_session(conn, :current_user))
end
end
UPDATE 05/23/2016
Используя часть выбранного ответа ниже, я получил эту работу путем изменения моей web\router.ex
:
defmodule MyApp.Router do
use MyApp.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :assign_current_user
end
pipeline :authenticated do
plug :authenticate
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", MyApp do
pipe_through [:browser, :authenticated] # Use the default browser stack
get "/", PageController, :index
resources "/users", UserController, only: [:index, :show, :new, :create]
end
scope "/auth", MyApp do
pipe_through :browser # Use the default browser stack
get "/:provider", AuthController, :index
get "/:provider/callback", AuthController, :callback
delete "/logout", AuthController, :delete
end
# Other scopes may use custom stacks.
# scope "/api", MyApp do
# pipe_through :api
# end
# Fetch the current user from the session and add it to `conn.assigns`. This
# will allow you to have access to the current user in your views with
# `@current_user`.
defp assign_current_user(conn, _params) do
assign(conn, :current_user, get_session(conn, :current_user))
end
defp authenticate(conn, _params) do
if conn.assigns.current_user do
conn
else
conn
|> put_flash(:error, "You must be logged in.")
|> redirect(to: "/auth/google")
|> halt()
end
end
end