-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
authorization controller setup for OAuth
- Loading branch information
Showing
7 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,6 @@ dist/ | |
|
||
# generated elm file | ||
web/static/js/main.js | ||
|
||
# local setup files | ||
local.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule Tracker2x2.AuthController do | ||
use Tracker2x2.Web, :controller | ||
|
||
def index(conn, %{"provider" => provider}) do | ||
redirect conn, external: authorize_url!(provider) | ||
end | ||
|
||
def callback(conn, %{"provider" => provider, "code" => code} = params) do | ||
client = get_token!(provider, code) | ||
user = get_user!(provider, client) | ||
|
||
conn | ||
|> put_session(:current_user, user) | ||
|> put_session(:access_token, client.token.access_token) | ||
|> redirect(to: "/") | ||
end | ||
|
||
defp authorize_url!("google") do | ||
Google.authorize_url!(scope: "https://www.googleapis.com/auth/userinfo.email") | ||
end | ||
|
||
defp authorize_url!("github") do | ||
GitHub.authorize_url! | ||
end | ||
|
||
defp authorize_url!(url) do | ||
raise "No matching provider for #{url} in authorize_url!" | ||
end | ||
|
||
defp get_token!("google", code) do | ||
IO.puts "in get_token!" | ||
Google.get_token!(code: code) | ||
end | ||
|
||
defp get_token!("github", code) do | ||
GitHub.get_token!(code: code) | ||
end | ||
|
||
defp get_token!(provider, code) do | ||
raise "No matching provider for #{provider} with code #{code} in get_token!" | ||
end | ||
|
||
defp get_user!("google", client) do | ||
IO.puts "in google callback" | ||
IO.inspect client | ||
user_url = "https://www.googleapis.com/plus/v1/people/me/openIdConnect" | ||
%{body: user} = OAuth2.Client.get!(client, user_url) | ||
%{name: user["name"]} | ||
end | ||
|
||
defp get_user!("github", client) do | ||
IO.puts "in github callback" | ||
IO.inspect client | ||
%{body: user} = OAuth2.Client.get!(client, "https://api.github.com/user") | ||
%{name: user["name"], avatar: user["avatar_url"]} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
<div id="elm-main"></div> | ||
<%= if @current_user do %> | ||
<%# <div id="elm-main"></div> %> | ||
<div><%= @current_user.name %></div> | ||
<% else %> | ||
<%= link to: auth_path(@conn, :index, "google"), class: "btn btn-primary" do %> | ||
Sign in with Google | ||
<% end %> | ||
<%= link to: auth_path(@conn, :index, "github"), class: "btn btn-primary" do %> | ||
Sign in with github | ||
<% end %> | ||
<% end %> |