creator = $creator; } private function createErrorMessage(Request $dict, string $key): string|null { if (empty($dict[$key])) { return "The $key field is required."; } return null; } /** * Create a new user. * * @param Request $request The request object containing user information. * * @return JsonResponse * * @params Request $request The request object containing: * - username (string): The desired username for the new user. * - password (string): The desired password for the new user. * - password_confirmation (string): Confirmation of the password. * - email (string): The desired email for the new user. * - type (string): The desired user type (dj, editor, admin). */ public function create(Request $request): JsonResponse { $errors = collect([ $this->createErrorMessage($request, 'username'), $this->createErrorMessage($request, 'password'), $this->createErrorMessage($request, 'password_confirmation'), $this->createErrorMessage($request, 'email'), $this->createErrorMessage($request, 'type'), ])->filter()->toArray(); if (! empty($errors)) { return response()->json(['errors' => $errors], 422); } $userInfos = [ 'username' => $request->username, 'password' => $request->password, 'password_confirmation' => $request->password_confirmation, 'email' => $request->email, 'type' => $request->type, ]; try { $this->creator->create($userInfos); return response()->json(['message' => 'User created successfully'], 201); } catch (Exception $e) { return response()->json(['errors' => $e->getMessage()], 422); } } ## To be removed public function register_token($request) { $token = $request->user()->createToken($request->token_name); return ['token' => $token->plainTextToken]; } }