You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi all.
I'm having trouble implementing a custom method in a controller to login an user.
In the User model I have, as requested in the doc, added include DeviseTokenAuth::Concerns::User, which should allow me to use methods like create_new_auth_token.
But, in my custom login method - which handles user which logged in via Facebook SDK in a mobile client, passing me back the access_token - :
def login_user_facebook
@graph = Koala::Facebook::API.new(facebook_params[:access_token])
@user_profile = @graph.get_object("me?fields=id,email")
if @user = User.where(provider: "facebook", uid: @user_profile["id"])
sign_in(:user, @user)
new_auth_headers = @user.create_new_auth_token
//posting the answer there
else
//posting the error there
end
end
I get the following error: undefined method 'create_new_auth_token' for #<User::ActiveRecord_Relation:0x00000005451f50>.
Is it a bug causing this? Am I implementing the sign_in wrongly?
The text was updated successfully, but these errors were encountered:
Ok I think I figured it out. @lynndylanhurley feel free to close this issue, or leave it open, as it may help some people using your gem is a scoped route.
Basically, I was making two mistakes here:
by using User.where I was not getting back an user, so that's why create_new_auth_token wasn't working. Needed to use User.find
Since my user resource is scoped into API::V1:: in the routes, I needed to call sign_in by scoping it too.
So the working code is:
@user = User.find_by(provider: "facebook", uid: @user_profile["id"])
if @user != nil
sign_in(:api_v1_user, @user)
new_auth_header = @user.create_new_auth_token()
# update response with the header that will be required by the next request
response.headers.merge!(new_auth_header)
render json: {
user: @user,
}, status: 200
else
render json: {
error: "There is no user via facebook with this name"
}, status: 400
end
Hi all.
I'm having trouble implementing a custom method in a controller to login an user.
In the User model I have, as requested in the doc, added
include DeviseTokenAuth::Concerns::User
, which should allow me to use methods likecreate_new_auth_token
.But, in my custom login method - which handles user which logged in via Facebook SDK in a mobile client, passing me back the access_token - :
I get the following error:
undefined method 'create_new_auth_token' for #<User::ActiveRecord_Relation:0x00000005451f50>
.Is it a bug causing this? Am I implementing the sign_in wrongly?
The text was updated successfully, but these errors were encountered: