unsa-pis-be/app/Http/Controllers/Api/UserController.php

79 lines
2.1 KiB
PHP
Raw Normal View History

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;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
public function register(Request $request){
$request->validate([
'name'=>'required',
'lastname'=>'required',
2022-06-14 01:21:39 +00:00
'email'=>'required|email|unique:users',
'password'=>'required|confirmed'
]);
$user = new User();
$user->name = $request->name;
$user->lastname = $request->lastname;
2022-06-14 01:21:39 +00:00
$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([
2022-06-29 22:38:43 +00:00
"message" => "Usuario logueado",
2022-06-14 01:21:39 +00:00
"access_token" => $token
]);
2022-06-14 01:21:39 +00:00
}else{
return response()->json([
2022-06-29 22:38:43 +00:00
"message" => "La password es incorrecta",
], 404);
2022-06-14 01:21:39 +00:00
}
}else{
return response()->json([
"status" => 0,
2022-06-29 22:38:43 +00:00
"message" => "Usuario no registrado",
], 404);
2022-06-14 01:21:39 +00:00
}
}
public function userProfile(){
return response()->json([
"status" => 0,
2022-06-29 22:38:43 +00:00
"message" => "Perfil de usuario",
2022-06-14 01:21:39 +00:00
"data" => auth()->user()
]);
2022-06-14 01:21:39 +00:00
}
public function logout(){
auth()->user()->tokens()->delete();
return response()->json([
"status" => 1,
"message" => "Cierre de Sesión",
]);
2022-06-14 01:21:39 +00:00
}
2022-06-14 01:21:39 +00:00
}