0% found this document useful (0 votes)
10 views6 pages

Log in Manual

The document outlines the steps to set up a Laravel application with user authentication, including database creation, migrations, and model adjustments. It details the creation of an AuthController for login/logout functionality, routing for authentication, and the creation of views for login and dashboard. Additionally, it includes instructions for inserting users into the database and hashing passwords.

Uploaded by

montes.jose.8762
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Log in Manual

The document outlines the steps to set up a Laravel application with user authentication, including database creation, migrations, and model adjustments. It details the creation of an AuthController for login/logout functionality, routing for authentication, and the creation of views for login and dashboard. Additionally, it includes instructions for inserting users into the database and hashing passwords.

Uploaded by

montes.jose.8762
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Instalamos Laravel, creamos BD y configuramos .

env
composer create-project laravel/laravel 14login ^10.0

cd 14login

Creamos una base de datos y configuramos .env


BD: sistema2

2. Migraciones y Modelos
Laravel ya cuenta con un Modelo y Migracion para usuarios, editar:

2.1 Migración:

database/migrations/xxxx_xx_xx_create_users_table.php

$table->string('role')->default('user');

2.2 Editar modelo: app/Models/User.php


'role',

Agregando función de comprobación admin

Migrar:

php artisan migrate

base

3. Controlador AuthController

php artisan make:controller AuthController

Editar: app/Http/Controllers/AuthController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\Hash;

use App\Models\User;

class AuthController extends Controller

public function showLogin()

return view('auth.login');

public function login(Request $request)

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) {

$request->session()->regenerate();

return redirect()->intended('dashboard');

return back()->withErrors(['email' => 'Credenciales inválidas'])->withInput();

public function logout(Request $request)

Auth::logout();
$request->session()->invalidate();

$request->session()->regenerateToken();

return redirect('/login');

4. Creando rutas

En routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\AuthController;

Route::get('/login', [AuthController::class, 'showLogin'])->name('login');

Route::post('/login', [AuthController::class, 'login']);

Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Página protegida

Route::middleware('auth')->group(function () {

Route::get('/dashboard', function () {

return view('dashboard');

});

});

5. Creando vistas

Creamos: resources/views/auth/login.blade.php

<!DOCTYPE html>

<html>

<head>
<title>Login</title>

</head>

<body>

<h2>Iniciar Sesión</h2>

@if ($errors->any())

<div style="color:red;">

{{ $errors->first() }}

</div>

@endif

<form method="POST" action="/login">

@csrf

<label>Email:</label>

<input type="email" name="email" value="{{ old('email') }}"><br><br>

<label>Contraseña:</label>

<input type="password" name="password"><br><br>

<button type="submit">Ingresar</button>

</form>

</body>

</html>

Creamos: resources/views/dashboard.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Dashboard</title>

</head>

<body>
<h1>Bienvenido {{ auth()->user()->name }}</h1>

<p>Rol: {{ auth()->user()->role }}</p>

<form method="POST" action="{{ route('logout') }}">

@csrf

<button type="submit">Cerrar sesión</button>

</form>

</body>

</html>

6. Insertamos usuarios
INSERT INTO users (name, email, password, role, created_at, updated_at) VALUES

('Admin Master', '[email protected]',


'$2y$10$aGZHi7Fb02O4k4bDe2GEieQgExUL3QZK4EXY4F1ZsyoQDjmiTgAFO', 'admin', NOW(),
NOW()),

('Usuario Normal', '[email protected]',


'$2y$10$aGZHi7Fb02O4k4bDe2GEieQgExUL3QZK4EXY4F1ZsyoQDjmiTgAFO', 'user', NOW(),
NOW()),

('Supervisor General', '[email protected]',


'$2y$10$aGZHi7Fb02O4k4bDe2GEieQgExUL3QZK4EXY4F1ZsyoQDjmiTgAFO', 'supervisor',
NOW(), NOW());

Accedemos a /login

7. Contraseñas

php artisan tinker

Hash::make('123456')

You might also like