Files
crud-laravel/app/Http/Controllers/UserController.php
2024-05-21 14:17:55 +08:00

127 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
/**
* Show the user details for the given ID.
*
* @param string $id The ID of the user to retrieve.
* @return \Illuminate\Http\JsonResponse A JSON response containing the user details.
*/
public function show(string $id)
{
$user = User::findOrFail($id);
return response()->json($user, 200);
}
/**
* Log in a user with the provided credentials.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response indicating success or failure of the login attempt.
*/
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (auth()->attempt($credentials)) {
error_log('User logged in');
return response()->json(['message' => 'User logged in', 'user' => auth()->user()], 200);
} else {
error_log('User not found');
return response()->json(['message' => 'User not found'], 401);
}
}
/**
* Log out the currently authenticated user.
*
* @return \Illuminate\Http\JsonResponse A JSON response indicating the user has been logged out.
*/
public function logout()
{
auth()->logout();
return response()->json(['message' => 'User logged out'], 200);
}
/**
* Validate the given user data.
*
* @param array $data The user data to validate.
* @return \Illuminate\Contracts\Validation\Validator The validator instance.
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8'],
]);
}
/**
* Create a new user with the given data.
*
* @param array $data The user data to use for creation.
* @return \App\Models\User The created user instance.
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
/**
* Register a new user.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response indicating success or failure of the registration attempt.
*/
public function register(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 422);
}
event(new Registered($user = $this->create($request->all())));
return response()->json(['message' => 'User registered successfully', 'user' => $user], 201);
}
/**
* Edit the profile of the currently authenticated user.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response indicating the profile update status.
*/
public function editProfile(Request $request)
{
// $validator = Validator::make($request->all(), [
// 'email' => 'required',
// 'name' => 'required',
// ]);
// if ($validator->fails()) {
// return response()->json($validator->errors(), 400);
// }
$user = User::find(auth()->user()->id);
$data = $request->only('name', 'email');
$user->fill($data)->save();
return response()->json(['message' => 'Profile updated successfully', 'user' => $user], 200);
}
}