diff --git a/app/Http/Controllers/AuthorController.php b/app/Http/Controllers/AuthorController.php index d89ba5e..0bb17a1 100644 --- a/app/Http/Controllers/AuthorController.php +++ b/app/Http/Controllers/AuthorController.php @@ -5,6 +5,9 @@ use App\Models\Role; use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Http\Request; +use App\Models\User; +use Illuminate\Validation\ValidationException; class AuthorController extends Controller { @@ -37,11 +40,29 @@ public function create() * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function store(Request $request) - { - User::create($request->post()); - return redirect()->route('authors.index')->with('message', 'Author created successfully'); + public function store(Request $request) +{ + try { + // Validate the incoming request data + $validatedData = $request->validate([ + 'name' => 'required|string|max:255', + 'email' => 'required|string|email|unique:users|max:255', + 'password' => 'required|string|min:8', + ]); + + // Hash the password for security before storing + $validatedData['password'] = bcrypt($validatedData['password']); + + // Create the user with the validated and sanitized data + User::create($validatedData); + + return redirect()->route('authors.index')->with('success', 'Author created successfully.'); + + } catch (ValidationException $e) { + // Redirect back with validation errors if validation fails + return redirect()->back()->withErrors($e->errors())->withInput(); } +} /** * Show the form for editing the specified resource.