Skip to content

Commit

Permalink
create auth_controller.ex for #54
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Nov 17, 2022
1 parent f91f1f3 commit 1b18ff3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 56 deletions.
28 changes: 28 additions & 0 deletions lib/chat/release.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Chat.Release do
@moduledoc """
Used for executing DB release tasks when run in production without Mix
installed.
"""
@app :chat

def migrate do
load_app()

for repo <- repos() do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end
end

def rollback(repo, version) do
load_app()
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end

defp repos do
Application.fetch_env!(@app, :ecto_repos)
end

defp load_app do
Application.load(@app)
end
end
14 changes: 14 additions & 0 deletions lib/chat_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ChatWeb.AuthController do
use ChatWeb, :controller

def login(conn, _params) do
redirect(conn, external: AuthPlug.get_auth_url(conn, "/"))
end

def logout(conn, _params) do
conn
|> AuthPlug.logout()
|> put_status(302)
|> redirect(to: "/")
end
end
9 changes: 0 additions & 9 deletions lib/chat_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,4 @@ defmodule ChatWeb.PageController do
def index(conn, _params) do
render(conn, "index.html")
end

# def auth_page(conn, _params) do
# render(conn, "auth.html")
# end

# see: github.com/dwyl/ping
def ping(conn, params) do
Ping.render_pixel(conn, params)
end
end
52 changes: 5 additions & 47 deletions lib/chat_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,14 @@ defmodule ChatWeb.Router do
plug :accepts, ["json"]
end

scope "/", ChatWeb do
pipe_through :browser

get "/", PageController, :index
get "/ping", PageController, :ping
end


pipeline :auth, do: plug(AuthPlug, %{auth_url: "https://auth.dwyl.com/"})
pipeline :authOptional, do: plug(AuthPlugOptional)

scope "/", ChatWeb do
pipe_through :browser
pipe_through :auth

get "/auth", PageController, :auth_page
end


# Other scopes may use custom stacks.
# scope "/api", ChatWeb do
# pipe_through :api
# end

# Enables LiveDashboard only for development
#
# If you want to use the LiveDashboard in production, you should put
# it behind authentication and allow only admins to access it.
# If your application does not have an admins-only section yet,
# you can use Plug.BasicAuth to set up some basic authentication
# as long as you are also using SSL (which you should anyway).
if Mix.env() in [:dev, :test] do
import Phoenix.LiveDashboard.Router
pipe_through [:browser, :authOptional]

scope "/" do
pipe_through :browser

live_dashboard "/dashboard", metrics: ChatWeb.Telemetry
end
get "/", PageController, :index
get "/login", AuthController, :login
get "/logout", AuthController, :logout
end

# Enables the Swoosh mailbox preview in development.
#
# Note that preview only shows emails that were sent by the same
# node running the Phoenix server.
#if Mix.env() == :dev do
# scope "/dev" do
# pipe_through :browser
#
# forward "/mailbox", Plug.Swoosh.MailboxPreview
# end
#end
end

0 comments on commit 1b18ff3

Please sign in to comment.