implement github oauth and display user's name and avatar from it

This commit is contained in:
2021-11-12 20:20:40 -05:00
parent dfaa328af4
commit 540519222d
24 changed files with 226 additions and 181 deletions

View File

@@ -0,0 +1,41 @@
defmodule EntenduWeb.AuthController do
@moduledoc """
Auth controller responsible for handling Ueberauth responses
"""
use EntenduWeb, :controller
plug Ueberauth
alias Ueberauth.Strategy.Helpers
alias Entendu.UserFromAuth
def delete(conn, _params) do
conn
|> put_flash(:info, "You have been logged out!")
|> clear_session()
|> redirect(to: "/")
end
def callback(%{assigns: %{ueberauth_failure: _fails}} = conn, _params) do
conn
|> put_flash(:error, "Failed to authenticate.")
|> redirect(to: "/")
end
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
case UserFromAuth.find_or_create(auth) do
{:ok, user} ->
conn
|> put_flash(:info, "Successfully authenticated.")
|> put_session(:current_user, user)
|> configure_session(renew: true)
|> redirect(to: "/")
{:error, reason} ->
conn
|> put_flash(:error, reason)
|> redirect(to: "/")
end
end
end

View File

@@ -0,0 +1,11 @@
defmodule EntenduWeb.LinkController do
@moduledoc """
Controller responsible for managing links responses
"""
use EntenduWeb, :controller
def just(conn, _params) do
render(conn, "just.html")
end
end

View File

@@ -0,0 +1,11 @@
defmodule EntenduWeb.PageController do
@moduledoc """
Static page controller
"""
use EntenduWeb, :controller
def index(conn, _params) do
render(conn, "index.html", current_user: get_session(conn, :current_user))
end
end

View File

@@ -1,13 +0,0 @@
defmodule EntenduWeb.LinkLive.For do
use EntenduWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(:step, 1)}
end
def handle_event("next_step", _, %{ assigns: %{ step: step } } = socket) do
socket = socket |> assign(:step, step + 1)
{:noreply, socket}
end
end

View File

@@ -1,4 +0,0 @@
<h1>Create Link</h1>
<%= live_component @socket, EntenduWeb.LinkLive.For.Step1Component, id: "step1", test: "test" %>
On Step: <%= @step %>

View File

