Code Monkey home page Code Monkey logo

laravel-verification-code's Introduction

Laravel verification code

Latest Version on Packagist Total Downloads

This package makes it possible to authenticate a user via a verification code.

Installation

You can install this package using composer:

composer require nextapps/laravel-verification-code

The package will automatically register itself.

You can publish the migration with:

php artisan vendor:publish --provider="NextApps\VerificationCode\VerificationCodeServiceProvider" --tag="migrations"

After publishing the migration, run the migration with:

php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="NextApps\VerificationCode\VerificationCodeServiceProvider" --tag="config"

Usage

Generate and send a verification code

use NextApps\VerificationCode\VerificationCode;

VerificationCode::send($email);

This will generate a verification code for the user. The code will be stored in the verification_codes table. An email with the generated code will then be sent to the user matching the given email address.

Verify a verification code

use NextApps\VerificationCode\VerificationCode;

VerificationCode::verify($code, $email);

If the verification code is expired or does not match the user's email address, it will return false. If valid, it will return true and delete the code.

If you do not want the code to be deleted (in case the same code needs to be verified at different points in the login flow) you can pass a third parameter.

use NextApps\VerificationCode\VerificationCode;

VerificationCode::verify($code, $email, $deleteAfterVerification);

Verification codes table cleanup

Since it is possible for the verification codes table to fill up with unused codes, the following command will prune all codes older than the given hours.

php artisan verification-code:prune --hours=24

Config settings

Length

This value defines the length of every generated verification code.

Characters

You can define which characters are used to generate a verification code. By default, certain characters are excluded (0, O, I, L) because they look too similar.

Expire seconds

A verification code is only valid for a certain amount of time. You can define after how many seconds a verification code will expire.

Max codes per verifiable

By default, only one verification code can be active per verifiable. If you want to allow multiple active codes per verifiable, then you can change this setting to a different number (or to null if you want unlimited codes per verifiable).

Custom Notification

If you want to use a custom notification to send the verification code, you can create your own notification class which should extend the VerificationCodeCreatedInterface. Make sure you don't forget to pass the verification code to the mail.

Custom Model

If you want to use a custom verification code model, you can create your own verification code class which should extend the VerificationCode.

Queue

If your notification is queueable, you can define the queue that will be used for the notification.

Test verifiables and test code

You sometimes may want to allow a user to log in immediately without letting them go through the verification code flow. To do this you can add the verifiable (e.g. email address) to the test_verifiables array. You then need to define a test_code. The combination of the verifiable and the test code will make it possible for the user to pass through.

Testing

You can run tests with:

composer test

Linting

composer lint

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-verification-code's People

Contributors

bart-kuenen-nextapps avatar codebeauty avatar earnould avatar gdebrauwer avatar jan-claeys avatar yinx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-verification-code's Issues

feature request: expires in amount of minutes

It would be nice if there would be an option to define expire time in minutes, not hours

This will make plugin more secure - if the user has only 15-30 minutes to apply the code rather than one hour.

I tired to apply

'expire_hours' => 0.5,

but it doesn't work as expected

Multibyte characters in verification code

Hi,
thanks a lot for a great package, it saved me a lot of time.

One minor suggestion: it does not work with Ukrainian or Russian strings, like 'characters' => 'цукенгзхфывапролджэячсмитбю'. So I changed in CodeGenerator

return $characters[rand(0, strlen($characters) - 1)];
to
return $characters[rand(0, count($characters) - 1)];

and in getCharacters
return $characters
to
return mb_str_split($characters);

PHP 8.0 support

Hi there, the library is a nice one but currently, it's kind of outdated since it does not support php8.0 I will send a PR for that

Customise email template

I'd like to be able to pass my own view when sending a code. For example:

VerificationCode::send($user->email)->view('emails.verify-email', [
    'email' => $user->email,
]);

VerificationCode::send($user->email)->view('emails.reset-password', [
    'username' => $user->username,
]);

I am using verification codes for email verification and password resetting, so I need a separate view for each. Also, in my view file, i need the user's email address / username (as above), but this isn't available when implementing the VerificationCodeCreatedInterface on a custom Notification.

Is there any easy way to achieve this currently? Thanks :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.