# -*- encoding : utf-8 -*-
class Student::PasswordResetsController < ApplicationController
  layout "connexion"
  
  def new
    @student = true
  end

  def create
    @student = true
    student_user = StudentUser.find_by_email(params[:email])
    if student_user
      student_user.reset_password_token = SecureRandom.urlsafe_base64(nil, false)
      student_user.reset_password_sent_at  = Time.now
      student_user.save
      StudentMails.reset_password(student_user).deliver
      redirect_to student_root_path, notice: "Un message vient de vous être envoyé avec un lien pour réinitialiser votre mot de passe."
    else
      flash.now.alert = "Email incorect"
      render "new"
    end
  end

  def edit
    @student = true
    
    @student_user = StudentUser.find_by_reset_password_token(params[:id])
    
    if @student_user and @student_user.reset_password_sent_at > Time.now - 1.day
      
    else
      
    end
  end
  
  def update
    @student = true
    
    @student_user = StudentUser.find_by_reset_password_token(params[:id])
    
    if @student_user.update_attributes(params.require(:student_user).permit(:password, :password_confirmation))
      redirect_to student_root_path, :notice => "Votre mot de passe a bien été changé, vous pouvez vous connecter dès maintenant."
        
  	else
  		render :action => "edit" 
  	end
    
    
    
  end
  
end