Advertisement
Guest User

Untitled

a guest
May 13th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. require 'bcrypt'
  2.  
  3. class User < ActiveRecord::Base
  4. # users.password_hash in the database is a :string
  5. include BCrypt
  6.  
  7. def password
  8. @password ||= Password.new(password_hash)
  9. end
  10.  
  11. def password=(new_password)
  12. @password = Password.create(new_password)
  13. self.password_hash = @password # 'password_hash' must match your column name
  14. end
  15.  
  16. def authenticate(password)
  17. self.password == password
  18. end
  19. end
  20.  
  21. # Create an account
  22. def create
  23. @user = User.new(params[:user])
  24. @user.password = params[:password]
  25. @user.save!
  26. end
  27.  
  28. # Authenticating a user
  29. def login
  30. @user = User.find_by_email(params[:email])
  31. if @user.password == params[:password]
  32. give_token
  33. else
  34. redirect_to home_url
  35. end
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement