Skip to content

Commit 1367e38

Browse files
author
Rohan Pujari
committed
Handle client side flow for google auth.
1 parent 0e4048d commit 1367e38

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

lib/ueberauth/strategy/google.ex

+15-9
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ defmodule Ueberauth.Strategy.Google do
3131
@doc """
3232
Handles the callback from Google.
3333
"""
34-
def handle_callback!(%Plug.Conn{params: %{"code" => code}} = conn) do
35-
opts = [redirect_uri: callback_url(conn)]
36-
token = Ueberauth.Strategy.Google.OAuth.get_token!([code: code], opts)
37-
38-
if token.access_token == nil do
39-
set_errors!(conn, [error(token.other_params["error"], token.other_params["error_description"])])
40-
else
41-
fetch_user(conn, token)
34+
def handle_callback!(%Plug.Conn{params: params} = conn) do
35+
case params do
36+
%{"code" => code} ->
37+
opts = [redirect_uri: callback_url(conn)]
38+
token = Ueberauth.Strategy.Google.OAuth.get_token!([code: code], opts)
39+
if token.access_token == nil do
40+
set_errors!(conn, [error(token.other_params["error"], token.other_params["error_description"])])
41+
else
42+
fetch_user(conn, token)
43+
end
44+
%{"token" => token} ->
45+
fetch_user(conn, OAuth2.AccessToken.new(token))
46+
_ ->
47+
set_errors!(conn, [error("missing_code_or_token", "No code or token received")])
4248
end
4349
end
4450

@@ -124,7 +130,7 @@ defmodule Ueberauth.Strategy.Google do
124130
resp = Ueberauth.Strategy.Google.OAuth.get(token, path)
125131

126132
case resp do
127-
{:ok, %OAuth2.Response{status_code: 401, body: _body}} ->
133+
{:error, %OAuth2.Response{status_code: 401, body: _body}} ->
128134
set_errors!(conn, [error("token", "unauthorized")])
129135
{:ok, %OAuth2.Response{status_code: status_code, body: user}} when status_code in 200..399 ->
130136
put_private(conn, :google_user, user)

0 commit comments

Comments
 (0)