Merge pull request #138 from inertiajs/simplify-auth

Replace laravel/ui with simplified auth
This commit is contained in:
Jonathan Reinink 2021-05-11 09:50:20 -04:00 committed by GitHub
commit 4bd372d7a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 117 deletions

View File

@ -0,0 +1,53 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
class AuthenticatedSessionController extends Controller
{
/**
* Display the login view.
*
* @return \Inertia\Response
*/
public function create()
{
return Inertia::render('Auth/Login');
}
/**
* Handle an incoming authentication request.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
return redirect()->intended(RouteServiceProvider::HOME);
}
/**
* Destroy an authenticated session.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Request $request)
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');
}
}

View File

@ -1,41 +0,0 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Inertia\Inertia;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Show the application's login form.
*
* @return \Inertia\Response
*/
public function showLoginForm()
{
return Inertia::render('Auth/Login');
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace App\Http\Requests\Auth;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
class LoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|string|email',
'password' => 'required|string',
];
}
/**
* Attempt to authenticate the request's credentials.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function authenticate()
{
$this->ensureIsNotRateLimited();
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
/**
* Ensure the login request is not rate limited.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function ensureIsNotRateLimited()
{
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
return;
}
event(new Lockout($this));
$seconds = RateLimiter::availableIn($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.throttle', [
'seconds' => $seconds,
'minutes' => ceil($seconds / 60),
]),
]);
}
/**
* Get the rate limiting throttle key for the request.
*
* @return string
*/
public function throttleKey()
{
return Str::lower($this->input('email')).'|'.$this->ip();
}
}

View File

@ -15,7 +15,6 @@
"inertiajs/inertia-laravel": "^0.4",
"laravel/framework": "^8.40",
"laravel/tinker": "^2.5",
"laravel/ui": "^2.0",
"league/glide-laravel": "^1.0",
"tightenco/ziggy": "^0.8.0"
},

61
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "3b6210b9a1903f968a3c773d9a60aad9",
"content-hash": "669badae23389c95a5f7e1890b965ae9",
"packages": [
{
"name": "asm89/stack-cors",
@ -1299,65 +1299,6 @@
},
"time": "2021-03-02T16:53:12+00:00"
},
{
"name": "laravel/ui",
"version": "v2.3.0",
"source": {
"type": "git",
"url": "https://github.com/laravel/ui.git",
"reference": "2ccaa3b821ea8ac7e05393b946d0578bdb46099b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/ui/zipball/2ccaa3b821ea8ac7e05393b946d0578bdb46099b",
"reference": "2ccaa3b821ea8ac7e05393b946d0578bdb46099b",
"shasum": ""
},
"require": {
"illuminate/console": "^7.0|^8.0",
"illuminate/filesystem": "^7.0|^8.0",
"illuminate/support": "^7.0|^8.0",
"php": "^7.2.5"
},
"require-dev": {
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Laravel\\Ui\\UiServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Laravel\\Ui\\": "src/",
"Illuminate\\Foundation\\Auth\\": "auth-backend/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "Laravel UI utilities and presets.",
"keywords": [
"laravel",
"ui"
],
"support": {
"issues": "https://github.com/laravel/ui/issues",
"source": "https://github.com/laravel/ui/tree/v2.3.0"
},
"time": "2020-09-09T12:07:59+00:00"
},
{
"name": "league/commonmark",
"version": "1.6.1",

View File

@ -13,9 +13,8 @@
<span class="text-sm">Remember Me</span>
</label>
</div>
<div class="px-10 py-4 bg-gray-100 border-t border-gray-100 flex justify-between items-center">
<a class="hover:underline" tabindex="-1" href="#reset-password">Forgot password?</a>
<loading-button :loading="form.processing" class="btn-indigo" type="submit">Login</loading-button>
<div class="px-10 py-4 bg-gray-100 border-t border-gray-100 flex">
<loading-button :loading="form.processing" class="ml-auto btn-indigo" type="submit">Login</loading-button>
</div>
</form>
</div>
@ -45,12 +44,7 @@ export default {
},
methods: {
login() {
this.form
.transform(data => ({
...data,
remember: data.remember ? 'on' : '',
}))
.post(this.route('login.attempt'))
this.form.post(this.route('login.store'))
},
},
}

View File

@ -28,7 +28,7 @@
<div slot="dropdown" class="mt-2 py-2 shadow-xl bg-white rounded text-sm">
<inertia-link class="block px-6 py-2 hover:bg-indigo-500 hover:text-white" :href="route('users.edit', $page.props.auth.user.id)">My Profile</inertia-link>
<inertia-link class="block px-6 py-2 hover:bg-indigo-500 hover:text-white" :href="route('users')">Manage Users</inertia-link>
<inertia-link class="block px-6 py-2 hover:bg-indigo-500 hover:text-white w-full text-left" :href="route('logout')" method="post" as="button">Logout</inertia-link>
<inertia-link class="block px-6 py-2 hover:bg-indigo-500 hover:text-white w-full text-left" :href="route('logout')" method="delete" as="button">Logout</inertia-link>
</div>
</dropdown>
</div>

View File

@ -1,6 +1,6 @@
<?php
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\ContactsController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ImagesController;
@ -22,15 +22,15 @@ use Illuminate\Support\Facades\Route;
// Auth
Route::get('login', [LoginController::class, 'showLoginForm'])
Route::get('login', [AuthenticatedSessionController::class, 'create'])
->name('login')
->middleware('guest');
Route::post('login', [LoginController::class, 'login'])
->name('login.attempt')
Route::post('login', [AuthenticatedSessionController::class, 'store'])
->name('login.store')
->middleware('guest');
Route::post('logout', [LoginController::class, 'logout'])
Route::delete('logout', [AuthenticatedSessionController::class, 'destroy'])
->name('logout');
// Dashboard