diff --git a/app/Http/Controllers/RegisterController.php b/app/Http/Controllers/RegisterController.php new file mode 100644 index 0000000..decfa45 --- /dev/null +++ b/app/Http/Controllers/RegisterController.php @@ -0,0 +1,33 @@ +validate([ + 'name' => ['required'], + 'username' => ['required', 'max:255', 'min:4', 'unique:users,username'], + 'email' => ['required', 'email', 'max:255', 'unique:users,email'], + 'password' => ['required'] + ]); + $attribute['password'] = bcrypt($attribute['password']); + $attribute['username'] = ucwords($attribute['username']); + $user = User::create($attribute); + + Auth::login($user); + + session()->flash('success', 'Your account has been created'); + return redirect('/'); + } +} diff --git a/app/Http/Controllers/SessionsController.php b/app/Http/Controllers/SessionsController.php new file mode 100644 index 0000000..e0e526b --- /dev/null +++ b/app/Http/Controllers/SessionsController.php @@ -0,0 +1,31 @@ +validate([ + 'email' => ['required', 'email'], + 'password' => ['required'] + ]); + if (Auth::attempt($attributes)) { + return redirect('/')->with('success', 'Welcome Back!'); + } + return back()->withInput() + ->withErrors(['email' => 'credentials could not be verified']); + } + public function destroy() + { + Auth::logout(); + return redirect('/')->with('success', 'Goodbye'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index cb6737d..12f935c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -17,11 +17,7 @@ class User extends Authenticatable * * @var string[] */ - protected $fillable = [ - 'name', - 'email', - 'password', - ]; + protected $guarded = []; /** * The attributes that should be hidden for serialization. diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 3bd3c81..affd6da 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider * * @var string */ - public const HOME = '/home'; + public const HOME = '/'; /** * The controller namespace for the application. diff --git a/resources/views/components/layout.blade.php b/resources/views/components/layout.blade.php index ab0f55f..96e3e29 100644 --- a/resources/views/components/layout.blade.php +++ b/resources/views/components/layout.blade.php @@ -16,16 +16,24 @@ -
- Home Page - +
+ @auth + Welcome, {{ auth()->user()->username }} +
+ @csrf + +
+ @else + Register + Login + @endauth Subscribe for Updates
- {{ $slot}} + {{ $slot }}
@@ -55,4 +62,10 @@ class="transition-colors duration-300 bg-blue-500 hover:bg-blue-600 mt-4 lg:mt-0 + @if (session()->has('success')) +
+

{{ session('success') }}

+
+ @endif diff --git a/resources/views/register/create.blade.php b/resources/views/register/create.blade.php new file mode 100644 index 0000000..ff1f44e --- /dev/null +++ b/resources/views/register/create.blade.php @@ -0,0 +1,33 @@ + +
+
+

Register!

+
+ @csrf +
+ + + @error('username') +

{{ $message }}

+ @enderror +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+
+
+
+
diff --git a/resources/views/sessions/create.blade.php b/resources/views/sessions/create.blade.php new file mode 100644 index 0000000..1d35abd --- /dev/null +++ b/resources/views/sessions/create.blade.php @@ -0,0 +1,28 @@ + +
+
+

Log in!

+
+ @csrf +
+ + + @error('email') +

{{ $message }}

+ @enderror +
+
+ + + @error('password') +

{{ $message }}

+ @enderror +
+ +
+ +
+
+
+
+
diff --git a/routes/web.php b/routes/web.php index b51d9fd..2abc18c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,8 @@ name('home'); Route::get('/posts/{post:slug}', [PostController::class, 'show']); +Route::get('/register',[RegisterController::class, 'create'])->middleware('guest'); +Route::post('/register',[RegisterController::class, 'store'])->middleware('guest'); +Route::get('/logout',[SessionsController::class, 'destroy'])->middleware('auth'); +Route::get('/login',[SessionsController::class, 'create']); +Route::post('/login',[SessionsController::class, 'store']); + + + /**Route::get('/categories/{category:slug}', function(Category $category) { return view('posts',[