Class: Guts::SessionsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Guts::SessionsController
- Defined in:
- app/controllers/guts/sessions_controller.rb
Overview
Sessions controller
Instance Method Summary collapse
-
#create ⇒ Object
Checks the users session through post.
-
#destroy ⇒ Object
Destroys a user session.
-
#forgot ⇒ Object
Forgot password page.
-
#forgot_token ⇒ Object
Sends the user a new token by email to reset their password.
-
#new ⇒ Object
Creation of a new session (login page).
-
#reset_password ⇒ Object
Resets the user’s password.
Instance Method Details
#create ⇒ Object
Note:
It will redirect to Guts::UsersController if successful and it will redirect back to #new if not
Checks the users session through post
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]) log_in user redirect_to home_path else flash.now[:alert] = 'Invalid login credentials' render :new end end |
#destroy ⇒ Object
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 |
#forgot ⇒ Object
Forgot password page
34 |
# File 'app/controllers/guts/sessions_controller.rb', line 34 def forgot; end |
#forgot_token ⇒ Object
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 |
#new ⇒ Object
Creation of a new session (login page)
7 |
# File 'app/controllers/guts/sessions_controller.rb', line 7 def new; end |
#reset_password ⇒ Object
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 |