Class: Guts::SessionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/guts/sessions_controller.rb

Overview

Sessions controller

Instance Method Summary collapse

Instance Method Details

#createObject

Note:

It will redirect to Guts::UsersController if successful and it will redirect back to #new if not

Checks the users session through post

See Also:

  • Guts::SessionsHelper#log_in


13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/guts/sessions_controller.rb', line 13

def create
  user = User.find_by(email: params[:session][:email].downcase)

  if user && user.authenticate(params[:session][:password])
     user
    redirect_to home_path
  else
    flash.now[:alert] = 'Invalid login credentials'
    render :new
  end
end

#destroyObject

Destroys a user session

See Also:

  • Guts::SessionsHelper#log_out


27
28
29
30
31
# File 'app/controllers/guts/sessions_controller.rb', line 27

def destroy
  log_out
  flash[:notice] = 'You have been logged out'
  redirect_to new_session_path
end

#forgotObject

Forgot password page



34
# File 'app/controllers/guts/sessions_controller.rb', line 34

def forgot; end

#forgot_tokenObject

Sends the user a new token by email to reset their password



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/guts/sessions_controller.rb', line 37

def forgot_token
  user = User.find_by(email: params[:session][:email].downcase)
  
  if user
    password = Digest::SHA1.hexdigest("#{Time.current}#{rand(100)}")[0, 8]
    user.update_attribute(:password_token, password)
    UserMailer.password_reset(user).deliver_now

    flash[:notice] = 'Your reset link has been sent to your inbox.'
    redirect_to new_session_path
  else
    flash.now[:alert] = 'Invalid email address'
    render :forgot
  end
end

#newObject

Creation of a new session (login page)



7
# File 'app/controllers/guts/sessions_controller.rb', line 7

def new; end

#reset_passwordObject

Resets the user’s password



54
55
56
57
58
59
60
61
# File 'app/controllers/guts/sessions_controller.rb', line 54

def reset_password
  new_password = Digest::SHA1.hexdigest("#{Time.current}#{rand(100)}")[0, 8]
  user         = User.find_by(password_token: params[:token])
  user.update(password_token: nil, password: new_password)

  flash[:notice] = "Your new password is now: #{new_password}. You may now login with it."
  redirect_to new_session_path
end