Reputation: 9427
I am using devise
and simple_token_authentication
for my JSON API. Once I have an email and token, according to simple_token_authentication, I should be authenticated and thus be able to access a controller action. However, when I try the following curl command:
curl -i -H "Accept: application/json" -H "Content-type: application/json" -H "X-User-Email: [email protected]" -H "X-User-Token: NFHCVtZdjy2zwT_hTezU" -X GET http://localhost:3001/users/11.json
I get the following error:
HTTP/1.1 401 Unauthorized X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff Content-Type: application/json; charset=utf-8 Cache-Control: no-cache Set-Cookie:
_my_app_session=Q0VTT1NNNGx1K2hoK3pSMXBZNFhzaG1BSXd5cGg3WWhkTmVtekEvSkc4Q2FqSkF4anZMYVc1dENWNzJkRUhwcFN2VG8yb0xzeGk4cjFtSm5tWHIxb1E9PS0tZ3V6cXZRL0ZKR3hxSVh1NWxjeENLZz09--145a421cac94ab6be992df6b0f534968ba751231; path=/; HttpOnly X-Request-Id: 8425dd4c-d13e-465b-997e-847288ddbdf0 X-Runtime: 0.027902 Transfer-Encoding: chunked
{"error":"You have to confirm your email address before continuing."}Mac-mini-3:my_app donato$
Now checked my user model out:
User.first.attributes.slice("confirmation_token", "confirmed_at", "confirmation_sent_at", "unconfirmed_email")
=> {"confirmation_token"=>"Lvh4XzW5wAeXmHVFEFQk", "confirmed_at"=>nil, "confirmation_sent_at"=>Fri, 10 Mar 2017 22:15:20 UTC +00:00, "unconfirmed_email"=>nil}
It seems like it is already confirmed?
Actually, once I did this:
user.confirmed_at = Time.now
user.save!
Then that error went away. Devise has a method skip_confirmation!
. But when I create a user from my mobile app, devise handles everything. How could I hook into devise to invoke this method?
Upvotes: 1
Views: 3730
Reputation: 5942
Did you enable :confirmable
with Devise
? because this is connected to the :confirmable
module in User.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
The .skip_confirmation! method needs to be called before saving of the user.
user = User.new
user.skip_confirmation!
user.save
This :confirmable module adds some fields in the user model and in the users table, if you correctly fill those fields you will be able to skip the confirmation.
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
You can ask me more details about the confirmable module as I configured it for my application.
Upvotes: 0