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
- {
-
-
-
- use AuthenticatesUsers;
-
-
-
- protected $redirectTo = '/dashboard';
-
-
-
- public function __construct()
- {
- $this->middleware('guest')->except('logout');
- }
-
-
- public function redirectToProvider()
- {
- return Socialite::driver('google')->redirect();
- }
-
-
-
- public function handleProviderCallback()
- {
-
- try {
- $user = Socialite::driver('google')->user();
- } catch (Exception $e) {
- return redirect('/login');
- }
-
- if(explode("@", $user->email)[1] !== 'upr.edu'){
- return redirect()->to('/');
- }
-
- $existingUser = User::where('email', $user->email)->first();
-
- if($existingUser){
-
- auth()->login($existingUser, true);
- } else if (User::all()->count() === 0) {
-
- $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('/');
- }
- }
|