Added possiblity to register

This commit is contained in:
Jeroen De Meerleer 2023-08-16 15:14:19 +02:00
parent 677eba19b5
commit 653cdffcae
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 740 additions and 534 deletions

View File

@ -4,6 +4,7 @@
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest; use App\Http\Requests\Auth\LoginRequest;
use App\Models\User;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
@ -25,4 +26,5 @@ public function store(LoginRequest $request): JsonResponse
return response()->json(['Success' => true, 'Authorization' => $authid]); return response()->json(['Success' => true, 'Authorization' => $authid]);
} }
} }

View File

@ -5,11 +5,13 @@
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\User; use App\Models\User;
use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Events\Registered;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules; use Illuminate\Validation\Rules;
use Illuminate\Validation\ValidationException;
class RegisteredUserController extends Controller class RegisteredUserController extends Controller
{ {
@ -18,13 +20,17 @@ class RegisteredUserController extends Controller
* *
* @throws \Illuminate\Validation\ValidationException * @throws \Illuminate\Validation\ValidationException
*/ */
public function store(Request $request): Response public function store(Request $request): JsonResponse
{ {
$request->validate([ try {
'name' => ['required', 'string', 'max:255'], $request->validate([
'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class], 'name' => ['required', 'string', 'max:255'],
'password' => ['required', Rules\Password::defaults()], '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);
}
$user = User::create([ $user = User::create([
'name' => $request->name, 'name' => $request->name,
@ -34,8 +40,21 @@ public function store(Request $request): Response
event(new Registered($user)); event(new Registered($user));
Auth::login($user); return new JsonResponse(['success'=> true, 'message' => 'User ' . $user->getAttribute('name') . ' Registered']);
}
return response()->noContent(); 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';
}
return new JsonResponse($return);
} }
} }

1235
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,8 @@
| |
*/ */
Route::get('/capabilities', [App\Http\Controllers\InfoController::class, 'capabilities']); Route::get('/capabilities', [App\Http\Controllers\InfoController::class, 'capabilities']);
Route::post('/register/checkusertaken', [App\Http\Controllers\Auth\RegisteredUserController::class, 'checkUserTaken']);
Route::get('/register/checkusertaken', [App\Http\Controllers\Auth\RegisteredUserController::class, 'checkUserTaken']);
Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) { Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) {
return $request->user(); return $request->user();