Correcion LoginController
This commit is contained in:
parent
356126a4aa
commit
0b9adf6306
@ -2,20 +2,25 @@
|
||||
|
||||
namespace App\Http\Controllers\api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;;
|
||||
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
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);
|
||||
if (!is_null ($validated)) {
|
||||
if (!is_null($validated)) {
|
||||
return $validated;
|
||||
}
|
||||
return Socialite::driver($provider)->stateless()->redirect();
|
||||
return Socialite::driver($provider)->redirect();
|
||||
//return Socialite::driver($provider)->redirect();
|
||||
}
|
||||
|
||||
@ -24,19 +29,23 @@ public function redirectToProvider($provider) {
|
||||
//error_log('Some message here.');
|
||||
|
||||
|
||||
public function handleProviderCallback($provider){
|
||||
public function handleProviderCallback($provider)
|
||||
{
|
||||
|
||||
$validated = $this->validateProvider($provider);
|
||||
if (!is_null($validated)) {return $validated;}
|
||||
if (!is_null($validated)) {
|
||||
return $validated;
|
||||
}
|
||||
|
||||
try {
|
||||
$userProvider = Socialite::driver($provider) ->stateless()->user();
|
||||
}
|
||||
catch (ClientException $exception) {
|
||||
return response()->json(['error' => 'Credenciales de google invalidas.'],422);
|
||||
$userProvider = Socialite::driver($provider)->user();
|
||||
} catch (ClientException $exception) {
|
||||
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(
|
||||
[
|
||||
'email' => $userProvider->email
|
||||
@ -67,22 +76,21 @@ public function handleProviderCallback($provider){
|
||||
"role" => $userCreated->roles[0]->name,
|
||||
"access_token" => $token
|
||||
]);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
return response()->json([
|
||||
"status" => 0,
|
||||
"message" => "Usuario no registrado en el sistema",
|
||||
], 404);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected function validateProvider($provider){
|
||||
protected function validateProvider($provider)
|
||||
{
|
||||
//En caso se quiera iniciar sesion con facebook o github
|
||||
//if (!in_array($provider, ['facebook', 'github', '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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user