Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically generate Devise Authenticity Token on sign up

I currently have an app that uses regular Devise authentication for logging in and out, but uses Devise's token_authenticatable module to create a unique token that is used when submitting data to the app from a bookmarklet. Each user gets a unique bookmarklet with their authenticity token baked right into the javascript. However, the way Devise is currently set up, the user has no auth token by default. They have to go to the /edit/users page, press "Generate Token" and then they have one in the db.

I need a way to tell Devise to automatically generate an auth token for each user at sign up.

I'm a newbie, and I'm not sure where to find the Devise controllers to edit this, and even if I could find them, I'm not entirely sure what I would do. Any help is welcome! Thanks.

like image 251
Bon Champion Avatar asked Mar 05 '12 22:03

Bon Champion


1 Answers

You can use the helper ensure_authentication_token of Devise::Models::TokenAuthenticatable module.

So you User class might be like this:

class User < ActiveRecord::Base
  devise :database_authenticatable

  before_save :ensure_authentication_token
end
like image 127
Trung Lê Avatar answered Oct 13 '22 00:10

Trung Lê