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 Google authentication page.
- *
- * @return \Illuminate\Http\Response
- */
- public function redirectToProvider()
- {
- return Socialite::driver('google')->redirect();
- }
-
- /**
- * Obtain the user information from Google.
- *
- * @return \Illuminate\Http\Response
- */
- public function handleProviderCallback()
- {
- try {
- $google_user = Socialite::driver('google')->user();
- } catch (Exception $e) {
- return redirect('/login');
- }
- // only allow people with @company.com to login
- // if(explode("@", $google_user->email)[1] !== 'upr.edu'){
- // return redirect()->to('/');
- // }
-
- // check if they're in the database
- $user = \App\User::where('email', $google_user->email)->first();
-
- if($user){
- $user->update([
- 'name' => $google_user->name,
- 'email' => $google_user->email,
- 'google_id' => $google_user->id
- ]);
- // dd($user);
- // log them in
- auth()->login($user, true);
- } else {
- return redirect('/');
- }
- return redirect()->back();
- }
-
- public function logout(Request $request) {
- auth()->logout();
- return redirect('/');
- }
- }
|