Authentication Tutorial in Laravel 11

 <?php


namespace App\Http\Controllers;

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

class UserController extends Controller
{
    public function register(Request $request)
    {
        $data = $request->validate([
            "name" => "required",
            "email" => "required|email",
            "password" => "required|confirmed"
        ]);

        // if above validation form field names like "name","email","password" and database column names both are same
        // then only we can save data in database by passing "$data" variable in create() method direct as shown below :
        $user = User::create($data);

        // when data is created in database at that time the id will return in "$user" variable
        if ($user) {
            return redirect()->route("login");
        }
    }

    public function login(Request $request)
    {
        $credentials = $request->validate([
            "email" => "required",
            "password" => "required"
        ]);

        if (Auth::attempt($credentials)) {
            return redirect()->route("dashboard");
        }
    }

    public function dashboardPage()
    {
        if (Auth::check()) {
            return view("dashboard");
        } else {
            return redirect()->route("login");
        }
    }

    // check() and guest() both methods are opposite to each other
    // guest() method checks if user not login then only condition will true
    // check() method checks if user login then only condition will true
    public function innerPage()
    {
        if (Auth::check()) {
            return view("inner");
        } else {
            return redirect()->route("login");
        }
    }

    // Generally when user login at that time session will create and all sessions are stored in storage/framework/sessions folder in Laravel 11
    public function logout()
    {
        Auth::logout(); //Auth::logout() will remove or destroy session
        return view("login");
    }
}
Above File is app\Http\Controllers\UserController File





Below File is app\Models\User File
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable;
    public $timestamps = false;
    protected $table = "users";

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'password' => 'hashed',
        ];
    }
}





Below File is database\migrations\2024_12_23_102902_create_users_table File
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string("name", 255);
            $table->string("email", 50)->unique();
            $table->string("password", 255);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
    }
};





Below File is resources\views\dashboard File
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Dashboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-5 mt-5">
                <div class="card">
                    <div class="card-header">
                        <h1>Welcome, {{ Auth::user()->name }}</h1>
                    </div>
                    <div class="card-body">
                        {{ Auth::user() }}
                        <a href="{{ route("inner") }}" class="btn btn-primary mt-5">Go to Inner Page</a>
                        <a href="{{ route("login") }}" class="btn btn-danger mt-5">Logout</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>




Below File is resources\views\inner File
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Inner Page</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-5 mt-5">
                <div class="card">
                    <div class="card-header">
                        <h3>Inner Page</h3>
                    </div>
                    <div class="card-body">
                        <a href="{{ route("dashboard") }}" class="btn btn-primary">Back to Dashboard</a>
                        <a href="{{ route("login") }}" class="btn btn-danger">Logout</a><br><br>
                        @if(auth()->check())
                            Id : {{ auth()->id() }},
                            Name : {{ Auth()->user()->name }},
                            Email : {{ Auth()->user()->email }}
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>




Below File is resources\views\login File
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Login</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-5 mt-5">
                <div class="card">
                    <div class="card-header">
                        <h3>Login</h3>
                    </div>
                    <div class="card-body">
                        <form action="{{ route("loginMatch") }}" method="POST">
                            @csrf
                            <div class="mb-3">
                                <label for="useremail" class="form-label">Email Address</label>
                                <input type="email" value="{{ old("email") }}" name="email" class="form-control @error("email") is-invalid @enderror" id="useremail"/>
                                <span class="text-danger">
                                    @error("email")
                                        {{ $message }}
                                    @enderror
                                </span>
                            </div>
                            <div class="mb-3">
                                <label for="userpassword" class="form-label">Password</label>
                                <input type="password" value="{{ old("password") }}" name="password" class="form-control @error("password") is-invalid @enderror" id="userpassword"/>
                                <span class="text-danger">
                                    @error("password")
                                        {{ $message }}
                                    @enderror
                                </span>
                            </div>
                            <button type="submit" class="btn btn-primary">Login</button>
                            <a href="{{ route("welcomeTwo") }}" class="btn btn-secondary">Back</a>
                        </form>
                    </div>

                    @if($errors->any())
                    <div class="alert alert-danger">
                        <ul>
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</body>
</html>




Below File is resources\views\register File
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Register</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-5 mt-5">
                <div class="card">
                    <div class="card-header">
                        <h3>Register</h3>
                    </div>
                    <div class="card-body">
                        <form action="{{ route("registerSave") }}" method="POST">
                            @csrf
                            <div class="mb-3">
                                <label for="username" class="form-label">Name</label>
                                <input type="text" value="{{ old("name") }}" name="name" class="form-control @error("name") is-invalid @enderror" id="username"/>
                                <span class="text-danger">
                                    @error("name")
                                        {{ $message }}
                                    @enderror
                                </span>
                            </div>
                            <div class="mb-3">
                                <label for="useremail" class="form-label">Email address</label>
                                <input type="email" value="{{ old("email") }}" name="email" class="form-control @error("email") is-invalid @enderror" id="useremail"/>
                                <span class="text-danger">
                                    @error("email")
                                        {{ $message }}
                                    @enderror
                                </span>
                            </div>
                            <div class="mb-3">
                                <label for="userpassword" class="form-label">Password</label>
                                <input type="password" value="{{ old("password") }}" name="password" class="form-control @error("password") is-invalid @enderror" id="userpassword"/>
                                <span class="text-danger">
                                    @error("password")
                                        {{ $message }}
                                    @enderror
                                </span>
                            </div>
                            <div class="mb-3">
                                <label for="userpassword-confirm" class="form-label">Confirm Password</label>
                                <input type="password" value="{{ old("password_confirmation") }}" name="password_confirmation" class="form-control @error("password-confirmation") is-invalid @enderror" id="userpassword-confirm"/>
                                <span class="text-danger">
                                    @error("password_confirmation")
                                        {{ $message }}
                                    @enderror
                                </span>
                            </div>
                            <button type="submit" class="btn btn-primary">Register</button>
                            <a href="{{ route("welcomeTwo") }}" class="btn btn-secondary">Back</a>
                        </form>
                    </div>

                    @if($errors->any())
                    <div class="card-footer text-body-secondary">
                        <div class="alert alert-danger">
                            <ul>
                                @foreach($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</body>
</html>




Below File is resources\views\welcomeTwo File
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>welcomeTwo</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-5 mt-5">
                <div class="card">
                    <div class="card-header">
                        <h1 class="mb-5">Laravel Authentication</h1>
                    </div>
                    <div class="card-body">
                        <a href="{{ route("register") }}" class="btn btn-primary">Register</a>
                        <a href="{{ route("login") }}" class="btn btn-primary">Login</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>




Below File is routes\web File
<?php

use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

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

Route::view("welcomeTwo", "welcomeTwo")->name("welcomeTwo");

Route::view("/register", "register")->name("register");

// Route::post("registerSave", [UserController::class, "register"])->name("registerSave");

Route::view("/login", "login")->name("login");

Route::controller(UserController::class)->group(function () {
    Route::post("registerSave", "register")->name("registerSave");
    Route::post("loginMatch", "login")->name("loginMatch");
    Route::get("dashboard", "dashboardPage")->name("dashboard");
    Route::get("dashboard/inner", "innerPage")->name("inner");
    Route::get("logout", "logout")->name("logout");
});











Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Eloquent with JSON Data Columns Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11