Skip to content

Commit

Permalink
refactor: replace User facades with IoC
Browse files Browse the repository at this point in the history
  • Loading branch information
Ion Babin committed Apr 27, 2024
1 parent 7dcf7e1 commit b8318df
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
33 changes: 19 additions & 14 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Illuminate\View\View;

class LoginController extends Controller
{
public function index(Request $request): RedirectResponse
{
$user = User::whereToken($request->token)->firstOrFail();
private User $user;

Auth::login($user);
public function __construct(User $user) {
$this->user = $user;
}

return redirect(route('ads.index'));
public function index(Request $request): RedirectResponse
{
$userWithToken = $this->user->whereToken($request->token)->firstOrFail();
auth()->login($userWithToken);
return $this->redirectHome();
}

public function create(): View
Expand All @@ -29,21 +31,24 @@ public function create(): View

public function store(Request $request): RedirectResponse
{
$user = User::whereEmail($request->email)->first();
if ($user && $user->exists) {
$user->update(['token' => Str::random(40)]);
$user = $this->user->whereEmail($request->email)->first();
if ($user !== null) {
$user->setLoginToken();
RequestedLogin::dispatch($user);
}

return redirect()->route('ads.index')->with('success', 'User created successfuly');
return $this->redirectHome()->with('success', 'User created successfuly');
}

public function delete(Request $request): RedirectResponse
{
Auth::logout();

auth()->logout();
$request->getSession()->invalidate();
return $this->redirectHome();
}

return redirect(route('ads.index'));
private function redirectHome(): RedirectResponse
{
return redirect()->route('ads.index');
}
}
28 changes: 12 additions & 16 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,32 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\User\RegisterRequest;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Illuminate\View\View;

class RegisterController extends Controller
{
public function index(): View
private User $user;

public function __construct(User $user)
{
return view('auth.register');
$this->user = $user;
}

public function store(Request $request): RedirectResponse
public function store(RegisterRequest $request): RedirectResponse
{
$request->validate([
'nickname' => ['required', 'string', 'max:25', 'unique:users'],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255'],
]);

// check if email is unique,
// but don't display any error message to avoid phishing
if (! User::where('email', $request->email)->exists()) {
User::create([
'nickname' => $request->nickname,
'email' => $request->email,
'token' => Str::random(40),
'token_generated_at' => Date::now(),
]);
if (!$this->user->whereEmail($request->validated()["email"])->exists()) {
$this->user->create($request->validated() +
[
'token' => Str::random(40),
'token_generated_at' => Date::now(),
]);
}

// also generate some sort of notification (later)
Expand Down
21 changes: 21 additions & 0 deletions app/Http/Requests/User/RegisterRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Requests\User;

use Illuminate\Foundation\Http\FormRequest;

class RegisterRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

public function rules(): array
{
return [
'nickname' => ['required', 'string', 'max:25', 'unique:users'],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255'],
];
}
}
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
Expand Down Expand Up @@ -51,4 +52,9 @@ public function ads(): HasMany
{
return $this->hasMany(Ad::class);
}

public function setLoginToken(): null
{
$this->update(['token' => Str::random(40)]);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.1",
"php": "^8.2",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
Expand Down

0 comments on commit b8318df

Please sign in to comment.