Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Email With Login Link And Change Password Laravel Package

daddl3/email_with_login_link_and_change_password

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require daddl3/email-with-login-link-and-change-password
    

    Add the bundle to your config/bundles.php (Symfony) or register the service provider in config/app.php (Laravel via Symfony bridge):

    Daddl3\EmailWithLoginLinkAndChangePasswordBundle\Daddl3EmailWithLoginLinkAndChangePasswordBundle::class => ['all' => true],
    
  2. Configuration Publish the default config:

    php artisan vendor:publish --provider="Daddl3\EmailWithLoginLinkAndChangePasswordBundle\Daddl3EmailWithLoginLinkAndChangePasswordBundle" --tag="config"
    

    Update config/daddl3_email_with_login_link_and_change_password.php with your email settings (e.g., mailer transport, from address, expiration_time for login links).

  3. First Use Case Trigger a login link email for a user:

    use Daddl3\EmailWithLoginLinkAndChangePasswordBundle\Service\EmailService;
    
    $emailService = app(EmailService::class);
    $user = User::find(1); // Replace with your user model
    $emailService->sendLoginLinkEmail($user->getEmail());
    

Implementation Patterns

Core Workflows

  1. Sending Login Links

    • Use EmailService::sendLoginLinkEmail($email) to generate and send a one-time login link.
    • Customize the email template by overriding the default Twig template (resources/views/emails/login_link.html.twig in Symfony; adapt for Laravel via resources/views/vendor/email-with-login-link-and-change-password/login_link.blade.php).
  2. Password Reset

    • Send a password reset link:
      $emailService->sendPasswordResetEmail($email);
      
    • Handle the reset request in your controller:
      $emailService->validatePasswordResetToken($token, $email);
      $emailService->resetPassword($email, $newPassword);
      
  3. Token Management

    • Manually generate tokens (e.g., for testing):
      $token = $emailService->generateLoginLinkToken($email);
      
    • Validate tokens before redirecting users:
      if ($emailService->isLoginLinkValid($token, $email)) {
          // Redirect to secure area
      }
      

Integration Tips

  • Laravel-Specific Adaptations

    • Replace Symfony’s Twig with Laravel’s Blade by creating a custom service provider to bind the EmailService and override template paths.
    • Use Laravel’s Mail facade for transport if not using Symfony Mailer:
      $mailer = app(\Symfony\Component\Mailer\MailerInterface::class);
      // Configure Symfony Mailer to use Laravel’s SwiftMailer transport.
      
  • Customizing Emails

    • Extend the base email class (Daddl3\EmailWithLoginLinkAndChangePasswordBundle\Email\BaseEmail) to add dynamic content:
      namespace App\Email;
      
      use Daddl3\EmailWithLoginLinkAndChangePasswordBundle\Email\BaseEmail;
      
      class CustomLoginEmail extends BaseEmail {
          public function build(): self {
              $this->subject('Custom Login Link');
              $this->html('<p>Hello, use this link to login: <a href="{{ link }}">{{ link }}</a></p>');
              return $this;
          }
      }
      
    • Bind your custom email class in the service configuration.
  • Token Storage

    • By default, tokens are stored in the database (via Token entity). For custom storage (e.g., Redis), implement TokenStorageInterface and update the email_service configuration:
      token_storage: App\Services\RedisTokenStorage
      
  • Security

    • Rate-limit token generation/usage to prevent abuse. Use Laravel’s throttle middleware or Symfony’s RateLimiter.
    • Log token generation/usage for auditing:
      event(new LoginLinkGenerated($user, $token));
      

Gotchas and Tips

Pitfalls

  1. Token Expiration

    • Tokens expire after expiration_time (default: 1 hour). Ensure this aligns with your security policies. Adjust via config:
      'expiration_time' => 3600, // 1 hour in seconds
      
  2. Email Delivery Failures

    • If emails aren’t sending, verify:
      • The mailer transport is configured (e.g., smtp, sendmail).
      • The from address is valid and not blacklisted.
      • Laravel/Symfony’s mail queue is processing (run php artisan queue:work if using queues).
  3. Token Reuse

    • Tokens are single-use by default. If a user clicks a link multiple times, implement logic to invalidate the token after first use:
      $emailService->consumeLoginLinkToken($token, $email);
      
  4. User Model Requirements

    • The User model must implement getEmail() and setPassword($password). For Laravel, ensure your User model uses HasApiTokens or similar for token storage.

Debugging

  • Token Validation Errors

    • Check if the token exists in the database and hasn’t expired. Enable debug logging:
      'debug' => env('APP_DEBUG', false),
      
    • Log token generation/validation in your controller:
      \Log::debug('Token validation', ['token' => $token, 'email' => $email]);
      
  • Email Template Issues

    • Clear cached views if templates aren’t updating:
      php artisan view:clear
      
    • For Symfony, dump the Twig environment to debug:
      $this->get('twig')->addExtension(new \Twig\Extra\Debug\DebugRuntime());
      

Extension Points

  1. Custom Token Storage

    • Implement TokenStorageInterface for non-database storage (e.g., Redis, Memcached):
      namespace App\Services;
      
      use Daddl3\EmailWithLoginLinkAndChangePasswordBundle\Token\TokenStorageInterface;
      
      class RedisTokenStorage implements TokenStorageInterface {
          public function save($token, $email, $expiry): void {
              // Redis logic
          }
      
          public function find($token): ?string {
              // Redis logic
          }
      
          public function delete($token): void {
              // Redis logic
          }
      }
      
  2. Event Listeners

    • Listen for token events to trigger actions (e.g., analytics, notifications):
      // In EventServiceProvider (Laravel) or symfony/event-dispatcher
      protected $listen = [
          \Daddl3\EmailWithLoginLinkAndChangePasswordBundle\Event\LoginLinkGenerated::class => [
              \App\Listeners\LogLoginLinkGeneration::class,
          ],
      ];
      
  3. API Integration

    • Expose endpoints for frontend frameworks (e.g., React, Vue) to request login links:
      // routes/api.php
      Route::post('/login-link', function (Request $request) {
          $emailService = app(EmailService::class);
          return $emailService->sendLoginLinkEmail($request->email);
      });
      
  4. Multi-Tenant Support

    • Extend the Token entity to include a tenant_id and filter queries by tenant:
      $tokens = Token::where('email', $email)
          ->where('tenant_id', auth()->tenant()->id)
          ->first();
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels
wazza/laravel-db-encryption
hokny/laravel-generator
vonsogt/backup
jengo/queues
jeremykenedy/laravel-observability
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums