Skip to content

Commit

Permalink
Login
Browse files Browse the repository at this point in the history
  • Loading branch information
ucan-lab committed Jun 30, 2023
1 parent b71ef1e commit 8cb316b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/app/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use Acme\Domain\User\Username;
use Acme\Domain\User\UserRepository;
use App\Aws\CognitoIdentityProvider\AdminInitiateAuth\AdminInitiateAuth;
use App\Aws\CognitoIdentityProvider\AdminInitiateAuth\AdminInitiateAuthPayload;
use App\Http\Requests\LoginRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

final readonly class LoginController
{
public function __construct(
private UserRepository $userRepository,
private AdminInitiateAuth $adminInitiateAuth,
) {
}

public function __invoke(LoginRequest $request, AdminInitiateAuth $adminInitiateAuth): RedirectResponse
{
$user = $this->userRepository->findByUsername(new Username($request->username()));

$payload = AdminInitiateAuthPayload::createForAdminUserPasswordAuthFlow($user->username(), $request->password());
$this->adminInitiateAuth->execute($payload);

Auth::loginUsingId($user->userId());
Session::regenerate();
Session::regenerateToken();

return redirect()->route('dashboard');
}
}
28 changes: 28 additions & 0 deletions src/app/Http/Requests/LoginRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

final class LoginRequest extends FormRequest
{
public function rules(): array
{
return [
'username' => ['required', 'string'],
'password' => ['required', 'string'],
];
}

public function username(): string
{
return $this->input('username');
}

public function password(): string
{
return $this->input('password');
}
}
14 changes: 14 additions & 0 deletions src/resources/views/login.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@extends('layouts.app')

@section('title', 'ログイン')

@section('content')
<h1>ログイン</h1>

<form action="{{ route('login') }}" method="POST">
@csrf
<p><label>ユーザー名: <input type="text" name="username"></label></p>
<p><label>パスワード: <input type="password" name="password"></label></p>
<button type="submit">ログイン</button>
</form>
@endsection
3 changes: 3 additions & 0 deletions src/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use App\Http\Controllers\LoginController;
use App\Http\Controllers\RegisterController;
use Illuminate\Support\Facades\Route;

Expand All @@ -20,6 +21,8 @@

Route::view('/register', 'register')->name('registerForm');
Route::post('/register', RegisterController::class)->name('register');
Route::view('/login', 'login')->name('loginForm');
Route::post('/login', LoginController::class)->name('login');


Route::group(['middleware' => 'auth'], static function () {
Expand Down

0 comments on commit 8cb316b

Please sign in to comment.