Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into #89-unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna-Sokolowska committed May 10, 2022
2 parents dbcba11 + 9a47c51 commit 7d7edd2
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 38 deletions.
3 changes: 2 additions & 1 deletion database/seeders/DummyDataSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Blumilk\Meetup\Core\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;

class DummyDataSeeder extends Seeder
{
Expand All @@ -19,7 +20,7 @@ public function run(): void
$user = User::factory([
"name" => "Admin",
"email" => "[email protected]",
"password" => "password",
"password" => Hash::make("password"),
"email_verified_at" => Carbon::createFromDate(2022, 01, 01),
])->create();

Expand Down
2 changes: 1 addition & 1 deletion environment/dev/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/blumilksoftware/php:8.1
FROM ghcr.io/blumilksoftware/php:8.1.5

ARG XDEBUG_VERSION=3.1.2
ARG INSTALL_XDEBUG=false
Expand Down
2 changes: 1 addition & 1 deletion resources/views/user/password/forgot-password.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div>
<h1 class="text-xl">Forgot your password?</h1>
<p class="text-gray-400 text-sm mt-1">
Please enter the email adress and we will send you
Please enter the email address and we will send you
instructions to reset your password
</p>
</div>
Expand Down
8 changes: 6 additions & 2 deletions resources/views/user/password/reset-password.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<div>
<h1 class="text-xl">Reset Password</h1>
</div>
@if (!empty($error))
<div class="relative text-red-500 -top-6 left-1/2 transform -translate-x-1/2">
{{ $error }}
</div>
@endif
<div>
<input type="string" id="token" hidden="hidden" name="token" value="{{ $token }}">
<x-input-error for="token" />
Expand All @@ -19,8 +24,7 @@
Email
</label>
<div class="mt-1">
<input id="email" name="email" type="email" placeholder="[email protected]"
value="{{ $_GET['email'] }}" required
<input id="email" name="email" type="email" placeholder="[email protected]" value="{{ $email }}" required
class="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
readonly="readonly" />
<x-input-error for="email" />
Expand Down
12 changes: 12 additions & 0 deletions src/Exceptions/PasswordIsTheSameAsOldException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Blumilk\Meetup\Core\Exceptions;

use Exception;

class PasswordIsTheSameAsOldException extends Exception
{
protected $message = "Password cannot be the same as old password";
}
22 changes: 20 additions & 2 deletions src/Http/Controllers/Auth/PasswordResetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Blumilk\Meetup\Core\Http\Controllers\Auth;

use Blumilk\Meetup\Core\Exceptions\PasswordIsTheSameAsOldException;
use Blumilk\Meetup\Core\Http\Controllers\Controller;
use Blumilk\Meetup\Core\Http\Requests\PasswordReset\PasswordResetRequest;
use Blumilk\Meetup\Core\Http\Requests\PasswordReset\PasswordUpdateRequest;
Expand All @@ -12,6 +13,7 @@
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class PasswordResetController extends Controller
Expand Down Expand Up @@ -44,16 +46,32 @@ public function store(PasswordResetRequest $request): View|RedirectResponse
return back()->withErrors(["email" => __($status)]);
}

public function edit(PasswordResetRequest $request, string $token): View
public function edit(string $token, Request $request): View
{
return view("user.password.reset-password")->with(["email" => $request->validated("email"), "token" => $token]);
$email = $request->email;

return view("user.password.reset-password")->with([
"token" => $token,
"email" => $email,
]);
}

/**
* @throws \Illuminate\Auth\AuthenticationException
*/
public function update(PasswordUpdateRequest $request, PasswordResetService $service): RedirectResponse|View
{
try {
$service->validatePassword($request->get("password"), $request->get("email"));
} catch (PasswordIsTheSameAsOldException $exception) {
return view("user.password.reset-password")
->with([
"error" => $exception->getMessage(),
"token" => $request->validated("token"),
"email" => $request->validated("email"),
]);
}

$status = $service->resetPassword($request->validated());

if ($status === PasswordBroker::PASSWORD_RESET) {
Expand Down
15 changes: 7 additions & 8 deletions src/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@

use Blumilk\Meetup\Core\Http\Controllers\Controller;
use Blumilk\Meetup\Core\Http\Requests\Authentication\RegisterUserRequest;
use Blumilk\Meetup\Core\Models\User;
use Illuminate\Auth\Events\Registered;
use Blumilk\Meetup\Core\Services\UserRegisterService;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
public function create(): View
public function create(Request $request): View
{
if (request()->has("email")) {
return view("user.register")->with("email", request()->get("email"));
if ($request->has("email")) {
return view("user.register")->with("email", $request->email);
}

return view("user.register")->with("email", old("email"));
}

public function store(RegisterUserRequest $request): View
public function store(RegisterUserRequest $request, UserRegisterService $service): View
{
$user = User::query()->create($request->validated());
event(new Registered($user));
$service->register($request->validated("email"), $request->validated("name"), $request->validated("password"));

return view("user.registered");
}
Expand Down
20 changes: 0 additions & 20 deletions src/Observers/UserObserver.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
use Blumilk\Meetup\Core\Models\Contact;
use Blumilk\Meetup\Core\Models\Meetup;
use Blumilk\Meetup\Core\Models\NewsletterSubscriber;
use Blumilk\Meetup\Core\Models\User;
use Blumilk\Meetup\Core\Observers\ContactObserver;
use Blumilk\Meetup\Core\Observers\MeetupObserver;
use Blumilk\Meetup\Core\Observers\NewsletterSubscriberObserver;
use Blumilk\Meetup\Core\Observers\UserObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -26,7 +24,6 @@ class EventServiceProvider extends ServiceProvider

public function boot(): void
{
User::observe(UserObserver::class);
Contact::observe(ContactObserver::class);
Meetup::observe(MeetupObserver::class);
NewsletterSubscriber::observe(NewsletterSubscriberObserver::class);
Expand Down
14 changes: 14 additions & 0 deletions src/Services/Authentication/PasswordResetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Blumilk\Meetup\Core\Services\Authentication;

use Blumilk\Meetup\Core\Exceptions\PasswordIsTheSameAsOldException;
use Blumilk\Meetup\Core\Models\User;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\Passwords\PasswordBrokerManager;
use Illuminate\Contracts\Hashing\Hasher;
Expand All @@ -16,6 +18,18 @@ public function __construct(
protected Hasher $hash,
) {}

/**
* @throws PasswordIsTheSameAsOldException
*/
public function validatePassword(string $password, string $email): void
{
$user = User::query()->where("email", $email)->first();

if ($this->hash->check($password, $user?->password)) {
throw new PasswordIsTheSameAsOldException();
}
}

/**
* @throws AuthenticationException
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Authentication/UserLoginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public function __construct(
public function loginUser(string $email, string $password): void
{
$user = User::where("email", $email)->first();

if (!$this->hasher->check($password, $user?->password)) {
throw new AuthenticationException("Bad credentials");
}

$this->authManager->login($user);
$this->session->regenerate();
}
Expand Down
29 changes: 29 additions & 0 deletions src/Services/UserRegisterService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Blumilk\Meetup\Core\Services;

use Blumilk\Meetup\Core\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Hashing\Hasher;

class UserRegisterService
{
public function __construct(
protected Hasher $hasher,
) {}

public function register(string $email, string $name, string $password): void
{
$hashedPassword = $this->hasher->make($password);

$user = User::query()->firstOrCreate([
"email" => $email,
"name" => $name,
"password" => $hashedPassword,
]);

event(new Registered($user));
}
}

0 comments on commit 7d7edd2

Please sign in to comment.