Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.60 KB | None | 0 0
  1. # please improve this rails action,
  2. # if you'd like to put some code in another file please state so:
  3. # authentication API
  4. def auth
  5.   user = User.find(params[:username])
  6.   if user.check_password(params[:password])
  7.     token = new Token({
  8.         user_id: user.id,
  9.         token_value: [*('a'..'z'),*('0'..'9')].shuffle[0,8].join,
  10.         valid_thru: Time.now + 1.day })
  11.     token.save!
  12.     render json: {user: user, token: token}
  13.   else
  14.     render json: { errors: ["wrong username or password"] }, status: :unauthorized
  15.   end
  16. end
  17.  
  18.  
  19. # token.rb
  20. # token class
  21. class Token < ApplicationRecord
  22.     belongs_to :user
  23. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement