jack
jack

Reputation: 454

Devise authentication token

In devise, how do i access the authentication token of a user. When an ajax call initiates a user session, I need to able to get an authentication token out of the user. I've tried to do things like adding :token_authenticable in my model

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

also adding attributes of names like :authentication_token, and :authenticity_token to attr_accessible. But every time i try to call authentication_token or this authenticity_token out of the user with user.auth ... i get errors like this

NoMethodError: undefined method `authentication_token' for #<User:0x000001016b1140>

Sorry, i need to get this token to send with my ajax calls, (the last project i worked on i had an incredibly BOOTLEG solution where devise was nearly torn out of the application, i just need this damn token)

Upvotes: 7

Views: 10016

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

This is fixed by including the :token_authenticatable module into the devise user model:

devise :token_authenticatable, ...

Also the method is defined on the class itself, not an instance. Call it as User.authentication_token.

See here: https://github.com/plataformatec/devise/blob/master/lib/devise/models/token_authenticatable.rb#L61-63.

Upvotes: 8

Related Questions