lynndylanhurley / lynndylanhurley/devise_token_auth

Authentication passthru when using with omniauth 2.0.3

Open
#1,466 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
3.6k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Trying to implement omniauth + google_oauth2 into token auth, with no luck. Am I missing any settings required?
Email authentication at `POST http://localhost:3000/v1/auth/sign_in` works as expected.

I'm concerned if we should access to the OAuth request phase using POST method.

## Version

* devise_token_auth 1.1.5
* omniauth 2.0.3
* rails 6.1.3
* ruby 3.0.0

## Request and response headers

GET request to `http://localhost:3000/v1/auth/google_oauth2` results:

```
Request URL: http://localhost:3000/v1/auth/google_oauth2
Request Method: GET
Status Code: 301 Moved Permanently
Remote Address: [::1]:3000
Referrer Policy: strict-origin-when-cross-origin
```

After redirect:

```
Request URL: http://localhost:3000/omniauth/google_oauth2?namespace_name=v1&resource_class=User
Request Method: GET
Status Code: 404 Not Found
Remote Address: [::1]:3000
Referrer Policy: strict-origin-when-cross-origin
```

With response body being: `Not found. Authentication passthru.`.

## Rails Stacktrace

```
Started GET "/v1/auth/google_oauth2" for ::1 at 2021-02-23 19:33:36 +0900
Started GET "/omniauth/google_oauth2?namespace_name=v1&resource_class=User" for ::1 at 2021-02-23 19:33:36 +0900
Processing by Users::OmniauthCallbacksController#passthru as HTML
Parameters: {"namespace_name"=>"v1", "resource_class"=>"User"}
Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 2)
Completed 404 Not Found in 1ms (Views: 0.7ms | Allocations: 222)
```

## Environmental Info

### Routes

```rb
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
namespace :v1 do
mount_devise_token_auth_for 'User', at: 'auth'
end
end
```

### Gems

```rb
ruby '3.0.0'

gem 'rails', '~> 6.1.0'
gem 'pg', '~> 1.1'
gem 'puma', '~> 5.0'
gem 'jbuilder', '~> 2.7'
gem 'bootsnap', '>= 1.4.4', require: false
gem 'rack-cors'
gem 'devise', git: 'https://github.com/heartcombo/devise.git', branch: 'ca-omniauth-2' # https://github.com/heartcombo/devise/issues/5326
gem 'devise_token_auth'
gem 'omniauth-google-oauth2'
```

### Custom Overrides

```rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
@user = User.from_omniauth(request.env['omniauth.auth'])

if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
sign_in_and_redirect @user, event: :authentication
else
session['devise.google_data'] = request.env['omniauth.auth'].except('extra') # Removing extra as it can overflow some session stores
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
end
```

### Custom Frontend

Just trying with GUI tools.

### Controller

Note that I'm on API mode:

```rb
class ApplicationController < ActionController::API
include DeviseTokenAuth::Concerns::SetUserByToken
end
```

### Model

```rb
class User < ApplicationRecord
include DeviseTokenAuth::Concerns::User

devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:validatable, :confirmable, :trackable, :timeoutable,
:omniauthable, omniauth_providers: %i[google_oauth2]

def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first

# Create user if they don't exist
unless user
user = User.create(
name: data['name'],
email: data['email'],
password: Devise.friendly_token[0,20]
)
end
user
end
end
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the GET request to /v1/auth/google_oauth2 using the shown routes and versions, then trace the redirect into Users::OmniauthCallbacksController#passthru. Read the custom callbacks controller, ApplicationController, and User model to determine why the OAuth request ends in the 404 passthru response; done means the OAuth flow reaches the intended callback behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, authentication, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.