def create
if sign_in_user
sign_in_successful
else
sign_in_not_successful
end
end
def user
@user ||= User.find_by_email(params[:session][:email].downcase)
end
def sign_in_user
return false if user.nil? || !user.authenticate(params[:session][:password])
user.connect
sign_in user
true
end
def sign_in_succesful
flash[:attacher] = attacher(user)
flash[:success] = 'Welcome back to IdleCampus!'
redirect_to home_path
end
def sign_in_not_succesful
flash[:error] = 'Invalid email/password combination'
redirect_to signin_path
end
It can be useful to do a class like this :
class Authenticator
attr_accessor :user
def initialize(user: user)
@user = user
end
def authenticate
return false if user.nil? || !user.authenticate(params[:session][:password])
user.connect
sign_in user
true
end
end
And call it like this :
def create
if Authenticator.new(user: user).authenticate
sign_in_successful
else
sign_in_not_successful
end
end
What you think?
An additional question : Why do you need an authenticate method, a connect method and a sign_in method which seems to do the same things.