blackbirdchess-server/app/Http/Controllers/Auth/RegisteredUserController.php

61 lines
1.9 KiB
PHP
Raw Normal View History

2023-02-01 17:10:21 +01:00
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
2023-08-16 15:14:19 +02:00
use Illuminate\Http\JsonResponse;
2023-02-01 17:10:21 +01:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
2023-08-16 15:14:19 +02:00
use Illuminate\Validation\ValidationException;
2023-02-01 17:10:21 +01:00
class RegisteredUserController extends Controller
{
/**
* Handle an incoming registration request.
*
* @throws \Illuminate\Validation\ValidationException
*/
2023-08-16 15:14:19 +02:00
public function store(Request $request): JsonResponse
2023-02-01 17:10:21 +01:00
{
2023-08-16 15:14:19 +02:00
try {
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class],
'password' => ['required', Rules\Password::defaults()],
]);
} catch (ValidationException $e) {
return new JsonResponse(['success'=> false, 'message' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
}
2023-02-01 17:10:21 +01:00
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
event(new Registered($user));
2023-08-16 15:14:19 +02:00
return new JsonResponse(['success'=> true, 'message' => 'User ' . $user->getAttribute('name') . ' Registered']);
}
public function checkUserTaken(Request $request): JsonResponse
{
$return = [];
if ($request->request->has('email')) {
$user = User::where('email', $request->request->get('email'))->first();
if ($user !== NULL) $return['email'] = 'taken';
}
if ($request->request->has('name')) {
$user = User::where('name', $request->request->get('name'))->first();
if ($user !== NULL) $return['name'] = 'taken';
}
2023-02-01 17:10:21 +01:00
2023-08-16 15:14:19 +02:00
return new JsonResponse($return);
2023-02-01 17:10:21 +01:00
}
}