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,29 +29,33 @@ 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();
$userCreated = User::firstOrCreate(
[ if (isset($user)) {
'email' => $userProvider->email $userCreated = User::firstOrCreate(
], [
[ 'email' => $userProvider->email
//'email_verified_at' => now(), ],
'name' => $userProvider->user['given_name'], [
'lastname' => $userProvider->user['family_name'], //'email_verified_at' => now(),
'status' => true 'name' => $userProvider->user['given_name'],
] 'lastname' => $userProvider->user['family_name'],
'status' => true
]
); );
$userCreated->providers()->updateOrCreate( $userCreated->providers()->updateOrCreate(
@ -65,24 +74,23 @@ public function handleProviderCallback($provider){
"user" => $userCreated, "user" => $userCreated,
"image" => $userProvider->getAvatar(), "image" => $userProvider->getAvatar(),
"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
if (!in_array($provider, ['google'])){ if (!in_array($provider, ['google'])) {
return response()->json(['error' => 'Por favor usar google para loguearse'], 422); return response()->json(['error' => 'Por favor usar google para loguearse'], 422);
} }
} }