DevDojo Auth Documentation

repository·main·Indexed 20 days ago

https://github.com/thedevdojo/auth

A plug-and-play authentication package for Laravel applications. It provides ready-to-use routes for login, registration, password management, and two-factor authentication (2FA). Key features include social authentication support via the HasSocialProviders trait, integration with Laravel Fortify, customizable password strength rules, and email verification with 6-digit codes. The package includes an Auth facade for service access and various middleware groups for managing 2FA states.

Tokens
3.8K
Snippets
16
Records
20
Agent score
69%

What's inside DevDojo Auth

  1. Add social authentication support using HasSocialProviders

    main

    To enable social authentication helpers in your application, include the HasSocialProviders trait in your App\Models\User model.

    <?php
    
    namespace App\Models;
    
    use Devdojo\Auth\Traits\HasSocialProviders; // Import the trait
    
    class User extends Devdojo\Auth\Models\User
    {
        use HasSocialProviders; // Use the trait in the User model
    
        // Existing User model code...
    }
  2. Install DevDojo Auth in a Laravel application

    main

    To install DevDojo Auth, require the package via Composer, publish the necessary assets and configurations, run migrations, and extend the base User model.

    1. Install the package

    composer require devdojo/auth

    2. Publish assets and configurations

    Run the following commands to publish authentication assets, configs, CI files, and migrations:

    php artisan vendor:publish --tag=auth:assets
    php artisan vendor:publish --tag=auth:config
    php artisan vendor:publish --tag=auth:ci
    php artisan vendor:publish --tag=auth:migrations

    3. Run migrations

    php artisan migrate

    4. Extend the User Model

    Update your App\Models\User model to extend the DevDojo Auth User model:

    use Devdojo\
    Auth\\Models\\User as AuthUser;
    
    class User extends AuthUser
    {
        // ...
    }
    composer require devdojo/auth
    
    php artisan vendor:publish --tag=auth:assets
    php artisan vendor:publish --tag=auth:config
    php artisan vendor:publish --tag=auth:ci
    php artisan vendor:publish --tag=auth:migrations
    
    php artisan migrate
  3. Integrate DevDojo Auth with Laravel Fortify

    main

    If you are using Laravel Fortify, DevDojo Auth can automatically enable Two-Factor Authentication (2FA) if the following conditions are met:

    1. The Laravel ortify eatures class exists.
    2. The configuration devdojo.auth.settings.enable_2fa is set to true.

    When enabled, it injects twoFactorAuthentication into Fortify's features with confirm and confirmPassword set to true.

  4. Publish DevDojo Auth assets and configuration

    main

    You can publish various package components to your application using Artisan commands. This allows you to customize the configuration, migrations, views, and assets provided by the package.

    Use the following tags with the vendor:publish command:

    • auth:config: Publish the package configuration files.
    • auth:assets: Publish public assets.
    • auth:migrations: Publish database migrations.
    • auth:components: Publish the auth element components to resources/views/components/auth/elements.
    • auth:ci: Publish CI workflow files to .github/workflows.
    php artisan vendor:publish --tag=auth:config
    php artisan vendor:publish --tag=auth:assets
    php artisan vendor:publish --tag=auth:migrations
    php artisan vendor:publish --tag=auth:components
    php artisan vendor:publish --tag=auth:ci
  5. Configure Email Verification requirements

    main

    The User model implements MustVerifyEmail. The behavior of hasVerifiedEmail() is controlled by a configuration setting. If devdojo.auth.settings.registration_require_email_verification is set to false, all users are considered verified regardless of their email_verified_at timestamp.

    When verification is required, sendEmailVerificationNotification() sends a notification containing a 6-digit code and a signed link via the VerifyEmailWithCode notification.

    // The logic depends on this config key:
    config('devdojo.auth.settings.registration_require_email_verification')
  6. Configure the redirect path after logout

    main

    You can customize where a user is redirected after logging out by setting the redirect_after_logout key in your devdojo.auth.settings configuration array. If this key is not defined, the application defaults to redirecting to the root path (/).

    // In your config/devdojo.php or equivalent configuration file
    'auth' => [
        'settings' => [
            'redirect_after_logout' => '/your-custom-path',
        ],
    ],
  7. Customize the email verification code expiration time

    main

    The VerifyEmailWithCode notification uses a configuration value to determine how long the verification code remains valid. You can customize this duration by setting the verification_code_expires_in key within the devdojo.auth.settings configuration array. The default value is 15 minutes.

    // In your config/devdojo.php or equivalent
    'settings' => [
        'verification_code_expires_in' => 30, // Set expiration to 30 minutes
    ],
  8. Configure DevDojo Auth settings

    main

    The package merges several configuration files into your application. You can customize the behavior of the auth system by modifying these keys in your config/ directory:

    • devdojo.auth.settings: General authentication settings (e.g., enable_2fa).
    • devdojo.auth.appearance: Settings related to the visual appearance of auth components.
    • devdojo.auth.language: Localization and language settings.
    • devdojo.auth.providers: Configuration for social authentication providers.
    • devdojo.auth.descriptions: Settings for text descriptions.
  9. Configure the redirect path after email verification

    main

    The VerifyEmailController uses a configuration value to determine where to redirect the user after they have successfully verified their email address. You can customize this destination by setting the redirect_after_auth key within the devdojo.auth.settings configuration group.

    // Example configuration structure
    'devdojo' => [
        'auth' => [
            'settings' => [
                'redirect_after_auth' => '/dashboard', // The path users are sent to after verification
            ],
        ],
    ];
  10. Configure password strength requirements

    main

    The PasswordStrength rule is driven by the devdojo.auth.settings configuration keys. You can customize the password validation requirements by updating your application's configuration file.

    Available configuration keys:

    • password_min_length: Minimum number of characters (defaults to 8).
    • password_require_uppercase: If true, requires mixed case characters.
    • password_require_numeric: If true, requires numbers.
    • password_require_special_character: If true, requires symbols.
    • password_require_uncompromised: If true, checks if the password has appeared in data leaks.
    // Example configuration structure
    'devdojo' => [
        'auth' => [
            'settings' => [
                'password_min_length' => 12,
                'password_require_uppercase' => true,
                'password_require_numeric' => true,
                'password_require_special_character' => true,
                'password_require_uncompromised' => true,
            ],
        ],
    ],
  11. Configure social authentication providers

    main

    Social authentication providers are defined in the devdojo.auth.providers configuration key. The SocialProvider model automatically hydrates its data from this configuration array. Each provider in the array should include its credentials and will be assigned a slug based on its array key.

    To add a new social provider, add an entry to your config/devdojo.auth.php file (or the relevant configuration file used by the package) under the providers key.

    // Example configuration structure for devdojo.auth.providers
    'providers' => [
        'google' => [
            'client_id' => 'YOUR_GOOGLE_CLIENT_ID',
            'client_secret' => 'YOUR_GOOGLE_CLIENT_SECRET',
        ],
        'github' => [
            'client_id' => 'YOUR_GITHUB_CLIENT_ID',
            'client_secret' => 'YOUR_GITHUB_CLIENT_SECRET',
        ],
    ],
  12. Available Authentication Routes

    main

    Once installed, DevDojo Auth provides the following default routes:

    • Login: /auth/login
    • Register: /auth/register
    • Forgot Password: /auth/register
    • Password Reset: /auth/password/reset
    • Password Reset Token: /auth/password/{token}
    • Password Confirmation: /auth/password/confirm
    • Two-Factor Challenge: /auth/two-factor-challenge
    • Two-Factor Setup: /user/two-factor-authentication
    • Logout: /auth/logout