lynndylanhurley / lynndylanhurley/devise_token_auth
Login the user manually and return the token on headers by using a POST endpoint (facebook provider)
- Dominant language
- Ruby
- Stars
- 3.6k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
When posting issues, please include the following information to speed up the troubleshooting process:
* **Version**: 1.1.4
* **Routes**: are you using some crazy namespace, scope, or constraint? no
* **Gems**: ActiveAdmin, but does not have to do with the issue
* **Custom Overrides**: what have you done in terms of [custom controller overrides](https://github.com/lynndylanhurley/devise_token_auth/#custom-controller-overrides)? None
* **Custom Frontend**: are you using [ng-token-auth](https://github.com/lynndylanhurley/ng-token-auth), [jToker](https://github.com/lynndylanhurley/j-toker), [Angular2-Token](https://github.com/neroniaky/angular2-token), or something else? None
Hello, this is an open question that I had to write a custom controller to log in to my user using the access token. Imagine the scenario:
1. You have a mobile app (react-native) to log in with Facebook
2. You use the mobile tools to do the OAuth login and get the access-token and don't need to use the omniauth regular routes to get the token
3. You need a way to log in the user on the Rails API using omniauth-facebook, but you already have the access-token.
This scenario is becoming very common since you don't need to use the whole omniauth flow on mobile apps.
Basically, it would be very nice to have a `POST` route that you can just pass the `access-token`, and the application would log in the user.
I had to create a custom facebook controller and use some of the internals of https://github.com/lynndylanhurley/devise_token_auth.
I am not sure if this question is more related to this repo or not. Anyway, here's you can find a way to log in just by using the access token by adding a field to the user model:
hacky by fancy way:
```ruby
class FacebookController < ApplicationController
skip_before_action :verify_authenticity_token
def create
facebook_data = HTTParty.get("https://graph.facebook.com/me", query: {
access_token: params[:access_token],
fields: 'email,name,id'
}).parsed_response
@user = User.find_by(facebook_id: facebook_data["id"])
if @user
@auth_headers = @user.create_new_auth_token
response.set_header('access-token', @auth_headers['access-token'])
response.set_header('token-type', @auth_headers['token-type'])
response.set_header('client', @auth_headers['client'])
response.set_header('expiry', @auth_headers['expiry'])
render json: @user
else
render json: { error: "user not found", status: :not_found }
end
end
end
```
Open question: Is it possible to POST to any route nowadays using a specific param with the access token and the gem would already login the use and return the headers, etc?
Contributor guide
Research direction
Start by reviewing the custom FacebookController example and the devise_token_auth internals it uses, along with the existing OAuth routes. Define whether a POST endpoint accepting a Facebook access token belongs in this repository and what authentication headers and user states it must return; no repository file or test is named in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, react-native, ruby
- Domain
- api, authentication, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100