unsa-pis-be/app/Http/Controllers/Api/UserController.php
2022-08-12 21:27:34 -05:00

81 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
public function register(Request $request){
$request->validate([
'name'=>'required',
'lastname'=>'required',
'email'=>'required|email|unique:users',
'password'=>'required|confirmed'
]);
$user = new User();
$user->name = $request->name;
$user->lastname = $request->lastname;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->save();
return response()->json([
'message'=>'registro exitoso'
]);
}
public function login(Request $request){
$request->validate([
"email" => "required|email",
"password" => "required"
]);
$user = User::where("email","=", $request->email)->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",
], 404);
}
}
public function userProfile(){
return response()->json([
"status" => 0,
"message" => "Perfil de usuario",
"data" => auth()->user()
]);
}
public function logout(){
auth()->user()->tokens()->delete();
return response()->json([
"status" => 1,
"message" => "Cierre de Sesión",
]);
}
}