Skip to content

mikelezc/laravel-authentication-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Laravel Authentication Example - Educational Django to Laravel Translation

This project is a pedagogical translation of Django's authentication system to Laravel, designed to learn Laravel by comparing it with Django/Python concepts.

๐Ÿš€ QUICK START (Recommended)

Option 1: Automated Setup with manage.sh (Easiest)

For first-time setup after cloning from GitHub:

# Make the script executable (if needed)
chmod +x manage.sh

# Run the interactive manager
./manage.sh

# Select option 9: "๐Ÿš€ Configuraciรณn inicial completa"

This will automatically:

  • โœ… Check PHP and Composer dependencies
  • โœ… Install Laravel dependencies
  • โœ… Create .env file from template
  • โœ… Generate application key (Laravel's SECRET_KEY)
  • โœ… Setup SQLite database
  • โœ… Run migrations
  • โœ… Create test users
  • โœ… Start the server automatically!
  • โœ… Open browser automatically (if possible)

Option 2: Manual Setup

# Install PHP dependencies with Composer
composer install

# Copy environment configuration
cp .env.example .env

# Generate application key (equivalent to Django's SECRET_KEY)
php artisan key:generate

# Create SQLite database file
touch database/database.sqlite

# Run migrations to create tables
php artisan migrate

# Seed database with test users
php artisan db:seed --class=UserSeeder

๐ŸŽฏ Start the Server

# Using manage.sh (recommended)
./manage.sh
# Then select option 1: "๐Ÿš€ Iniciar servidor Laravel"

# OR manually
php artisan serve

# Check server status anytime
./check-server.sh

๐ŸŽฏ Ready to Test!

Test Users:

  • Username: admin | Email: admin@example.com | Password: password123
  • Username: testuser | Email: testuser@example.com | Password: password123

๐Ÿ“š Django vs Laravel Comparison

๐Ÿ—‚๏ธ Routing (URLs)

Aspect Django Laravel
File urls.py routes/web.php
Syntax path('login/', login_view, name='login') Route::get('login', [AuthController::class, 'showLogin'])->name('login')
Parameters path('user/<int:id>/', user_detail) Route::get('user/{id}', [UserController::class, 'show'])
Groups include() or path() with prefix Route::prefix('auth')->group()

๐ŸŽฎ Controllers/Views

Aspect Django Laravel
File views.py app/Http/Controllers/AuthController.php
Function def login(request): public function login(Request $request)
Request request.POST.get('username') $request->input('username')
Response return render(request, 'template.html') return view('template')
Redirect return redirect('home') return redirect()->route('home')

๐Ÿ—„๏ธ Models/ORM

Aspect Django Laravel
File models.py app/Models/User.php
Definition class User(AbstractUser): class User extends Authenticatable
Fields email = models.EmailField() protected $fillable = ['email']
Queries User.objects.all() User::all()
Create User.objects.create() User::create()
Filter User.objects.filter(active=True) User::where('active', true)->get()

๐Ÿ—ƒ๏ธ Migrations

Aspect Django Laravel
Create python manage.py makemigrations php artisan make:migration
Run python manage.py migrate php artisan migrate
Rollback python manage.py migrate app 0001 php artisan migrate:rollback
Status python manage.py showmigrations php artisan migrate:status

๐ŸŽจ Templates

Aspect Django Laravel Blade
Inheritance {% extends 'base.html' %} @extends('layouts.app')
Blocks {% block content %}{% endblock %} @section('content') @endsection
Variables {{ user.username }} {{ Auth::user()->username }}
Conditionals {% if user.is_authenticated %} @auth @endauth
Loops {% for item in items %} @foreach($items as $item)
CSRF {% csrf_token %} @csrf

๐Ÿ›ก๏ธ Authentication

Aspect Django Laravel
Login auth_login(request, user) Auth::login($user)
Logout auth_logout(request) Auth::logout()
Current User request.user Auth::user()
Is Authenticated? user.is_authenticated Auth::check()
Protect Routes @login_required decorator ->middleware('auth')

๐Ÿ”’ Middleware/Decorators

Aspect Django Laravel
Login Required @login_required ->middleware('auth')
Guest Only @unauthenticated_user ->middleware('guest')
CSRF Protection @csrf_protect Automatic (can exclude with middleware)
Rate Limiting Custom middleware ->middleware('throttle:5,1')

๐Ÿš€ Implemented Features

  • โœ… Login with username/email and password
  • โœ… Secure logout
  • โœ… Rate limiting (brute force protection)
  • โœ… Remember me functionality
  • โœ… CSRF protection
  • โœ… Authentication middleware
  • โœ… Protected dashboard
  • โœ… User migration
  • โœ… Database seeders with test data
  • โœ… Responsive templates with Bootstrap
  • โœ… Flexible authentication (username or email)
  • โœ… Flash messages
  • โœ… Error handling and validation

๐Ÿ“ Test Users

After running php artisan db:seed --class=UserSeeder:

Username Email Password
admin admin@example.com password123
testuser testuser@example.com password123

๐ŸŽ“ Key Learning Concepts

1. Artisan CLI vs Django manage.py

# Laravel                          # Django
php artisan migrate                # python manage.py migrate
php artisan make:model User        # Edit models.py manually
php artisan serve                  # python manage.py runserver
php artisan tinker                 # python manage.py shell
php artisan make:controller Auth   # Create views.py manually

2. Eloquent ORM vs Django ORM

// Laravel
$users = User::where('active', true)->get();
$user = User::find(1);
User::create(['name' => 'John']);
# Django
users = User.objects.filter(active=True)
user = User.objects.get(id=1)
User.objects.create(name='John')

3. Blade Templates vs Django Templates

{{-- Laravel Blade --}}
@if(Auth::check())
    Welcome, {{ Auth::user()->username }}!
@endif
{# Django Templates #}
{% if user.is_authenticated %}
    Welcome, {{ user.username }}!
{% endif %}

4. Routes with Middleware vs Decorators

// Laravel
Route::middleware('auth')->group(function () {
    Route::get('/dashboard', function () {
        return view('dashboard');
    });
});
# Django
@login_required
def dashboard(request):
    return render(request, 'dashboard.html')

๏ฟฝ Project Structure

โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ Http/
โ”‚   โ”‚   โ”œโ”€โ”€ Controllers/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Controller.php          # Base controller with shared methods
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ AuthController.php      # Authentication controller
โ”‚   โ”‚   โ””โ”€โ”€ Middleware/
โ”‚   โ”‚       โ””โ”€โ”€ Authenticate.php        # Authentication middleware
โ”‚   โ”œโ”€โ”€ Models/
โ”‚   โ”‚   โ””โ”€โ”€ User.php                    # User model (Eloquent)
โ”‚   โ””โ”€โ”€ Providers/
โ”‚       โ””โ”€โ”€ AppServiceProvider.php      # Service provider configuration
โ”œโ”€โ”€ config/                             # Configuration files (like Django settings)
โ”œโ”€โ”€ database/
โ”‚   โ”œโ”€โ”€ migrations/                     # Database migrations
โ”‚   โ””โ”€โ”€ seeders/                        # Database seeders
โ”œโ”€โ”€ resources/views/                    # Blade templates
โ”‚   โ”œโ”€โ”€ layouts/
โ”‚   โ”œโ”€โ”€ auth/
โ”‚   โ”œโ”€โ”€ dashboard.blade.php
โ”‚   โ””โ”€โ”€ welcome.blade.php
โ”œโ”€โ”€ routes/
โ”‚   โ””โ”€โ”€ web.php                         # Web routes (like Django urls.py)
โ””โ”€โ”€ storage/                           # File storage and logs

๐Ÿ”„ Next Steps for Learning

To continue learning, you can implement:

  1. User Registration (equivalent to Django's register view)
  2. Password Reset (equivalent to Django's password reset)
  3. Email Verification (equivalent to Django's email verification)
  4. User Profile Management (equivalent to Django's user profile)
  5. REST API (equivalent to Django REST Framework)
  6. Unit Testing (equivalent to Django's TestCase)

๐Ÿ“– Learning Resources

Note: This project is designed exclusively for educational purposes and comparison between Django and Laravel. It is not optimized for production use.

About

Educational Laravel authentication project - Django to Laravel translation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published