Class: Guts::SessionsController

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

Overview

Sessions controller

Instance Method Summary collapse

Methods included from SessionsHelper

#current_user, #log_in, #log_out, #logged_in?

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



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

def create
  user = User.find_by(email: params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
     user
    redirect_to users_path
  else
    flash.now[:notice] = "Invalid login credentials"
    render :new
  end
end

#destroyObject

Destroys a user session



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
35
# 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



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

def forgot_token
  user = User.find_by(email: params[:session][:email].downcase)
  if user
    user.password_token = Digest::SHA1.hexdigest("#{Time.now.to_s}#{rand(100)}")[0,8]
    user.save
    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[:notice] = "Invalid email address"
    render :forgot
  end
end

#newObject

Creation of a new session (login page)



7
8
# 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
62
63
64
# File 'app/controllers/guts/sessions_controller.rb', line 54

def reset_password
  new_password = Digest::SHA1.hexdigest("#{Time.now.to_s}#{rand(100)}")[0,8]
  
  user                = User.find_by(password_token: params[:token])
  user.password_token = nil
  user.password       = new_password
  user.save
  
  flash[:notice] = "Your new password is now: #{new_password}. You may now login with it."
  redirect_to new_session_path
end