ruby on rails - Creating a token with encryption -
i using rails api backend of iphone app. users need able sign in on rails side creating remember_token, sending app in header, , encrypting token on rails side. on, whenever post request made app, sends token it. things creating photo on app, need find user sending request. this, using listed bellow. problem remember_token being sent phone different 1 being saved rails. example: 1 save phone:cknqijl5ecapewup_px4rw
token saved user on rails: e18f05d5e7260cc70dde4e68c590dde4bd2d2b04
here how create token:
def user.new_remember_token securerandom.urlsafe_base64 end def user.hash(token) digest::sha1.hexdigest(token.to_s) end
when user created or signed in:
remember_token = user.new_remember_token @user.update_attribute(:remember_token, user.hash(remember_token)) response.headers['x-token'] = remember_token
finding user through token when needed:
@key = request.headers['x-token'] @user = user.find_by_remember_token(@key)
error message:
nomethoderror (undefined method `id' nil:nilclass): app/models/school.rb:21:in `follow!' app/controllers/api/v1/schools_controller.rb:21:in `create'
the issue calling user.hash
in update_attributes
method, setting response header remember_token
variable. need hash remember_token
, store in remember_token
variable before update_attributes
call.
remember_token = user.hash(user.new_remember_token) @user.update_attribute(:remember_token, remember_token) response.headers['x-token'] = remember_token
Comments
Post a Comment