Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 290
jack Avatar asked Dec 14 '11 02:12

jack


People also ask

What is authentication token in REST API?

Users of the REST API can authenticate by providing a user ID and password to the REST API login resource with the HTTP POST method. An LTPA token is generated that enables the user to authenticate future requests. This LTPA token has the prefix LtpaToken2 .


1 Answers

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.

like image 187
Ryan Bigg Avatar answered Oct 18 '22 06:10

Ryan Bigg