Skip to content

Commit 230e8f2

Browse files
rchampourlierscrogson
authored andcommitted
Phoenix 1.2.1 and OAuth2 0.7 updates (#16)
* Phoenix and OAuth2 update - Updated dependencies - Made some changes by applying the same setup as a default Phoenix project - Updated the OAuth2 calls where the API changed for Github integration **TODO** - Fix/update Facebook and Google integrations * Updated calls to OAuth2 lib - For Google and Facebook - Not tested
1 parent fc2e391 commit 230e8f2

File tree

16 files changed

+167
-71
lines changed

16 files changed

+167
-71
lines changed

.gitignore

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
.envrc
2-
3-
# Mix artifacts
1+
# App artifacts
42
/_build
3+
/db
54
/deps
65
/*.ez
76

8-
# Generate on crash by the VM
7+
# Generated on crash by the VM
98
erl_crash.dump
109

1110
# Static artifacts
1211
/node_modules
1312

14-
# Since we are building js and css from web/static,
15-
# we ignore priv/static/{css,js}. You may want to
16-
# comment this depending on your deployment strategy.
17-
/priv/static/css
18-
/priv/static/js
13+
# Since we are building assets from web/static,
14+
# we ignore priv/static. You may want to comment
15+
# this depending on your deployment strategy.
16+
/priv/static/
17+
18+
# The config/prod.secret.exs file by default contains sensitive
19+
# data and you should not commit it into version control.
20+
#
21+
# Alternatively, you may comment the line below and commit the
22+
# secrets file as long as you replace its contents by environment
23+
# variables.
24+
/config/prod.secret.exs

brunch-config.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ exports.config = {
55
joinTo: "js/app.js"
66

77
// To use a separate vendor.js bundle, specify two files path
8-
// https://github.com/brunch/brunch/blob/stable/docs/config.md#files
8+
// http://brunch.io/docs/config#-files-
99
// joinTo: {
1010
// "js/app.js": /^(web\/static\/js)/,
1111
// "js/vendor.js": /^(web\/static\/vendor)|(deps)/
1212
// }
1313
//
1414
// To change the order of concatenation of files, explicitly mention here
15-
// https://github.com/brunch/brunch/tree/master/docs#concatenation
1615
// order: {
1716
// before: [
1817
// "web/static/vendor/js/jquery-2.1.1.js",
@@ -21,7 +20,10 @@ exports.config = {
2120
// }
2221
},
2322
stylesheets: {
24-
joinTo: "css/app.css"
23+
joinTo: "css/app.css",
24+
order: {
25+
after: ["web/static/css/app.css"] // concat app.css last
26+
}
2527
},
2628
templates: {
2729
joinTo: "js/app.js"
@@ -39,8 +41,6 @@ exports.config = {
3941
paths: {
4042
// Dependencies and current project directories to watch
4143
watched: [
42-
"deps/phoenix/web/static",
43-
"deps/phoenix_html/web/static",
4444
"web/static",
4545
"test/static"
4646
],

config/config.exs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use Mix.Config
77

88
# Configures the namespace used by Phoenix generators
99
config :oauth2_example,
10-
app_namespace: OAuth2Example
10+
app_namespace: OAuth2Example,
11+
ecto_repos: [OAuth2Example.Repo]
1112

1213
# Configures the endpoint
1314
config :oauth2_example, OAuth2Example.Endpoint,
1415
url: [host: "localhost"],
15-
root: Path.dirname(__DIR__),
1616
secret_key_base: "zWoDIUN+sA1X958CxqY38CE42MrplMBZtnCOs+d1DoE05sJ++vfrrAU8OKm3JPL+",
1717
render_errors: [accepts: ~w(html json)],
1818
pubsub: [name: OAuth2Example.PubSub,
@@ -23,10 +23,10 @@ config :logger, :console,
2323
format: "$time $metadata[$level] $message\n",
2424
metadata: [:request_id]
2525

26-
# Configure phoenix generators
27-
config :phoenix, :generators,
28-
migration: true,
29-
binary_id: false
26+
# Configures Elixir's Logger
27+
config :logger, :console,
28+
format: "$time $metadata[$level] $message\n",
29+
metadata: [:request_id]
3030

3131
# Import environment specific config. This must remain at the bottom
3232
# of this file so it overrides the configuration defined above.

config/dev.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ config :oauth2_example, OAuth2Example.Endpoint,
1010
http: [port: 4000],
1111
debug_errors: true,
1212
code_reloader: true,
13-
cache_static_lookup: false,
1413
check_origin: false,
15-
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin"]]
14+
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
15+
cd: Path.expand("../", __DIR__)]]
1616

1717
# Watch static and templates for browser reloading.
1818
config :oauth2_example, OAuth2Example.Endpoint,
1919
live_reload: [
2020
patterns: [
2121
~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
22+
~r{priv/gettext/.*(po)$},
2223
~r{web/views/.*(ex)$},
2324
~r{web/templates/.*(eex)$}
2425
]

mix.exs

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ defmodule OAuth2Example.Mixfile do
3535
#
3636
# Type `mix help deps` for examples and options
3737
defp deps do
38-
[{:phoenix, "~> 1.0.3"},
39-
{:phoenix_ecto, "~> 1.1"},
38+
[{:phoenix, "~> 1.2.1"},
39+
{:phoenix_pubsub, "~> 1.0"},
40+
{:phoenix_ecto, "~> 3.0"},
4041
{:postgrex, ">= 0.0.0"},
41-
{:phoenix_html, "~> 2.2"},
42+
{:phoenix_html, "~> 2.6"},
4243
{:phoenix_live_reload, "~> 1.0", only: :dev},
44+
{:gettext, "~> 0.11"},
4345
{:cowboy, "~> 1.0"},
44-
{:oauth2, "~> 0.5"}]
46+
{:oauth2, "~> 0.7"}]
4547
end
4648
end
47-

mix.lock

+26-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
%{"cowboy": {:hex, :cowboy, "1.0.3"},
2-
"cowlib": {:hex, :cowlib, "1.0.1"},
3-
"decimal": {:hex, :decimal, "1.1.0"},
4-
"ecto": {:hex, :ecto, "1.0.2"},
5-
"fs": {:hex, :fs, "0.9.2"},
6-
"hackney": {:hex, :hackney, "1.3.2"},
1+
%{"certifi": {:hex, :certifi, "0.4.0", "a7966efb868b179023618d29a407548f70c52466bf1849b9e8ebd0e34b7ea11f", [:rebar3], []},
2+
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
3+
"cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:rebar, :make], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]},
4+
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []},
5+
"db_connection": {:hex, :db_connection, "1.0.0-rc.5", "1d9ab6e01387bdf2de7a16c56866971f7c2f75aea7c69cae2a0346e4b537ae0d", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}]},
6+
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
7+
"ecto": {:hex, :ecto, "2.0.4", "03fd3b9aa508b1383eb38c00ac389953ed22af53811aa2e504975a3e814a8d97", [:mix], [{:db_connection, "~> 1.0-rc.2", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]},
8+
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
9+
"gettext": {:hex, :gettext, "0.11.0", "80c1dd42d270482418fa158ec5ba073d2980e3718bacad86f3d4ad71d5667679", [:mix], []},
10+
"hackney": {:hex, :hackney, "1.6.1", "ddd22d42db2b50e6a155439c8811b8f6df61a4395de10509714ad2751c6da817", [:rebar3], [{:certifi, "0.4.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.0", [hex: :ssl_verify_fun, optional: false]}]},
711
"httpoison": {:hex, :httpoison, "0.7.4"},
8-
"idna": {:hex, :idna, "1.0.2"},
12+
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
13+
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
14+
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
15+
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
916
"mimetype_parser": {:hex, :mimetype_parser, "0.1.0"},
10-
"oauth2": {:hex, :oauth2, "0.5.0"},
11-
"phoenix": {:hex, :phoenix, "1.0.3"},
12-
"phoenix_ecto": {:hex, :phoenix_ecto, "1.2.0"},
13-
"phoenix_html": {:hex, :phoenix_html, "2.2.0"},
14-
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.0"},
15-
"plug": {:hex, :plug, "1.0.2"},
16-
"poison": {:hex, :poison, "1.5.0"},
17-
"poolboy": {:hex, :poolboy, "1.5.1"},
18-
"postgrex": {:hex, :postgrex, "0.9.1"},
19-
"ranch": {:hex, :ranch, "1.1.0"},
17+
"oauth2": {:hex, :oauth2, "0.7.0", "950502a56078faed531fd2ed039d8709e047032671d0a53c2d5cceedd3c69155", [:mix], [{:hackney, "~> 1.6", [hex: :hackney, optional: false]}]},
18+
"phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
19+
"phoenix_ecto": {:hex, :phoenix_ecto, "3.0.1", "42eb486ef732cf209d0a353e791806721f33ff40beab0a86f02070a5649ed00a", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.6", [hex: :phoenix_html, optional: true]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]},
20+
"phoenix_html": {:hex, :phoenix_html, "2.6.2", "944a5e581b0d899e4f4c838a69503ebd05300fe35ba228a74439e6253e10e0c0", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]},
21+
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.5", "829218c4152ba1e9848e2bf8e161fcde6b4ec679a516259442561d21fde68d0b", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]},
22+
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.0", "c31af4be22afeeebfaf246592778c8c840e5a1ddc7ca87610c41ccfb160c2c57", [:mix], []},
23+
"plug": {:hex, :plug, "1.2.0", "496bef96634a49d7803ab2671482f0c5ce9ce0b7b9bc25bc0ae8e09859dd2004", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
24+
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
25+
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
26+
"postgrex": {:hex, :postgrex, "0.11.2", "139755c1359d3c5c6d6e8b1ea72556d39e2746f61c6ddfb442813c91f53487e8", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
27+
"ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []},
28+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0", "edee20847c42e379bf91261db474ffbe373f8acb56e9079acb6038d4e0bf414f", [:rebar, :make], []},
2029
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5"}}

package.json

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
{
2-
"repository": {
2+
"repository": {},
3+
"license": "MIT",
4+
"scripts": {
5+
"deploy": "brunch build --production",
6+
"watch": "brunch watch --stdin"
37
},
48
"dependencies": {
5-
"brunch": "^1.8.5",
6-
"babel-brunch": "^5.1.1",
7-
"clean-css-brunch": ">= 1.0 < 1.8",
8-
"css-brunch": ">= 1.0 < 1.8",
9-
"javascript-brunch": ">= 1.0 < 1.8",
10-
"uglify-js-brunch": ">= 1.0 < 1.8"
9+
"phoenix": "file:deps/phoenix",
10+
"phoenix_html": "file:deps/phoenix_html"
11+
},
12+
"devDependencies": {
13+
"babel-brunch": "~6.0.0",
14+
"brunch": "2.7.4",
15+
"clean-css-brunch": "~2.0.0",
16+
"css-brunch": "~2.0.0",
17+
"javascript-brunch": "~2.0.0",
18+
"uglify-js-brunch": "~2.0.1"
1119
}
1220
}

priv/repo/migrations/.gitkeep

Whitespace-only changes.

web/controllers/auth_controller.ex

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ defmodule OAuth2Example.AuthController do
2424
"""
2525
def callback(conn, %{"provider" => provider, "code" => code}) do
2626
# Exchange an auth code for an access token
27-
token = get_token!(provider, code)
27+
client = get_token!(provider, code)
2828

2929
# Request the user's data with the access token
30-
user = get_user!(provider, token)
30+
user = get_user!(provider, client)
3131

3232
# Store the user in the session under `:current_user` and redirect to /.
3333
# In most cases, we'd probably just store the user's ID that can be used
@@ -38,7 +38,7 @@ defmodule OAuth2Example.AuthController do
3838
# the access token as well.
3939
conn
4040
|> put_session(:current_user, user)
41-
|> put_session(:access_token, token.access_token)
41+
|> put_session(:access_token, client.token.access_token)
4242
|> redirect(to: "/")
4343
end
4444

@@ -52,16 +52,16 @@ defmodule OAuth2Example.AuthController do
5252
defp get_token!("facebook", code), do: Facebook.get_token!(code: code)
5353
defp get_token!(_, _), do: raise "No matching provider available"
5454

55-
defp get_user!("github", token) do
56-
{:ok, %{body: user}} = OAuth2.AccessToken.get(token, "/user")
55+
defp get_user!("github", client) do
56+
%{body: user} = OAuth2.Client.get!(client, "/user")
5757
%{name: user["name"], avatar: user["avatar_url"]}
5858
end
59-
defp get_user!("google", token) do
60-
{:ok, %{body: user}} = OAuth2.AccessToken.get(token, "https://www.googleapis.com/plus/v1/people/me/openIdConnect")
59+
defp get_user!("google", client) do
60+
{:ok, %{body: user}} = OAuth2.Client.get!(client, "https://www.googleapis.com/plus/v1/people/me/openIdConnect")
6161
%{name: user["name"], avatar: user["picture"]}
6262
end
63-
defp get_user!("facebook", token) do
64-
{:ok, %{body: user}} = OAuth2.AccessToken.get(token, "/me", fields: "id,name")
63+
defp get_user!("facebook", client) do
64+
{:ok, %{body: user}} = OAuth2.Client.get!(client, "/me", fields: "id,name")
6565
%{name: user["name"], avatar: "https://graph.facebook.com/#{user["id"]}/picture"}
6666
end
6767
end

web/gettext.ex

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule OAuth2Example.Gettext do
2+
@moduledoc """
3+
A module providing Internationalization with a gettext-based API.
4+
5+
By using [Gettext](https://hexdocs.pm/gettext),
6+
your module gains a set of macros for translations, for example:
7+
8+
import Trelloto.Gettext
9+
10+
# Simple translation
11+
gettext "Here is the string to translate"
12+
13+
# Plural translation
14+
ngettext "Here is the string to translate",
15+
"Here are the strings to translate",
16+
3
17+
18+
# Domain-based translation
19+
dgettext "errors", "Here is the error message to translate"
20+
21+
See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
22+
"""
23+
use Gettext, otp_app: :oauth2_example
24+
end

web/oauth/github.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule GitHub do
2626
end
2727

2828
def get_token!(params \\ [], headers \\ []) do
29-
OAuth2.Client.get_token!(client(), params)
29+
OAuth2.Client.get_token!(client(), Keyword.merge(params, client_secret: client().client_secret))
3030
end
3131

3232
# Strategy Callbacks

web/static/js/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
// If you no longer want to use a dependency, remember
1313
// to also remove its path from "config.paths.watched".
14-
import "deps/phoenix_html/web/static/js/phoenix_html"
14+
import "phoenix_html"
1515

1616
// Import local files
1717
//

web/static/js/socket.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
// To use Phoenix channels, the first step is to import Socket
55
// and connect at the socket path in "lib/my_app/endpoint.ex":
6-
import {Socket} from "deps/phoenix/web/static/js/phoenix"
6+
import {Socket} from "phoenix"
77

8-
let socket = new Socket("/socket")
8+
let socket = new Socket("/socket", {params: {token: window.userToken}})
99

1010
// When you connect, you'll often need to authenticate the client.
1111
// For example, imagine you have an authentication plug, `MyAuth`,
@@ -51,7 +51,7 @@ let socket = new Socket("/socket")
5151
// Finally, pass the token on connect as below. Or remove it
5252
// from connect if you don't care about authentication.
5353

54-
socket.connect({token: window.userToken})
54+
socket.connect()
5555

5656
// Now that you are connected, you can join channels with a topic:
5757
let channel = socket.channel("topic:subtopic", {})

web/templates/layout/app.html.eex

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
<% end %>
6969
</div>
7070

71-
<div class="container-fluid">
72-
<%= @inner %>
73-
</div>
71+
<main role="main">
72+
<%= render @view_module, @view_template, assigns %>
73+
</main>
7474
<script src="<%= static_path(@conn, "/js/app.js") %>"</script>
7575
<script>require("/web/static/app")</script>
7676
</body>

web/views/error_helpers.ex

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule OAuth2Example.ErrorHelpers do
2+
@moduledoc """
3+
Conveniences for translating and building error messages.
4+
"""
5+
6+
use Phoenix.HTML
7+
8+
@doc """
9+
Generates tag for inlined form input errors.
10+
"""
11+
def error_tag(form, field) do
12+
if error = form.errors[field] do
13+
content_tag :span, translate_error(error), class: "help-block"
14+
end
15+
end
16+
17+
@doc """
18+
Translates an error message using gettext.
19+
"""
20+
def translate_error({msg, opts}) do
21+
# Because error messages were defined within Ecto, we must
22+
# call the Gettext module passing our Gettext backend. We
23+
# also use the "errors" domain as translations are placed
24+
# in the errors.po file.
25+
# Ecto will pass the :count keyword if the error message is
26+
# meant to be pluralized.
27+
# On your own code and templates, depending on whether you
28+
# need the message to be pluralized or not, this could be
29+
# written simply as:
30+
#
31+
# dngettext "errors", "1 file", "%{count} files", count
32+
# dgettext "errors", "is invalid"
33+
#
34+
if count = opts[:count] do
35+
Gettext.dngettext(OAuth2Example.Gettext, "errors", msg, msg, count, opts)
36+
else
37+
Gettext.dgettext(OAuth2Example.Gettext, "errors", msg, opts)
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)