Skip to content

Commit

Permalink
Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
wasilolly committed Oct 7, 2021
1 parent c54d309 commit 6a774d0
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 13 deletions.
33 changes: 33 additions & 0 deletions app/Http/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\Auth;

class RegisterController extends Controller
{

public function create()
{
return view('register.create');
}

public function store()
{
$attribute = request()->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('/');
}
}
31 changes: 31 additions & 0 deletions app/Http/Controllers/SessionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\View\ViewName;

class SessionsController extends Controller
{
public function create()
{
return view('sessions.create');
}
public function store()
{
$attributes = request()->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');
}
}
6 changes: 1 addition & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/home';
public const HOME = '/';

/**
* The controller namespace for the application.
Expand Down
27 changes: 20 additions & 7 deletions resources/views/components/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@
</a>
</div>

<div class="mt-8 md:mt-0">
<a href="/" class="text-xs font-bold uppercase">Home Page</a>

<div class="mt-8 md:mt-0 inline-flex">
@auth
<span class="text-xs font-bold uppercase">Welcome, {{ auth()->user()->username }}</span>
<form action="/logout" method="post" class="text-xs font-semibold text-blue-500 ml-6">
@csrf
<button type="submit">logout</button>
</form>
@else
<a href="/register" class="text-xs font-bold uppercase">Register</a>
<a href="/login" class="ml-3 text-xs font-bold uppercase">Login</a>
@endauth
<a href="#" class="bg-blue-500 ml-3 rounded-full text-xs font-semibold text-white uppercase py-3 px-5">
Subscribe for Updates
</a>
</div>
</nav>

{{ $slot}}
{{ $slot }}

<footer class="bg-gray-100 border border-black border-opacity-5 rounded-xl text-center py-16 px-10 mt-16">
<img src="./images/lary-newsletter-icon.svg" alt="" class="mx-auto -mb-6" style="width: 145px;">
Expand All @@ -42,17 +50,22 @@
</label>

<input id="email" type="text" placeholder="Your email address"
class="lg:bg-transparent py-2 lg:py-0 pl-4 focus-within:outline-none">
class="lg:bg-transparent py-2 lg:py-0 pl-4 focus-within:outline-none">
</div>

<button type="submit"
class="transition-colors duration-300 bg-blue-500 hover:bg-blue-600 mt-4 lg:mt-0 lg:ml-3 rounded-full text-xs font-semibold text-white uppercase py-3 px-8"
>
class="transition-colors duration-300 bg-blue-500 hover:bg-blue-600 mt-4 lg:mt-0 lg:ml-3 rounded-full text-xs font-semibold text-white uppercase py-3 px-8">
Subscribe
</button>
</form>
</div>
</div>
</footer>
</section>
@if (session()->has('success'))
<div x-data="{ show:true}" x-init="setTimeout(() => show = false, 4000)" x-show="show"
class="fixed bg-blue-500 text-white py-2 px-4 rounded-xl bottom-3 text-sm">
<p>{{ session('success') }}</p>
</div>
@endif
</body>
33 changes: 33 additions & 0 deletions resources/views/register/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<x-layout>
<section class="px-6 py-8">
<main class="max-w-lg mx-auto mt-10 bg-gray-100 p-6 rounded-xl">
<h1 class="text-center font-bold text-xl">Register!</h1>
<form action="/register" method="post">
@csrf
<div class="mb-6">
<label class="block mb-2 uppercase font-bold text-xs text-gray-700 mt-5" for="username">Username</label>
<input class="border border-gray-400 p-2 w-full" type="text" name="username" id="username" value="{{old('username')}}">
@error('username')
<p class="text-red-500 text-xs mt-1">{{ $message }}</p>
@enderror
</div>
<div class="mb-6">
<label class="block mb-2 uppercase font-bold text-xs text-gray-700 mt-5" for="name">Name</label>
<input class="border border-gray-400 p-2 w-full" type="text" name="name" id="name" value="{{old('name')}}">
</div>
<div class="mb-6">
<label class="block mb-2 uppercase font-bold text-xs text-gray-700 mt-5" for="email">Email</label>
<input class="border border-gray-400 p-2 w-full" type="email" name="email" id="email" value="{{old('email')}}">
</div>
<div class="mb-6">
<label class="block mb-2 uppercase font-bold text-xs text-gray-700 mt-5" for="password">Password</label>
<input class="border border-gray-400 p-2 w-full" type="password" name="password" id="password">
</div>

<div class="mb-6">
<button class="text-center bg-blue-400 text-white rounded py-2 px-4 hover:bg-blue-500" type="submit">Submit</button>
</div>
</form>
</main>
</section>
</x-layout>
28 changes: 28 additions & 0 deletions resources/views/sessions/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<x-layout>
<section class="px-6 py-8">
<main class="max-w-lg mx-auto mt-10 bg-gray-100 p-6 rounded-xl">
<h1 class="text-center font-bold text-xl">Log in!</h1>
<form action="/login" method="post">
@csrf
<div class="mb-6">
<label class="block mb-2 uppercase font-bold text-xs text-gray-700 mt-5" for="email">Email</label>
<input class="border border-gray-400 p-2 w-full" type="email" name="email" id="email" value="{{old('email')}}">
@error('email')
<p class="text-red-500 text-xs mt-1">{{ $message }}</p>
@enderror
</div>
<div class="mb-6">
<label class="block mb-2 uppercase font-bold text-xs text-gray-700 mt-5" for="password">Password</label>
<input class="border border-gray-400 p-2 w-full" type="password" name="password" id="password">
@error('password')
<p class="text-red-500 text-xs mt-1">{{ $message }}</p>
@enderror
</div>

<div class="mb-6">
<button class="text-center bg-blue-400 text-white rounded py-2 px-4 hover:bg-blue-500" type="submit">Log in</button>
</div>
</form>
</main>
</section>
</x-layout>
10 changes: 10 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use App\Http\Controllers\PostController;
use App\Http\Controllers\RegisterController;
use App\Http\Controllers\SessionsController;
use Illuminate\Support\Facades\Route;


Expand All @@ -17,6 +19,14 @@

Route::get('/', [PostController::class, 'index'])->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',[
Expand Down

0 comments on commit 6a774d0

Please sign in to comment.