```bash sudo apt install -y composer composer create-project laravel/laravel auth-laravel cd auth-laravel php artisan serve cd auth-laravel vim .env php artisan migrate php artisan make:controller AuthController ``` ### Creamos controlador Funciones para el inicio de sesión, autenticación, registro, almacenamiento y cierre de sesión: ```php middleware('auth'); // Login Route::get('/login', [AuthController::class, 'login'])->name('login'); Route::post('/login', [AuthController::class, 'authenticate'])->name('auth.authenticate'); // Register Route::get('/register', [AuthController::class, 'register'])->name('auth.register'); Route::post('/register', [AuthController::class, 'store'])->name('auth.store'); // Logout Route::post('/logout', [AuthController::class, 'logout'])->name('auth.logout'); ``` ### Creamos vistas Usaremos la plantilla de ejemplo de bootstrap. Descargar el que contiene en el nombre `-example`: https://github.com/twbs/bootstrap/releases/ buscá `sign-in` Copiá todo el código `index.html` en `views/auth/login`. A continuación, copie todas las carpetas en `assets/dist` y `sign-in/signin.css` en `public`. No olvides copiar la foto de la marca a `public/img`. Después de eso, ajuste vistas/autorización/inicio de sesión. (Esto está en sign-in) ```php Signin Template · Bootstrap v5.1
@csrf

Please sign in

@error('email')
@enderror
@error('password')
@enderror

© 2017–2021

Signin Template · Bootstrap v5.1
@csrf

Please sign in

@error('email')
@enderror
@error('password')
@enderror

© 2017–2021

``` #### Registro: ```php Signin Template · Bootstrap v5.1
@csrf

Please sign in

@error('email')
@enderror
@error('name')
@enderror
@error('password')
@enderror
@error('confirm-password')
@enderror

© 2017–2021

``` #### Crear funciones de registro ```php use Illuminate\Support\Facades\Hash; use App\Models\User; public function store(Request $request){ $validate = $request->validate([ 'name' => 'required', 'email' => 'required|unique:users,email', 'password' => 'required', 'confirm-password' => 'required|same:password' ]); $data = $request->except('confirm-password', 'password'); $data['password'] = Hash::make($request->password); User::create($data); return redirect('/login'); } ``` ### Crear funciones de inicio de sesión https://laravel.com/docs/8.x/authentication#authentication-users. ```php use Illuminate\Support\Facades\Auth; public function authenticate(Request $request) { $credentials = $request->validate([ 'email' => ['required', 'email'], 'password' => ['required'], ]); if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended('/'); } return back()->withErrors([ 'email' => 'The provided credentials do not match our records.', ]); } ``` ### Crear características de cierre de sesión ```php public function logout(Request $request) { Auth::logout(); $request->session()->invalidate(); $request->session()->regenerateToken(); return redirect('/login'); } ``` Agregaremos un botón de cierre de sesión en la vista de bienvenida. ```php Laravel
@if (Route::has('login')) @endif
Laravel has wonderful, thorough documentation covering every aspect of the framework. Whether you are new to the framework or have previous experience with Laravel, we recommend reading all of the documentation from beginning to end.
Laracasts offers thousands of video tutorials on Laravel, PHP, and JavaScript development. Check them out, see for yourself, and massively level up your development skills in the process.
Laravel News is a community driven portal and newsletter aggregating all of the latest and most important news in the Laravel ecosystem, including new package releases and tutorials.
Vibrant Ecosystem
Laravel's robust library of first-party tools and libraries, such as Forge, Vapor, Nova, and Envoyer help you take your projects to the next level. Pair them with powerful open source libraries like Cashier, Dusk, Echo, Horizon, Sanctum, Telescope, and more.
Shop Sponsor
@csrf
Laravel v (PHP v)
```