1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
-
- namespace App\Http\Controllers\Auth;
-
- use App\Http\Controllers\Controller;
- use Socialite;
- use Illuminate\Foundation\Auth\AuthenticatesUsers;
- use Illuminate\Foundation\Auth\User;
- use Illuminate\Http\Request;
-
- class LoginController extends Controller
- {
- /*
- |--------------------------------------------------------------------------
- | Login Controller
- |--------------------------------------------------------------------------
- |
- | This controller handles authenticating users for the application and
- | redirecting them to your home screen. The controller uses a trait
- | to conveniently provide its functionality to your applications.
- |
- */
-
- use AuthenticatesUsers;
-
- /**
- * Where to redirect users after login.
- *
- * @var string
- */
- protected $redirectTo = '/dashboard';
-
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->middleware('guest')->except('logout');
- }
-
- /**
- * Redirect the user to the GitHub authentication page.
- *
- * @return \Illuminate\Http\Response
- */
- public function redirectToProvider()
- {
- return Socialite::driver('google')->redirect();
- }
-
- /**
- * Obtain the user information from GitHub.
- *
- * @return \Illuminate\Http\Response
- */
- public function handleProviderCallback()
- {
- // $user = Socialite::driver('google')->user();
- try {
- $user = Socialite::driver('google')->user();
- } catch (Exception $e) {
- return redirect('/login');
- }
- // only allow people with @company.com to login
- if(explode("@", $user->email)[1] !== 'upr.edu'){
- return redirect()->to('/');
- }
- // check if they're an existing user
- $existingUser = User::where('email', $user->email)->first();
-
- if($existingUser){
- // log them in
- auth()->login($existingUser, true);
- } else if (User::all()->count() === 0) {
- // create a new user
- $newUser = new User;
- $newUser->name = $user->name;
- $newUser->email = $user->email;
- $newUser->google_id = $user->id;
- $newUser->save();
- auth()->login($newUser, true);
- }
- return redirect()->to('/');
- }
-
- public function logout(Request $request) {
- auth()->logout();
- return redirect('/');
- }
- }
|