Fixear los loginController y UserController

This commit is contained in:
cgonzalesmo 2022-11-05 11:53:41 -05:00
parent 0b15a0283f
commit 73234784c9
3 changed files with 61 additions and 59 deletions

View File

@ -4,13 +4,50 @@
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Estandar;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
//Login normal (correo y password)
public function login(Request $request)
{
public function redirectToProvider($provider)
{
$request->validate([
"email" => "required|email",
"password" => "required"
]);
$user = User::where("email", "=", $request->email)->where("estado",true)->first();
if (isset($user->id)) {
if (Hash::check($request->password, $user->password)) {
$token = $user->createToken("auth_token")->plainTextToken;
return response()->json([
"message" => "Usuario logueado",
"access_token" => $token,
"nombre" => $user->name,
"apellido" => $user->lastname,
]);
} else {
return response()->json([
"message" => "La password es incorrecta",
], 404);
}
} else {
return response()->json([
"status" => 0,
"message" => "Usuario no registrado o deshabilitado",
], 404);
}
}
//Login con plataformas externas
//Funcion de la recepcion del provider(google-facebook-github-twitter)
public function redirectToProvider($provider){
$validated = $this->validateProvider($provider);
if (!is_null($validated)) {
return $validated;
@ -19,14 +56,8 @@ public function redirectToProvider($provider)
//return Socialite::driver($provider)->redirect();
}
//$out = new \Symfony\Component\Console\Output\ConsoleOutput();
//$out->writeln("Hello from Terminal");
//error_log('Some message here.');
public function handleProviderCallback($provider)
{
//Funcion de la respuesta del provider
public function handleProviderCallback($provider){
$validated = $this->validateProvider($provider);
if (!is_null($validated)) {
return $validated;
@ -80,8 +111,7 @@ public function handleProviderCallback($provider)
}
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
@ -89,4 +119,14 @@ protected function validateProvider($provider)
return response()->json(['error' => 'Por favor usar google para loguearse'], 422);
}
}
//Logout
public function logout()
{
auth()->user()->tokens()->delete();
return response()->json([
"message" => "Sesion cerrada"
]);
}
}

View File

@ -39,37 +39,7 @@ public function register(Request $request)
}
}
public function login(Request $request)
{
$request->validate([
"email" => "required|email",
"password" => "required"
]);
$user = User::where("email", "=", $request->email)->where("estado",true)->first();
if (isset($user->id)) {
if (Hash::check($request->password, $user->password)) {
$token = $user->createToken("auth_token")->plainTextToken;
return response()->json([
"message" => "Usuario logueado",
"access_token" => $token,
"nombre" => $user->name,
"apellido" => $user->lastname,
]);
} else {
return response()->json([
"message" => "La password es incorrecta",
], 404);
}
} else {
return response()->json([
"status" => 0,
"message" => "Usuario no registrado o deshabilitado",
], 404);
}
}
public function userProfile()
{
@ -104,13 +74,7 @@ public function listUserHabilitados(){
]);
}
public function logout()
{
auth()->user()->tokens()->delete();
return response()->json([
"message" => "Sesion cerrada"
]);
}
public function updateRoleEstado(Request $request){
$request->validate([
"id"=>"exists:users",

View File

@ -20,9 +20,8 @@
use App\Http\Controllers\Api\LoginController;
use App\Http\Controllers\Api\NarrativasController;
Route::post('login', [UserController::class, 'login'])->name('login');
//Rutas de Auth
Route::post('login', [LoginController::class, 'login'])->name('login');
Route::get('login/{provider}', [LoginController::class, 'redirectToProvider']);
Route::get('login/{provider}/callback', [LoginController::class, 'handleProviderCallback']);
@ -37,18 +36,17 @@
Route::get('estandares', [EstandarController::class, 'listEstandarValores']);
//CAMBIAR ESTO
//Route::get('user', [UserController::class, 'listUser']);
Route::get('user', [UserController::class, 'listUserHabilitados']);
Route::middleware("auth:sanctum")->group(function () {
//rutas auth
//Rutas de Auth
Route::get('logout', [LoginController::class, 'logout']);
//Rutas de Gestion de usarios
Route::get('user-profile', [UserController::class, 'userProfile']);
Route::get('update', [UserController::class, 'updateRoleEstado']);
Route::get('logout', [UserController::class, 'logout']);
Route::post('register', [UserController::class, 'register']);
Route::get('user', [UserController::class, 'listUser']);
Route::get('enabled_users', [UserController::class, 'listUserHabilitados']);
//rutas estandar
Route::post('estandar', [EstandarController::class, 'createEstandar']);