Download the PHP package doge-dev/laravel-google-authenticator without Composer
On this page you can find all versions of the php package doge-dev/laravel-google-authenticator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-google-authenticator
laravel-google-authenticator
Google Authenticator implementation for Laravel
The package has a trait and a custom validation rule that you can use on any model for verifying the Google Authenticators code.
Table of contents
- Installation
- Example
- Displaying the QR code
- Verifying that the User set up Google Authenticator
- Adding 2FA custom Validation
- Adding 2FA custom Validation using route model binding
- Adding a custom name in Google Authenticator
Installation
Pull the lib with composer:
Add the service provider in
You can add 2 Factor Verification to any model, and it will create:
- A getter (accessor) for enabled_2fa attribute
- function enable2FA() - enables 2 Factor Authentication on the model.
- function getQRCodeURL() - gets the URL for retrieving the QR code image for the User to scan with Google Authenticator
- function getBase64EncodedQRImage() - gets the base64 encoded image (convenient for placing into for emails and such...)
- function verifyCode($code) - attempts to verify the code submitted by the user (will increment the attempts)
- function activate2FA($code) - attempts to verify the code and on successful verification will set the attribute to true
- function getNameForQRCode() - function that is used for generating a name that will be displayed in the Google Authenticator App (you can overwrite this function to use a custom naming convention)
Example
Add the trait to your User model (or any other model on which you might want to enable 2FA):
For MySQL databases you will need to add these attributes to your Model's migration:
Call the function on your model, and you're done. This will generate a secret for the given model.
You can add some helper attributes to your model, for the sake of convenience. We've also added and to the $visible attribute, tho this is not necessary either.
That's it! :) Now you can display the QR code to your user so that he can start using the Google Authenticator with your app.
Displaying the QR code
Now you can show the QR code to the user either with:
or
Verifying that the User set up Google Authenticator
You can check if the User successfully activated his 2FA:
When a User scans the QR Code, prompt him for the code and validate the User's 2FA:
For simple verification you can use:
Adding 2FA custom Validation
You can easily verify the code on any custom request by adding the validation:
The validator will try to validate the code using the logged in user (Auth::user()).
Adding 2FA custom Validation using route model binding
Or you can leverage route model binding and create a validation in your custom Request model:
In this case the validator expects to find an object in the route. The object needs to implement the above mentioned Trait in order for the validation to work.
Adding a custom name in Google Authenticator
You can set the text that is displayed in Google Authenticator by overriding the traits default getNameForQRCode() function in your Model implementation: