Ruby on Rails
Friday, May 19, 2017
Hello,
-- I've the question about the "devise_token_auth" gem, authentication in Ruby on Rails application (API) and deploying it to Heroku.
Introduction
What I did:
1/ Created an --api --database=postgresql app
2/ Added some required gems like
gem 'devise_token_auth'
gem 'omniauth'
gem 'rack-cors', :require => 'rack/cors'
3/ Added CORS
4/ Added User model
rails generate devise_token_auth:install User auth
and modified my class definition
class User < ActiveRecord::Base
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable,
:omniauthable
include DeviseTokenAuth::Concerns::User
end
5/ Added the following statement into the seed.rb file
User.create(email: 'user@example.com', nickname: 'UOne', name: 'User One', password: "monkey67")
6/ Initialized git repo, pushed it, initialized heroku app and pushed this app using the commands:
heroku signin
heroku keys:add
heroku create
git push heroku master
heroku run rake db:create
heroku run rake db:migrate
heroku run rake db:seed
So, this was the first deploy. Everything works as expected. When I send a request using RESTclient in firefox:
Request type: POST on "https://myapp.herokuapp.com/auth/sign_in"
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Body: email=user%40example.com&password=monkey67
I got the response 200(OK) with the token. It is OK.
Further steps are:
7/ Added a new user in seed.rb file, the new file looks as follows
User.create(email: 'user@example.com', nickname: 'UOne', name: 'User One', password: "monkey67")
User.create(email: 'user2@example.com', nickname: 'UTwo', name: 'User two', password: "monkey69")
8/ Pushed the changes to git repo and then to heroku
...
git push
git push heroku master
heroku run rake db:seed
...
The problem
When I send a request using RESTclient in firefox for the new user:
Request type: POST on "https://hereismyapp.herokuapp.com/auth/sign_in"
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Body: email=user2%40example.com&password=monkey69
I got the response 200(OK) with the token. It is OK.
The part of heroku logs for the second user
> Started POST "/auth/sign_in" for 94.254.189.86 at 2017-05-18 21:09:33 +0000
> User Load (1.1ms) SELECT "users".* FROM "users" WHERE (email = 'user2@example.com' AND provider='email') ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
> Processing by DeviseTokenAuth::SessionsController#create as */*
> Parameters: {"email"=>"user2@example.com", "password"=>"[FILTERED]"}
...
> Completed 200 OK in 492ms (Views: 0.5ms | ActiveRecord: 36.8ms)
> at=info method=POST path="/auth/sign_in" host=hereismyapp.herokuapp.com request_id=df397a4c-ea1e-4c90-8d40-f2382dfab1f8 fwd="94.254.189.86" > dyno=web.1 connect=3ms service=500ms status=200 bytes=580 protocol=https
However, when I do the same thing for the old user that was in my seed.rb file from the beginning the response code is 500 (Internal server error).
I would like to say that both records are in my db on heroku.
The part of heroku logs for the first user
> Started POST "/auth/sign_in" for 94.254.189.86 at 2017-05-18 21:09:43 +0000
> User Load (1.0ms) SELECT "users".* FROM "users" WHERE (email = 'user@example.com' AND provider='email') ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
> IndexError (string not matched):
> at=info method=POST path="/auth/sign_in" host=hereismyapp.herokuapp.com request_id=f2e57933-1220-4e55-9554-2496638a1c9f fwd="94.254.189.86" dyno=web.1 connect=1ms service=162ms status=500 bytes=189 protocol=https
How to resolve this issue?
I had to clean my db on heroku using the commands
heroku pg:reset DATABASE
heroku run rake db:migrate
The main question
Why do I have to clean my database using pg:reset command to make my api work for the all data. Why is that? Is it the correct behaviour?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2661621c-2d10-4a3b-9603-fc3a88579402%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment