Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.74 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use \App\UserAuthenticate;
  6.  
  7. use App\User;
  8.  
  9. use Auth;
  10. use DB;
  11. use Hash;
  12. use Input;
  13. use Request;
  14. use Redirect;
  15. use Session;
  16. use Validator;
  17. use View;
  18.  
  19. class WebController extends Controller
  20. {
  21.     /**
  22.      * login function
  23.      */
  24.     public function login(Request $request)
  25.     {
  26.         // Get a handle of the user
  27.         $user = $this->getUser();
  28.        
  29.         // If there is a logged in User then redirect to the homepage
  30.         if ($user)
  31.         {
  32.             return redirect()->route('my-profile');
  33.         }
  34.        
  35.         // Retrieve the input      
  36.         $input = Request::all();   
  37.  
  38.         // Check that an input has been supplied       
  39.         if ($input)
  40.         {
  41.             // Define the validation values
  42.             $entry = array(
  43.                 'email'     => 'required|email', // @Validate entry as (email)
  44.                 'password'  => 'required|alphaNum|min:6' // @Alphanumber character greater than (6)
  45.             );
  46.            
  47.             // Validate all form data
  48.             $validator = Validator::make($input, $entry);
  49.            
  50.             // Check that the validation has passed
  51.             if ($validator->fails())
  52.             {
  53.                 // Define a flash error
  54.                 Session::flash('error', 'The login details you have provided are not valid.');
  55.                    
  56.                 return redirect()->route('login')
  57.                 ->withErrors($validator) // Send back all errors to the login form
  58.                 ->withInput($input); // Return all fields, except the password field
  59.             }
  60.             else
  61.             {
  62.                 // Create our user data for the authentication
  63.                 $formData = array(
  64.                     'email'     => $input['email'],
  65.                     'password'  => $input['password']
  66.                 );
  67.            
  68.                 // Attempt to do the login
  69.                 if (Auth::attempt($formData))
  70.                 {
  71.                     // Authentication Granted
  72.                     return redirect()->route('my-profile');
  73.                 }
  74.                 else
  75.                 {    
  76.                     // Define a flash error
  77.                     Session::flash('error', 'The login details you have provided are not valid.');
  78.                    
  79.                     // Redirect
  80.                     return redirect()->route('login');
  81.                 }
  82.             }
  83.         }
  84.        
  85.         // Marshall
  86.         $viewData = array();
  87.         $viewData['user'] = $user;
  88.         return view('Web.login', $viewData);
  89.     }
  90.    
  91.     /**
  92.      * register function
  93.      */
  94.     public function register(Request $request)
  95.     {
  96.         // Mandatory functions
  97.         $user = $this->getUser();
  98.        
  99.         // If there is a logged in User then redirect to the homepage
  100.         if ($user)
  101.         {
  102.             return redirect()->route('my-profile');
  103.         }
  104.  
  105.         // Retrieve the input      
  106.         $input = Request::all();
  107.  
  108.         // Check that an input has been supplied       
  109.         if ($input)
  110.         {
  111.             // Define the validation values
  112.             $entry = array(
  113.                 'username' => 'required', // Required
  114.                 'email' => 'required|email', // Validate entry as (email)
  115.                 'password'          => 'required|alphaNum|min:6', // Alphanumber character greater than (6)
  116.                 'confirm_password'  => 'required|alphaNum|min:6', // Alphanumber character greater than (6)
  117.             );
  118.            
  119.             // Validate all form data
  120.             $validator = Validator::make($input, $entry);
  121.            
  122.             // Check that the validation has passed
  123.             if ($validator->fails())
  124.             {
  125.                 Session::flash('error', 'Please provide the correct information');
  126.                
  127.                 return redirect()->route('register')
  128.                 ->withErrors($validator) // Send back all errors to the login form
  129.                 ->withInput($input) // Return all fields, except the password fields
  130.                 ;
  131.             }
  132.             else
  133.             {
  134.                 // Retrieve the form data
  135.                 $formData = array(
  136.                     'username' => $input['username'],
  137.                     'email' => $input['email'],
  138.                     'password' => $input['password']
  139.                 );
  140.                
  141.                 // Retrieve any User with the provided email address
  142.                 $duplicateUsers = DB::table('users')
  143.                 ->where('email', '=', $formData['email'])
  144.                 ->get()
  145.                 ;
  146.                
  147.                 // If there are any $duplicateUsers then redirect them back
  148.                 if (count($duplicateUsers) > 0)
  149.                 {
  150.                     // Define a flash error
  151.                     Session::flash('error', 'There is aready an account associated with this email address.');
  152.                    
  153.                     return redirect()->route('register');
  154.                 }
  155.                 else
  156.                 {
  157.                     // Store the  so that we can reset the hash for login
  158.                     $password = $formData['password'];
  159.                    
  160.                     // Hash the password
  161.                     $formData['password'] = hash::make($formData['password']);
  162.                    
  163.                     // Define some default information for the User
  164.                     $formData['silver_coins'] = 1000;
  165.                     $formData['gold_coins'] = 100;
  166.                     $formData['city_id'] = 1;
  167.                     $formData['vip_days'] = 3;
  168.                     $formData['ip_address'] = Request::ip();
  169.                    
  170.                     // Create the User
  171.                     DB::table('users')->insert($formData);
  172.                    
  173.                     // Reset the hash
  174.                     $formData['password'] = $password;
  175.                    
  176.                     // Log the User in
  177.                     if (Auth::attempt(['email' => $formData['email'], 'password' => $password]))
  178.                     {
  179.                         // Authentication Granted
  180.                         return redirect()->route('my-profile');
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.        
  186.         // Marshall
  187.         $viewData = array();
  188.         return view('Web.register', $viewData);
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement