定制 norbybaru/passwordless-auth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

norbybaru/passwordless-auth

Composer 安装命令:

composer require norbybaru/passwordless-auth

包简介

Laravel Passwordless login - Magic link

README 文档

README

Run Unit Tests PHPStan Laravel Pint

PASSWORDLESS-AUTH

LARAVEL PASSWORDLESS AUTHENTICATION

Laravel Passwordless Authentication using Magic Link.

This package enables authentication through email links, eliminating the requirement for users to input passwords for authentication. Instead, it leverages the user's email address to send a login link to their inbox. Users can securely authenticate by clicking on this link. It's important to note that the package does not include a user interface for the authentication page; it assumes that the application's login page will be custom-built. Make sure to scaffold your login UI page accordingly to integrate seamlessly with this package.

PS. Email provider must be setup correctly and working to email magic link to authenticate user

Installation

composer require norbybaru/passwordless-auth

Publishing the config file

php artisan vendor:publish --provider="NorbyBaru\Passwordless\PasswordlessServiceProvider" --tag="passwordless-config"

Preparing the database

Publish the migration to create required table:

php artisan vendor:publish --provider="NorbyBaru\Passwordless\PasswordlessServiceProvider" --tag="passwordless-migrations"

Run migrations.

php artisan migrate

Basic Usage

Preparing Model

Open the User::class Model and ensure to implements NorbyBaru\Passwordless\CanUsePasswordlessAuthenticatable::class and to add trait NorbyBaru\Passwordless\Traits\PasswordlessAuthenticatable::class to the class

<?php

namespace App\Models;

...
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use NorbyBaru\Passwordless\CanUsePasswordlessAuthenticatable;
use NorbyBaru\Passwordless\Traits\PasswordlessAuthenticatable;

class User extends Authenticatable implements CanUsePasswordlessAuthenticatable
{
    ...
    use Notifiable;
    use PasswordlessAuthenticatable;
    ...
}

Preparing config/passwordless.php

Open config file config/passwordless.php

  • Update default_redirect_route to the correct route name the user should land by default once authenticated in case you have different route name than home. eg.
'default_redirect_route' => 'dashboard',
  • Update login_route to the correct route name of your login page to allow redirecting user back to that page on invalid magic link. eg.
'login_route' => 'auth.login',

Setup Login Routes

Update application Login routes to sen Magic Link to user

<?php

use Illuminate\Support\Facades\Route;

Route::post('login', function (Request $request) {
    $validated = $request->validate([
        'email' => 'required|email|exists:users',
    ]);

    $status = Passwordless::magicLink()->sendLink($validated);

    return redirect()->back()->with([
        'status' => trans($message)
    ]);
});

Setup Mail Provider

Make sure to have your application mail provider setup and working 100% for your Laravel application

MAIL_MAILER=
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="${APP_NAME}"

Setup Translations

Add file passwordless.php in your translations directory and copy the entry below. Feel free to update text to suit your application needs.

return [
    'sent' => 'Login link sent to inbox.',
    'throttled' => 'Login link was already sent. Please check your inbox or try again later.',
    'invalid_token' => 'Invalid link supplied. Please request new one.',
    'invalid_user' => 'Invalid user info supplied.',
    'verified' => 'Login successful.',
];

Advance Usage

Override MagicLinkNotification

To override default notification template, override method sendAuthenticationMagicLink in your User model which implements interface CanUsePasswordlessAuthenticatable

public function sendAuthenticationMagicLink(string $token): void
{
    // Replace with your notification class.

    // eg. $this->notify(new SendMagicLinkNotification($token));
}

Run Unit Test

composer test

Run Code Formatter

composer fmt

norbybaru/passwordless-auth 适用场景与选型建议

norbybaru/passwordless-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 742 次下载、GitHub Stars 达 21, 最近一次更新时间为 2022 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「Laravel Auth」 「Passwordless」 「magic link」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 norbybaru/passwordless-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 norbybaru/passwordless-auth 我们能提供哪些服务?
定制开发 / 二次开发

基于 norbybaru/passwordless-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 742
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 21
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 21
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-05