mardi 11 avril 2017

How to set up Token Authentication properly in Rails?

I am trying to make an API for a project. I followed instructions from this video - https://www.youtube.com/watch?v=lgdUqtw4weg&t=165s

Basically in the video, I make a token column for the user model. And set up the controller so I can use a post method. But when i run the POST. I get the error saying

{"error":"You need to sign in or sign up before continuing."}

I think Devise is interfering with the POST and sees that the user has not logged in when trying to visit non public pages.

How do I get past this and get the token ?

Here is my api controller.

class Api::V1::UserSerializedController < ApplicationController
protect_from_forgery except: :index
respond_to :json


def create 


    user = User.where(email: params[:email]).first

    if user.valid_password?(params[:encrypted_password])
        render json: user.as_json(only: [:email, :authentication_token]),status: :created

    else 
        head(:unauthorized)
    end

end 

def show

end
end

Aucun commentaire:

Enregistrer un commentaire