Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use \App\UserAuthenticate;
- use App\User;
- use Auth;
- use DB;
- use Hash;
- use Input;
- use Request;
- use Redirect;
- use Session;
- use Validator;
- use View;
- class WebController extends Controller
- {
- /**
- * login function
- */
- public function login(Request $request)
- {
- // Get a handle of the user
- $user = $this->getUser();
- // If there is a logged in User then redirect to the homepage
- if ($user)
- {
- return redirect()->route('my-profile');
- }
- // Retrieve the input
- $input = Request::all();
- // Check that an input has been supplied
- if ($input)
- {
- // Define the validation values
- $entry = array(
- 'email' => 'required|email', // @Validate entry as (email)
- 'password' => 'required|alphaNum|min:6' // @Alphanumber character greater than (6)
- );
- // Validate all form data
- $validator = Validator::make($input, $entry);
- // Check that the validation has passed
- if ($validator->fails())
- {
- // Define a flash error
- Session::flash('error', 'The login details you have provided are not valid.');
- return redirect()->route('login')
- ->withErrors($validator) // Send back all errors to the login form
- ->withInput($input); // Return all fields, except the password field
- }
- else
- {
- // Create our user data for the authentication
- $formData = array(
- 'email' => $input['email'],
- 'password' => $input['password']
- );
- // Attempt to do the login
- if (Auth::attempt($formData))
- {
- // Authentication Granted
- return redirect()->route('my-profile');
- }
- else
- {
- // Define a flash error
- Session::flash('error', 'The login details you have provided are not valid.');
- // Redirect
- return redirect()->route('login');
- }
- }
- }
- // Marshall
- $viewData = array();
- $viewData['user'] = $user;
- return view('Web.login', $viewData);
- }
- /**
- * register function
- */
- public function register(Request $request)
- {
- // Mandatory functions
- $user = $this->getUser();
- // If there is a logged in User then redirect to the homepage
- if ($user)
- {
- return redirect()->route('my-profile');
- }
- // Retrieve the input
- $input = Request::all();
- // Check that an input has been supplied
- if ($input)
- {
- // Define the validation values
- $entry = array(
- 'username' => 'required', // Required
- 'email' => 'required|email', // Validate entry as (email)
- 'password' => 'required|alphaNum|min:6', // Alphanumber character greater than (6)
- 'confirm_password' => 'required|alphaNum|min:6', // Alphanumber character greater than (6)
- );
- // Validate all form data
- $validator = Validator::make($input, $entry);
- // Check that the validation has passed
- if ($validator->fails())
- {
- Session::flash('error', 'Please provide the correct information');
- return redirect()->route('register')
- ->withErrors($validator) // Send back all errors to the login form
- ->withInput($input) // Return all fields, except the password fields
- ;
- }
- else
- {
- // Retrieve the form data
- $formData = array(
- 'username' => $input['username'],
- 'email' => $input['email'],
- 'password' => $input['password']
- );
- // Retrieve any User with the provided email address
- $duplicateUsers = DB::table('users')
- ->where('email', '=', $formData['email'])
- ->get()
- ;
- // If there are any $duplicateUsers then redirect them back
- if (count($duplicateUsers) > 0)
- {
- // Define a flash error
- Session::flash('error', 'There is aready an account associated with this email address.');
- return redirect()->route('register');
- }
- else
- {
- // Store the so that we can reset the hash for login
- $password = $formData['password'];
- // Hash the password
- $formData['password'] = hash::make($formData['password']);
- // Define some default information for the User
- $formData['silver_coins'] = 1000;
- $formData['gold_coins'] = 100;
- $formData['city_id'] = 1;
- $formData['vip_days'] = 3;
- $formData['ip_address'] = Request::ip();
- // Create the User
- DB::table('users')->insert($formData);
- // Reset the hash
- $formData['password'] = $password;
- // Log the User in
- if (Auth::attempt(['email' => $formData['email'], 'password' => $password]))
- {
- // Authentication Granted
- return redirect()->route('my-profile');
- }
- }
- }
- }
- // Marshall
- $viewData = array();
- return view('Web.register', $viewData);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement