2022-06-14 01:21:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\User;
|
2022-10-20 03:15:33 +00:00
|
|
|
use App\Models\Estandar;
|
2022-06-14 01:21:39 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2022-10-20 22:27:03 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2022-06-14 01:21:39 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2022-10-20 22:27:03 +00:00
|
|
|
public function register(Request $request)
|
|
|
|
{
|
|
|
|
$request->validate([
|
2022-10-22 15:13:00 +00:00
|
|
|
'email' => 'required|email|unique:users',
|
|
|
|
'rol'=> 'required|numeric|size:2'
|
2022-06-14 01:21:39 +00:00
|
|
|
]);
|
2022-10-20 22:27:03 +00:00
|
|
|
$userAuth = auth()->user()->roles[0]->name;
|
2022-10-22 15:13:00 +00:00
|
|
|
if ($userAuth == "Admin") {
|
2022-10-20 22:27:03 +00:00
|
|
|
$user = new User();
|
|
|
|
$user->name = "null";
|
|
|
|
$user->lastname = "null";
|
|
|
|
$user->email = $request->email;
|
|
|
|
$user->password = "null";
|
2022-10-22 15:30:42 +00:00
|
|
|
$user->estado = true;
|
2022-10-20 22:27:03 +00:00
|
|
|
$user->save();
|
2022-10-22 15:13:00 +00:00
|
|
|
$user->roles()->attach($request->rol);
|
2022-10-20 22:27:03 +00:00
|
|
|
return response()->json([
|
|
|
|
'message' => 'Correo registrado exitosamente',
|
|
|
|
'userAuth' => $user,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
return response()->json([
|
2022-10-20 03:15:33 +00:00
|
|
|
"status" => 0,
|
2022-10-22 15:13:00 +00:00
|
|
|
"message" => "No eres administrador: Correo no registrado",
|
2022-10-20 03:15:33 +00:00
|
|
|
], 404);
|
2022-10-20 22:27:03 +00:00
|
|
|
}
|
2022-06-14 01:21:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 22:27:03 +00:00
|
|
|
public function login(Request $request)
|
|
|
|
{
|
2022-06-14 01:21:39 +00:00
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
"email" => "required|email",
|
|
|
|
"password" => "required"
|
|
|
|
]);
|
|
|
|
|
2022-10-20 22:27:03 +00:00
|
|
|
$user = User::where("email", "=", $request->email)->first();
|
2022-06-14 01:21:39 +00:00
|
|
|
|
2022-10-20 22:27:03 +00:00
|
|
|
if (isset($user->id)) {
|
|
|
|
if (Hash::check($request->password, $user->password)) {
|
2022-06-14 01:21:39 +00:00
|
|
|
$token = $user->createToken("auth_token")->plainTextToken;
|
|
|
|
return response()->json([
|
2022-06-29 22:38:43 +00:00
|
|
|
"message" => "Usuario logueado",
|
2022-08-13 02:27:34 +00:00
|
|
|
"access_token" => $token,
|
|
|
|
"nombre" => $user->name,
|
|
|
|
"apellido" => $user->lastname,
|
2022-07-09 03:09:04 +00:00
|
|
|
]);
|
2022-10-20 22:27:03 +00:00
|
|
|
} else {
|
2022-06-14 01:21:39 +00:00
|
|
|
return response()->json([
|
2022-06-29 22:38:43 +00:00
|
|
|
"message" => "La password es incorrecta",
|
2022-07-09 03:09:04 +00:00
|
|
|
], 404);
|
2022-06-14 01:21:39 +00:00
|
|
|
}
|
2022-10-20 22:27:03 +00:00
|
|
|
} else {
|
2022-06-14 01:21:39 +00:00
|
|
|
return response()->json([
|
|
|
|
"status" => 0,
|
2022-06-29 22:38:43 +00:00
|
|
|
"message" => "Usuario no registrado",
|
2022-07-09 03:09:04 +00:00
|
|
|
], 404);
|
2022-06-14 01:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 22:27:03 +00:00
|
|
|
public function userProfile()
|
|
|
|
{
|
2022-06-14 01:21:39 +00:00
|
|
|
return response()->json([
|
|
|
|
"status" => 0,
|
2022-06-29 22:38:43 +00:00
|
|
|
"message" => "Perfil de usuario",
|
2022-10-21 06:33:03 +00:00
|
|
|
"data" => auth()->user(),
|
2022-07-09 03:09:04 +00:00
|
|
|
]);
|
2022-06-14 01:21:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:55:51 +00:00
|
|
|
public function listUser(){
|
|
|
|
$users = User::all();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Lista de usuarios",
|
|
|
|
"data" => $users,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-10-20 22:27:03 +00:00
|
|
|
public function logout()
|
|
|
|
{
|
2022-06-14 01:21:39 +00:00
|
|
|
auth()->user()->tokens()->delete();
|
|
|
|
return response()->json([
|
2022-10-20 22:27:03 +00:00
|
|
|
"message" => "Sesion cerrada"
|
2022-07-09 03:09:04 +00:00
|
|
|
]);
|
2022-06-14 01:21:39 +00:00
|
|
|
}
|
|
|
|
}
|