# -*- encoding : utf-8 -*-
class Public::PCustomerAuthsController < ApplicationController
  layout "public"

  def new
    
     params[:step] = "login"
    

    @p_customer = PCustomer.new(:valid_public => true)
    
    


  end

  def create

    
    @p_customer = PCustomer.new(:valid_public => true)
    
    params[:step] = "login"
    
    user = PCustomer.find_by_email(params[:email])
    
    if user && user.password_digest && user.authenticate(params[:password].to_s)
      #session[:forum_user_id] = user.id


      if params[:remember_me]
        cookies.permanent[:p_customer_auth_token] = user.token
      else
        cookies[:p_customer_auth_token] = user.token
      end

      user.last_sign_in_at = Time.now
      user.save(:validate => false)

     
      redirect_to today_public_kaps_days_path
    
    
    else
      flash.now.alert = "Email ou mot de passe incorect"

      render :action => "new"
    end
  end

  def logout
    cookies.delete(:p_customer_auth_token)
    redirect_to "/", notice: "Déconnecté."
  end
end
