Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete Auth tasks #480

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function store(Request $request)
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
'password' => ['required', 'confirmed', Rules\Password::defaults()->letters()],
]);

$user = User::create([
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Models\User;
use App\Http\Requests\ProfileUpdateRequest;

class ProfileController extends Controller
Expand All @@ -15,6 +16,13 @@ public function update(ProfileUpdateRequest $request)
{
// Task: fill in the code here to update name and email
// Also, update the password if it is set
$user = auth()->user();
if ($request->has('password')) {
$user->password = \bcrypt($request->password);
}
$user->name = $request->name;
$user->email = $request->email;
$user->save();

return redirect()->route('profile.show')->with('success', 'Profile updated.');
}
Expand Down
21 changes: 21 additions & 0 deletions app/Http/Middleware/VerifyEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Auth\Middleware\EnsureEmailIsVerified;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class VerifyEmail extends EnsureEmailIsVerified
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle($request, Closure $next, $redirectToRoute = "verification.notice")
{
return parent::handle($request, $next, $redirectToRoute);
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable;

Expand Down
38 changes: 12 additions & 26 deletions resources/views/auth/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">

<x-validation-errors class="mb-4" :errors="$errors"/>
<x-validation-errors class="mb-4" :errors="$errors" />

<x-alert.success></x-alert.success>

Expand All @@ -22,44 +22,30 @@
<em><b>Task:</b> replace ??? for name/email with logged in user's name/email</em>
<br /><br />

<x-label for="name" :value="__('Name')"/>
<x-label for="name" :value="__('Name')" />

{{-- Task: replace ??? for name/email with logged in user's name/email --}}
<x-input id="name"
class="block mt-1 w-full"
type="text"
name="name"
value="???"
required />
<x-input id="name" class="block mt-1 w-full" type="text" name="name"
value="{{ auth()->user()->name }}" required />
</div>

<div class="mt-4">
<x-label for="email" :value="__('Email')"/>
<x-label for="email" :value="__('Email')" />

<x-input id="email"
class="block mt-1 w-full"
type="email"
name="email"
value="???"
required />
<x-input id="email" class="block mt-1 w-full" type="email" name="email"
value="{{ auth()->user()->email }}" required />
</div>

<div class="mt-4">
<x-label for="password" :value="__('New password (if you want to change it)')"/>
<x-label for="password" :value="__('New password (if you want to change it)')" />

<x-input id="password"
class="block mt-1 w-full"
type="password"
name="password" />
<x-input id="password" class="block mt-1 w-full" type="password" name="password" />
</div>

<div class="mt-4">
<x-label for="password_confirmation" :value="__('New password confirmation')"/>
<x-label for="password_confirmation" :value="__('New password confirmation')" />

<x-input id="password_confirmation"
class="block mt-1 w-full"
type="password"
name="password_confirmation" />
<x-input id="password_confirmation" class="block mt-1 w-full" type="password"
name="password_confirmation" />
</div>

<x-button class="mt-4">
Expand Down
42 changes: 22 additions & 20 deletions resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- Logo -->
<div class="flex flex-shrink-0 items-center">
<a href="{{ route('home') }}">
<x-application-logo class="block w-auto h-10 text-gray-600 fill-current"/>
<x-application-logo class="block w-auto h-10 text-gray-600 fill-current" />
</a>
</div>

Expand All @@ -15,10 +15,11 @@
<x-nav-link :href="route('users.index')" :active="request()->routeIs('users.index')">
{{ __('Users') }}
</x-nav-link>
{{-- Task: this "Profile" link should be visible only to logged-in users --}}
@auth
<x-nav-link href="/profile" :active="request()->routeIs('profile.show')">
{{ __('Profile') }}
</x-nav-link>
@endauth
</div>
</div>

Expand All @@ -33,10 +34,10 @@ class="flex items-center text-sm font-medium text-gray-500 transition duration-1

<div class="ml-1">
<svg class="w-4 h-4 fill-current" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20">
viewBox="0 0 20 20">
<path fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd"/>
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd" />
</svg>
</div>
</button>
Expand All @@ -55,24 +56,25 @@ class="flex items-center text-sm font-medium text-gray-500 transition duration-1
</x-slot>
</x-dropdown>
@else
<a href="{{ route('login') }}" class="text-sm text-gray-700 underline dark:text-gray-500">Log in</a>
<a href="{{ route('login') }}" class="text-sm text-gray-700 underline dark:text-gray-500">Log in</a>

@if (Route::has('register'))
<a href="{{ route('register') }}" class="ml-4 text-sm text-gray-700 underline dark:text-gray-500">Register</a>
@endif
@if (Route::has('register'))
<a href="{{ route('register') }}"
class="ml-4 text-sm text-gray-700 underline dark:text-gray-500">Register</a>
@endif
@endauth
</div>

<!-- Hamburger -->
<div class="flex items-center -mr-2 sm:hidden">
<button @click="open = ! open"
class="inline-flex justify-center items-center p-2 text-gray-400 rounded-md transition duration-150 ease-in-out hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500">
class="inline-flex justify-center items-center p-2 text-gray-400 rounded-md transition duration-150 ease-in-out hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500">
<svg class="w-6 h-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path :class="{'hidden': open, 'inline-flex': ! open }" class="inline-flex"
stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M4 6h16M4 12h16M4 18h16"/>
<path :class="{'hidden': ! open, 'inline-flex': open }" class="hidden"
stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M4 6h16M4 12h16M4 18h16" />
<path :class="{'hidden': ! open, 'inline-flex': open }" class="hidden" stroke-linecap="round"
stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
Expand All @@ -83,7 +85,7 @@ class="inline-flex justify-center items-center p-2 text-gray-400 rounded-md tran
<div :class="{'block': open, 'hidden': ! open}" class="hidden sm:hidden">
<!-- Responsive Settings Options -->
<div class="pt-4 pb-1 border-t border-gray-200">
@auth
@auth
<div class="px-4">
<div class="text-base font-medium text-gray-800">{{ Auth::user()->name }}</div>
<div class="text-sm font-medium text-gray-500">{{ Auth::user()->email }}</div>
Expand All @@ -100,18 +102,18 @@ class="inline-flex justify-center items-center p-2 text-gray-400 rounded-md tran
</x-responsive-nav-link>
</form>
</div>
@else
@else
<div class="space-y-1">
<x-responsive-nav-link :href="route('login')">
{{ __('Login') }}
</x-responsive-nav-link>
@if (Route::has('register'))
<x-responsive-nav-link :href="route('register')">
{{ __('Register') }}
</x-responsive-nav-link>
<x-responsive-nav-link :href="route('register')">
{{ __('Register') }}
</x-responsive-nav-link>
@endif
</div>
@endauth
@endauth
</div>
</div>
</nav>
12 changes: 7 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
Route::get('users', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');

// Task: profile functionality should be available only for logged-in users
Route::get('profile', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
Route::put('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
Route::middleware(["auth"])->group(function () {
Route::get('profile', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
Route::put('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
});

// Task: this "/secretpage" URL should be visible only for those who VERIFIED their email
// Add some middleware here, and change some code in app/Models/User.php to enable this
Route::view('/secretpage', 'secretpage')
->name('secretpage');
->name('secretpage')->middleware(["auth", "verified"]);

// Task: this "/verysecretpage" URL should ask user for verifying their password once again
// You need to add some middleware here
Route::view('/verysecretpage', 'verysecretpage')
->name('verysecretpage');
->name('verysecretpage')->middleware("password.confirm");

require __DIR__.'/auth.php';
require __DIR__ . '/auth.php';
Loading