Created
June 29, 2015 15:35
-
-
Save YuriFontella/97fa55e7609a34338e55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Session | |
include ActiveModel::Model | |
attr_accessor :email, :password | |
validates :email, :presence => { :message => 'Entre com o seu e-mail' } | |
validates :password, :presence => { :message => 'Coloque a sua senha' } | |
def initialize(attr={}) | |
@email = attr[:email] | |
@password = attr[:password] | |
end | |
def authenticate! | |
return false if @email.blank? || @password.blank? | |
user = User.find_by(email: @email) | |
if user && user.authenticate(@password) | |
session[:id] = user.id | |
else | |
errors.add(:invalid, 'Usuário ou senha incorretos') | |
false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment