You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and dots ('.'), can be up to 35 characters long. Letters must be lowercase.
		
		
		
		
		
			
		
			
				
					
					
						
							55 lines
						
					
					
						
							1.4 KiB
						
					
					
				
			
		
		
	
	
							55 lines
						
					
					
						
							1.4 KiB
						
					
					
				| <?php | |
| 
 | |
| namespace App\Http\Controllers\Auth; | |
| 
 | |
| use App\Http\Controllers\Controller; | |
| use App\Models\Entreprise; | |
| use Laravel\Socialite\Facades\Socialite; | |
| use App\Models\User; | |
| use Illuminate\Support\Str; | |
| use Illuminate\Support\Carbon; | |
| use Illuminate\Support\Facades\Auth; | |
| use Exception; | |
| use Illuminate\Support\Facades\Hash; | |
| 
 | |
| class SocialiteController extends Controller | |
| { | |
|     protected $providers = [ "google" ]; | |
| 
 | |
|     public function redirect($provider) | |
|     { | |
|         if(in_array($provider, $this->providers)) | |
|         { | |
|             return Socialite::driver($provider)->redirect(); | |
|         } | |
|         return response("", 404); | |
|     } | |
| 
 | |
|     public function callback($provider) | |
|     { | |
|         if(in_array($provider, $this->providers)) | |
|         { | |
|             $user = null; | |
|             try { | |
|                 $user = Socialite::driver($provider)->user(); | |
|             } catch(Exception $e) { | |
|                 return redirect('/login')->withErrors([ | |
|                     "message" => "Erreur durant l'authentification avec " . $provider | |
|                 ]); | |
|             } | |
|             $email = $user->getEmail(); | |
|             $name = $user->getName(); | |
| 
 | |
|             $user = User::where("email", $email)->first(); | |
|             if(!$user) | |
|             { | |
|                 return redirect(route("login")); | |
|             } | |
| 
 | |
|             Auth::login($user); | |
|             if (Auth::check()) return redirect('/'); | |
|         } | |
|         return response("", 404); | |
|     } | |
| } | |
| 
 | |
| 
 |