0

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
user2775042
  • 509
  • 2
  • 6
  • 21

1 Answers1

0

You are inheriting ApplicationController and i'm guessing you have authenticate_user! before action set there.

You'd have to either change the parent class to something like ApiController (i'd prefer this)

OR

skip this action for your Api::V1::UserSerializedController

Alfie
  • 2,706
  • 1
  • 14
  • 28