/**
  * Registration manages the confirmation link sent to the users 
  * who registered for the first time.
  * ------------------------------------------------------------
  * 
  * @method confirm string 
  * 
  */
 public function confirm($confirmation_code)
 {
     /**
      * Check for confirmation link with code in the database 
      * if true set the confirmation code field to null and 
      * the confirmed feild to 1 then redirect to login page
      * else redirect them to login with error message.
      */
     if ($confirmed_code = Userconfirmation::where('confirmation_code', '=', $confirmation_code)->first()) {
         $user_id = $confirmed_code->user_id;
         $confirmed_code->user_id = $user_id;
         $confirmed_code->confirmed = 1;
         $confirmed_code->confirmation_code = null;
         $confirmed_code->save();
         return Redirect::to('login')->with(array('welcome_back' => 'You have been confirmed please login.'));
         //return Redirect::to('login')->with(array('confirmed'=>'true','send_to'=>'chooseaccount'));
     } else {
         return Redirect::to('login')->with(array('message' => 'The verification link has expired or not correct. <a href="http://popibay.com">Register</a>'));
     }
 }
 public function login()
 {
     $rules = ['email' => 'required|exists:users', 'password' => 'required'];
     $input = Input::only('email', 'password');
     $validator = Validator::make($input, $rules);
     if ($validator->fails()) {
         //dd($input);
         return Redirect::back()->withInput()->with($validator);
     }
     $credentials = ['email' => Input::get('email'), 'password' => Input::get('password')];
     // check if user is authentic
     $valid = Auth::validate($credentials);
     if (!$valid) {
         //dd($input);
         return Redirect::back()->withInput()->with(['message' => 'We were unable to sign you in. Incorrect email/password combination!']);
     }
     // user is valid, lets check a few things
     $user = User::where('email', '=', Input::get('email'))->first();
     $user_id = $user->id;
     $get_user_id = Userconfirmation::where('user_id', '=', $user_id)->first();
     $user_confirm = $get_user_id->confirmed;
     // check if user has confirmed their account
     if ($user_confirm != 1) {
         return Redirect::back()->withInput()->with(['message' => 'You must confirm your account before you can use your dashboard.']);
     }
     // Store your session variables
     Session::put('pb_user_name', $user->name);
     Session::put('pb_user_email', Input::get('email'));
     // incase you decide to use 'Remember me?' checkbox on login
     $remember = Input::get('remember');
     // login the user
     Auth::login($user, $remember);
     // redirect to the page they were trying to view, or redirect to index
     return Redirect::intended('dashboard');
     // Use this if you want to redirect to a named route instead
     // return Redirect::intended(route('home'));
 }
 public function register()
 {
     /**
      * Register form variables
      * 
      * @var $pbregisterName string "The name of the full new user"
      * @var $pbregisterEmail string "The email of the new user"
      * @var $pbregisterPassword string "The password of the new user"
      * @var $pbregisterCpassword string "The confirmed password of the new users"
      * 
      */
     $pbregister = Input::all();
     // Unique value for validation
     $pbregisterName = Input::get('name');
     $pbregisterEmail = Input::get('email');
     $pbregisterPassword = Input::get('password');
     $pbregisterCpassword = Input::get('password_confirmation');
     /**
      * Validate the form variables
      * 
      * @var $pbregister string "All the input in the form..."
      * @var $pbrules string "Our rules to validate the form"
      *
      */
     $pbrules = array('name' => 'required|min:2|max:32', 'email' => 'required|email', 'password' => 'required|alpha_num|min:8');
     // Unique value for validation
     /**
      * Call the Validate class
      * 
      * @class Validate class "using the Validate::make()"
      * @var $pbvalidator class "holds the class"
      * 
      */
     $pbvalidator = Validator::make($pbregister, $pbrules);
     /**
      * Check if the new/anoynomous user passes the validation
      * 
      */
     if ($pbvalidator->passes()) {
         /**
          * Goto the mail and database now...
          * 
          */
         //$confirmation_code = str_random(30);
         $pbuser = new User();
         $pbuser->name = Input::get('name');
         $pbuser->email = Input::get('email');
         $pbuser->password = Hash::make(Input::get('password'));
         $pbuser->save();
         /* save cofirmation code */
         $pbemail = $pbuser->email;
         $pbname = $pbuser->name;
         $email = Input::get('email');
         $users = User::where('email', '=', $email)->first();
         $user_id = $users->id;
         // am not sure if it will return the user value...
         $confirmation_code = str_random(30);
         $pbconfirm = new Userconfirmation();
         $pbconfirm->user_id = $user_id;
         $pbconfirm->confirmed = 0;
         $pbconfirm->confirmation_code = $confirmation_code;
         $pbconfirm->save();
         /*$user = User::find($user_id);
         		$username = $user->name;
         		$useremail = $user->email;
         		$userpassword = $user->password;
         		$userlogintimes = $user->no_of_logins;
         		///////////////////////////////////////////
         		$user->name = $username;
         		$user->email = $useremail;
         		$user->password = $userpassword;
         		$user->no_of_logins = 0;
         		$user->confirmed = 1;
         		$user->save();*/
         $confirmation_code = array('confirmation_code' => $confirmation_code, 'pb_username' => $pbname);
         Mail::send('emails.welcome', $confirmation_code, function ($message) {
             $pbemail = Input::get('email');
             $pbname = Input::get('name');
             $pb_user_name = $pbname;
             $pb_user_email = $pbemail;
             $message->from('*****@*****.**');
             $message->to($pb_user_email, $pb_user_name)->subject('Welcome to Popibay ');
         });
         return Redirect::to('/regsuccess')->with(array($confirmation_code));
     } else {
         return Redirect::to('/register')->with(array('message' => 'Password must contain uppercase and lower case characters.'));
     }
 }