I have an app that uses devise for login. I have a login form in a web app that authenticates a user to the database and simply returns the @user hash as a json string.
The goal is to get the users authenticate the user and retrieve their authentication_token for future use in the app to prevent the user from having to continually log in.
The problem is that I can’t get authentication_token to be included in the returned json.
My user.rb Model
attr_accessible :authentication_token, :email, :password, :password_confirmation, :remember_me, :bio, :confirmed, :deposit, :email, :fri25, :mon21, :name, :paid, :picture, :sat26, :sun20, :sun27, :thur24, :tue22, :wed23
clearly includes the authentication_token symbol.
Then in the session controller I have a custom action called newApi which runs a simple authentication method and responds with the @user hash as json.
def newapi
@user = User.authenticate(params[:email],params[:password])
respond_to do |format|
format.json { render json: @user }
end
eend
Yet no matter what I do the authentication token is not included with the json response. Am I missing something obvious? Why is it not included?
I ended up solving the problem myself by using jbuilder.
Essentially you just remove the
render json: @usercall and use a jbuilder template.in newapi.json.jbuilder
Review their docs if you got this route as there are plenty of It does everything I needed and more and is the only way we do api returns now. If you’ve not used it before go check it out! it make API endpoints amazingly simple.
Nick Knudson’s answer is also valid if you want to roll your own setup.