m.rahimi/laravel-tokenize-auth

生成用于认证的令牌。

v1.0.0 2022-05-06 16:49 UTC

This package is auto-updated.

Last update: 2024-09-19 21:34:21 UTC


README

Latest Stable Version Total Downloads License

此包创建一个6位数的令牌,允许您通过短信、电子邮件等方式将其发送给用户,他们可以通过将其发送到特定端点来使用该令牌登录到他们的账户。

令牌会自动过期并且只能使用一次。

⬇ 安装

composer require m.rahimi/laravel-tokenize-auth

然后发布配置文件

php artisan vendor:publish

基本用法

基本上,此包引入了两个端点,您可以向它们发送请求。

  1. 生成并发送令牌给用户
POST '/tokenized-login/request-token?email=foo@example.com'
  1. 接受令牌并在令牌有效的情况下对用户进行认证。
POST '/tokenized-login/login?email=foo@example.com'

注意:如果您想使用其他形式的URL,只需在配置文件中禁用默认路由 'use_default_routes' => false, 并定义自己的路由。

自定义

您可以根据需要自定义并替换默认类,使用您自己的类。

如果您想用您自己的实现替换门面背后的默认实现,您可以在任何服务提供者类的 boot 方法中这样做

    /**
     * The life time of tokens in seconds.
     */
    'token_ttl' => 120,

    /**
     * Determine if you are ok with using the routes
     * defined within the package or you want to define them
     */
    'use_default_routes' => true,

    /**
     * The rules to validate the the receiver address.
     * Whether email or phone number.
     */
    'address_validation_rules' => ['required', 'email'],

    /**
     * Specify the middlewares to be applied on
     * the routes, which the package has provided for you.
     */
    'route_middlewares' => ['api'],

    /**
     * Define a prefix for the urls to avoid conflicts.
     * Note: the prefix should NOT end in a slash / character.
     */
    'route_prefix_url' => '/tokenized-login',

    /**
     * Notification class used to send the token.
     * You may define your own token sender class.
     */
    'token_sender' => \TokenizedLogin\TokenSenders\TokenSender::class,

    /**
     * You can change the way you generate the token by define you own class.
     */
    'token_generator' => \TokenizedLogin\TokenGenerators\TokenGenerator::class,

    /**
     * You can extend Responses class and override
     * it's methods, to define your own responses.
     */
    'responses' => \TokenizedLogin\Http\Responses\Responses::class,

    /**
     * You can change the way you fetch the user from your database
     * by defining a custom user provider class, and set it here.
     */
    'user_provider' => \TokenizedLogin\UserProvider::class,

    /**
     * You may provide a middleware throttler to throttle
     * the requesting and submission of the tokens.
     */
    'throttler_middleware' => 'throttle:3,1',
];

测试

composer test

🙋 贡献

如果您发现一个问题,或者有更好的方法来做某事,请随时提交一个问题或拉取请求。

安全

如果您发现任何与安全相关的问题,请通过电子邮件mohammadreza.rahimi1373@gmail.com联系,而不是使用问题跟踪器。

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件