Laravel 7 Sign in | Dashboard Dynamic

Laravel 7 Sign in | Dashboard Dynamic


Laravel Authentication

1. Install Laravel Fresh Setup

To install a fresh Laravel project, run the following command:

composer create-project --prefer-dist laravel/laravel Blog

2. Setup Database

After installing Laravel, configure your database in the .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=your_database_name
DB_PORT=3306
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Then, migrate the tables into the database:

php artisan migrate

3. Define Routes

Open routes/web.php and add the following routes:

use App\Http\Controllers\AuthController;

Route::get('/', function () {
    return view('login');
});

Route::get('login', [AuthController::class, 'index']);
Route::post('post-login', [AuthController::class, 'postLogin']);
Route::get('registration', [AuthController::class, 'registration']);
Route::post('post-registration', [AuthController::class, 'postRegistration']);
Route::get('dashboard', [AuthController::class, 'dashboard']);
Route::get('logout', [AuthController::class, 'logout']);

4. Create Controller & Methods

Generate the authentication controller:

php artisan make:controller AuthController

Update app/Http/Controllers/AuthController.php with the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use App\Models\User;

class AuthController extends Controller
{
    public function index()
    {
        return view('login');
    }

    public function registration()
    {
        return view('registration');
    }
    
    public function postLogin(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            return redirect()->intended('dashboard');
        }

        return redirect('login')->withErrors(['email' => 'Invalid credentials']);
    }

    public function postRegistration(Request $request)
    {  
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
        
        $data = $request->all();
        User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
      
        return redirect('dashboard')->withSuccess('Successfully registered');
    }
    
    public function dashboard()
    {
        if (Auth::check()) {
            return view('dashboard');
        }
        return redirect('login')->withErrors(['access' => 'Unauthorized']);
    }

    public function logout()
    {
        Session::flush();
        Auth::logout();
        return redirect('login');
    }
}

5. Create Blade Views

Login Page (resources/views/login.blade.php)

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <form action="{{ url('post-login') }}" method="POST">
            {{ csrf_field() }}
            <h2>Login</h2>
            <div class="form-group">
                <input type="email" name="email" class="form-control" placeholder="Email" required>
            </div>
            <div class="form-group">
                <input type="password" name="password" class="form-control" placeholder="Password" required>
            </div>
            <button type="submit" class="btn btn-primary">Login</button>
        </form>
        <p>Don't have an account? <a href="{{ url('registration') }}">Register here</a></p>
    </div>
</body>
</html>

Registration Page (resources/views/registration.blade.php)

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <form action="{{ url('post-registration') }}" method="POST">
            {{ csrf_field() }}
            <h2>Register</h2>
            <div class="form-group">
                <input type="text" name="name" class="form-control" placeholder="Full Name" required>
            </div>
            <div class="form-group">
                <input type="email" name="email" class="form-control" placeholder="Email" required>
            </div>
            <div class="form-group">
                <input type="password" name="password" class="form-control" placeholder="Password" required>
            </div>
            <button type="submit" class="btn btn-primary">Register</button>
        </form>
    </div>
</body>
</html>

Dashboard Page (resources/views/dashboard.blade.php)

<!DOCTYPE html>
<html>
<head>
    <title>Dashboard</title>
</head>
<body>
    <h1>Welcome to the Dashboard!</h1>
    <a href="{{ url('logout') }}">Logout</a>
</body>
</html>

6. Run Development Server

Start the Laravel application:

php artisan serve

Your authentication system is now ready. Access it via http://127.0.0.1:8000.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close