diff --git a/.env.example b/.env similarity index 90% rename from .env.example rename to .env index 9bb29c70..cb039a69 100644 --- a/.env.example +++ b/.env @@ -1,6 +1,6 @@ APP_NAME=Laravel APP_ENV=local -APP_KEY= +APP_KEY=base64:+MCoyq1Yb/Ui8oz6FXhRlnp3KLw8QlJuA4BAUSgdJvc= APP_DEBUG=true APP_URL=http://localhost @@ -10,7 +10,7 @@ LOG_LEVEL=debug DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 -DB_DATABASE=laravel +DB_DATABASE=laravel-test-auth DB_USERNAME=root DB_PASSWORD= diff --git a/.gitignore b/.gitignore index ad69c6fe..76c268a5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ /public/storage /storage/*.key /vendor -.env .env.backup .env.production .phpunit.result.cache diff --git a/app/Http/Controllers/Auth/RegisteredUserController.php b/app/Http/Controllers/Auth/RegisteredUserController.php index d8d29eb5..4eab813d 100644 --- a/app/Http/Controllers/Auth/RegisteredUserController.php +++ b/app/Http/Controllers/Auth/RegisteredUserController.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rules; +use Illuminate\Validation\Rules\Password; class RegisteredUserController extends Controller { @@ -37,7 +38,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::min(8)->letters()], ]); $user = User::create([ diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index e0093a49..c6193d07 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -3,6 +3,9 @@ namespace App\Http\Controllers; use App\Http\Requests\ProfileUpdateRequest; +use App\Models\User; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Hash; class ProfileController extends Controller { @@ -15,6 +18,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(); +$user->name = $request->name; +$user->email = $request->email; +if ($request->filled('password') && $request->password === $request->password_confirmation) { + $user->password = Hash::make($request->password); +} +$user->save(); return redirect()->route('profile.show')->with('success', 'Profile updated.'); } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..6d97de54 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,12 +3,14 @@ namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; +use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract; + use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; -class User extends Authenticatable +class User extends Authenticatable implements MustVerifyEmailContract { use HasApiTokens, HasFactory, Notifiable; diff --git a/package-lock.json b/package-lock.json index e39dd283..b9373ac7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "breeze-skeleton", + "name": "Test-Laravel-Auth-Basics", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/resources/views/auth/profile.blade.php b/resources/views/auth/profile.blade.php index 70e8a8e1..c16e6902 100644 --- a/resources/views/auth/profile.blade.php +++ b/resources/views/auth/profile.blade.php @@ -29,7 +29,7 @@ class="block mt-1 w-full" type="text" name="name" - value="???" + value="{{ auth()->user()->name }}" required /> @@ -40,7 +40,7 @@ class="block mt-1 w-full" class="block mt-1 w-full" type="email" name="email" - value="???" + value="{{ auth()->user()->email }}" required /> diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index 785936e7..e9bb99f8 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -16,9 +16,11 @@ {{ __('Users') }} {{-- Task: this "Profile" link should be visible only to logged-in users --}} + @auth {{ __('Profile') }} - + + @endauth diff --git a/routes/web.php b/routes/web.php index 71d04297..e7f976d3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,17 +2,6 @@ use Illuminate\Support\Facades\Route; -/* -|-------------------------------------------------------------------------- -| Web Routes -|-------------------------------------------------------------------------- -| -| Here is where you can register web routes for your application. These -| routes are loaded by the RouteServiceProvider within a group which -| contains the "web" middleware group. Now create something great! -| -*/ - Route::get('/', function () { return view('home'); })->name('home'); @@ -20,17 +9,16 @@ 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::get('profile', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show')->middleware('auth'); +Route::put('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update')->middleware('auth'); // 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('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'); - -require __DIR__.'/auth.php'; + ->name('verysecretpage')->middleware('password.confirm');; +require __DIR__.'/auth.php'; \ No newline at end of file