Correcion LoginController

This commit is contained in:
Christian Dacid Sullca Puma 2022-10-20 09:51:13 -05:00
parent 356126a4aa
commit 0b9adf6306

View File

@ -2,20 +2,25 @@
namespace App\Http\Controllers\api; namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite; use Laravel\Socialite\Facades\Socialite;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Hash; use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class LoginController extends Controller{
public function redirectToProvider($provider) { class LoginController extends Controller
{
public function redirectToProvider($provider)
{
$validated = $this->validateProvider($provider); $validated = $this->validateProvider($provider);
if (!is_null($validated)) { if (!is_null($validated)) {
return $validated; return $validated;
} }
return Socialite::driver($provider)->stateless()->redirect(); return Socialite::driver($provider)->redirect();
//return Socialite::driver($provider)->redirect(); //return Socialite::driver($provider)->redirect();
} }
@ -24,19 +29,23 @@ public function redirectToProvider($provider) {
//error_log('Some message here.'); //error_log('Some message here.');
public function handleProviderCallback($provider){ public function handleProviderCallback($provider)
{
$validated = $this->validateProvider($provider); $validated = $this->validateProvider($provider);
if (!is_null($validated)) {return $validated;} if (!is_null($validated)) {
return $validated;
}
try { try {
$userProvider = Socialite::driver($provider) ->stateless()->user(); $userProvider = Socialite::driver($provider)->user();
} } catch (ClientException $exception) {
catch (ClientException $exception) {
return response()->json(['error' => 'Credenciales de google invalidas.'], 422); return response()->json(['error' => 'Credenciales de google invalidas.'], 422);
} }
if(isset(User::where("email","=", $userProvider->email)->first()){ $user = User::where('email', $userProvider->getEmail())->first();
if (isset($user)) {
$userCreated = User::firstOrCreate( $userCreated = User::firstOrCreate(
[ [
'email' => $userProvider->email 'email' => $userProvider->email
@ -67,18 +76,17 @@ public function handleProviderCallback($provider){
"role" => $userCreated->roles[0]->name, "role" => $userCreated->roles[0]->name,
"access_token" => $token "access_token" => $token
]); ]);
} } else {
else{
return response()->json([ return response()->json([
"status" => 0, "status" => 0,
"message" => "Usuario no registrado en el sistema", "message" => "Usuario no registrado en el sistema",
], 404); ], 404);
} }
} }
protected function validateProvider($provider){ protected function validateProvider($provider)
{
//En caso se quiera iniciar sesion con facebook o github //En caso se quiera iniciar sesion con facebook o github
//if (!in_array($provider, ['facebook', 'github', 'google'])){ //if (!in_array($provider, ['facebook', 'github', 'google'])){
//por el momento solo con google //por el momento solo con google