@@ -1,46 +0,0 @@
defmodule EntenduWeb.LinkLive.Index do
use EntenduWeb, :live_view
alias Entendu.Links
alias Entendu.Links.Link
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :links, list_links())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Link")
|> assign(:link, Links.get_link!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Link")
|> assign(:link, %Link{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Links")
|> assign(:link, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
link = Links.get_link!(id)
{:ok, _} = Links.delete_link(link)
{:noreply, assign(socket, :links, list_links())}
end
defp list_links do
Links.list_links()
end
end

View File

@@ -1,37 +0,0 @@
<h1>Listing Links</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal @socket, EntenduWeb.LinkLive.FormComponent,
id: @link.id || :new,
title: @page_title,
action: @live_action,
link: @link,
return_to: Routes.link_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Expires</th>
<th>Burn after reading</th>
<th></th>
</tr>
</thead>
<tbody id="links">
<%= for link <- @links do %>
<tr id="link-<%= link.id %>">
<td><%= link.expires %></td>
<td><%= link.burn_after_reading %></td>
<td>
<span><%= live_redirect "Show", to: Routes.link_show_path(@socket, :show, link) %></span>
<span><%= live_patch "Edit", to: Routes.link_index_path(@socket, :edit, link) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: link.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Link", to: Routes.link_index_path(@socket, :new) %></span>

View File

@@ -1,4 +0,0 @@
defmodule EntenduWeb.LinkLive.For.Step1Component do
use EntenduWeb, :live_component
end

View File

@@ -1,3 +0,0 @@
This is step 1. <%= @test %>
<button phx-click="next_step">Next Step</button>

View File

@@ -1,39 +0,0 @@
defmodule EntenduWeb.PageLive do
use EntenduWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, query: "", results: %{})}
end
@impl true
def handle_event("suggest", %{"q" => query}, socket) do
{:noreply, assign(socket, results: search(query), query: query)}
end
@impl true
def handle_event("search", %{"q" => query}, socket) do
case search(query) do
%{^query => vsn} ->
{:noreply, redirect(socket, external: "https://hexdocs.pm/#{query}/#{vsn}")}
_ ->
{:noreply,
socket
|> put_flash(:error, "No dependencies found matching \"#{query}\"")
|> assign(results: %{}, query: query)}
end
end
defp search(query) do
if not EntenduWeb.Endpoint.config(:code_reloader) do
raise "action disabled when not in development"
end
for {app, desc, vsn} <- Application.started_applications(),
app = to_string(app),
String.starts_with?(app, query) and not List.starts_with?(desc, ~c"ERTS"),
into: %{},
do: {app, vsn}
end
end

View File

@@ -1,18 +0,0 @@
<section class="phx-hero">
<h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
<p>Peace of mind from prototype to production</p>
<form phx-change="suggest" phx-submit="search">
<input type="text" name="q" value="<%= @query %>" placeholder="Live dependency search" list="results" autocomplete="off"/>
<datalist id="results">
<%= for {app, _vsn} <- @results do %>
<option value="<%= app %>"><%= app %></option>
<% end %>
</datalist>
<button type="submit" phx-disable-with="Searching...">Go to Hexdocs</button>
</form>
</section>
<section class="row">
<a href="/links">Links</a>
</section>

View File

@@ -17,8 +17,16 @@ defmodule EntenduWeb.Router do
scope "/", EntenduWeb do
pipe_through :browser
live "/", PageLive, :index
live "/for", LinkLive.For
get "/", PageController, :index
get "/just", LinkController, :just
end
scope "/auth", EntenduWeb do
pipe_through :browser
get "/:provider", AuthController, :request
get "/:provider/callback", AuthController, :callback
delete "/logout", AuthController, :delete
end
# Other scopes may use custom stacks.

View File

@@ -0,0 +1,6 @@
<section class="phx-hero">
<h2>Create a secret!</h2>
<p>There will be a form here</p>
<p>And a file input here</p>
<p>Then a next button</p>
</section>

View File

@@ -0,0 +1,49 @@
<section class="phx-hero">
<h1>Überauth + Phoenix Example</h1>
<p>
This is an application to show an example of how to wire up
<a href="https://github.com/ueberauth/ueberauth">Überauth</a> with
<a href="https://github.com/phoenixframework/phoenix">Phoenix</a>.
</p>
<%= if @current_user do %>
<h2>Welcome, <%= @current_user.name %>!</h2>
<div>
<img src="<%= @current_user.avatar %>" />
</div>
<%= link "Logout", to: Routes.auth_path(@conn, :delete), method: "delete", class: "button" %>
<br>
<% else %>
<ul style="list-style: none">
<li>
<a class="button" href="<%= Routes.auth_path(@conn, :request, "github") %>">
<i class="fa fa-github"></i>
Sign in with GitHub
</a>
</li>
<li>
<a class="button" href="<%= Routes.auth_path(@conn, :request, "facebook") %>">
<i class="fa fa-facebook"></i>
Sign in with Facebook
</a>
</li>
<li>
<a class="button" href="<%= Routes.auth_path(@conn, :request, "google") %>">
<i class="fa fa-google"></i>
Sign in with Google
</a>
</li>
<li>
<a class="button" href="<%= Routes.auth_path(@conn, :request, "slack") %>">
<i class="fa fa-slack"></i>
Sign in with Slack
</a>
</li>
<li>
<a class="button" href="<%= Routes.auth_path(@conn, :request, "twitter") %>">
<i class="fa fa-twitter"></i>
Sign in with Twitter
</a>
</li>
</ul>
<% end %>
</section>

View File

@@ -0,0 +1,3 @@
defmodule EntenduWeb.LinkView do
use EntenduWeb, :view
end

View File

@@ -0,0 +1,3 @@
defmodule EntenduWeb.PageView do
use EntenduWeb, :view
end