Sha256: 5689ea0a0fc78b2378c5099f4e0a49dfe1a5feaabb2220bbd88123c4ddd310a9

Contents?: true

Size: 1.94 KB

Versions: 4

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
        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
    
    # Resets the user's password
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guts-1.0.8 app/controllers/guts/sessions_controller.rb
guts-1.0.7 app/controllers/guts/sessions_controller.rb
guts-1.0.5 app/controllers/guts/sessions_controller.rb
guts-1.0.3 app/controllers/guts/sessions_controller.rb