Sha256: 3eb16e998e0ca0f2afa4d21f0cdbe3f0c94a551bc3e9848d2da964f61bbacbdc

Contents?: true

Size: 1.94 KB

Versions: 12

Compression:

Stored size: 1.94 KB

Contents

require_dependency 'guts/application_controller'

module Guts
  # Sessions controller
  class SessionsController < ApplicationController
    # Creation of a new session (login page)
    def new
    end
    
    # Checks the users session through post
    # @note It will redirect to Guts::UsersController if successful and
    #   it will redirect back to #new if not
    # @see Guts::SessionsHelper#log_in
    def create
      user = User.find_by(email: params[:session][:email].downcase)
      
      if user && user.authenticate(params[:session][:password])
        log_in user
        redirect_to users_path
      else
        flash.now[:notice] = 'Invalid login credentials'
        render :new
      end
    end
    
    # Destroys a user session
    # @see Guts::SessionsHelper#log_out
    def destroy
      log_out
      flash[:notice] = 'You have been logged out'
      redirect_to new_session_path
    end
    
    # Forgot password page
    def forgot
    end
    
    # Sends the user a new token by email to reset their password
    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[:notice] = 'Invalid email address'
        render :forgot
      end
    end
    
    # Resets the user's password
    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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
guts-1.3.6 app/controllers/guts/sessions_controller.rb
guts-1.3.5 app/controllers/guts/sessions_controller.rb
guts-1.3.4 app/controllers/guts/sessions_controller.rb
guts-1.3.3 app/controllers/guts/sessions_controller.rb
guts-1.3.2 app/controllers/guts/sessions_controller.rb
guts-1.3.1 app/controllers/guts/sessions_controller.rb
guts-1.3.0 app/controllers/guts/sessions_controller.rb
guts-1.2.2 app/controllers/guts/sessions_controller.rb
guts-1.2.1 app/controllers/guts/sessions_controller.rb
guts-1.2.0 app/controllers/guts/sessions_controller.rb
guts-1.1.1 app/controllers/guts/sessions_controller.rb
guts-1.1.0 app/controllers/guts/sessions_controller.rb