- Notifications
You must be signed in to change notification settings - Fork0
Create link for authenticate in Laravel without password or get private content
License
uwpsych/laravel-magiclink
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Through theMagicLink class we can create a secure link that laterbeing visited will perform certain actions, which will allow usoffer secure content and even log in to the application.
You can install this package via composer using:
composer require cesargb/laravel-magiclink
You need to publish the migration to create themagic_links table:
php artisan vendor:publish --provider="MagicLink\MagicLinkServiceProvider" --tag="migrations"
After that, you need to run migrations.
php artisan migrate
With this example you can create a link to auto login on your application withthe desired user:
useMagicLink\Actions\LoginAction;useMagicLink\MagicLink;$urlToAutoLogin = MagicLink::create(newLoginAction($user))->url
TheMagicLink class has thecreate method to generate a class that throughtheurl property we will obtain the link that we will send to our visitor.
This method requires the action to be performed.
Each MagicLink is associated with an action, which is what will be performedonce the link is visited.
- Login Action
- Download file Action
- View Action
- Http Response Action
- Http Response
- Controller
- Custom Action
- Custom Base URL
Through theLoginAction action, you can log in to the application using thegenerated link byMagicLink.
Your constructor supports the user who will login. Optionally we can specifytheHTTP response using the methodresponse or specify other guard with methodguard.
Examples:
useMagicLink\Actions\LoginAction;useMagicLink\MagicLink;// Sample 1; Login and redirect to dash board$action =newLoginAction(User::first());$action->response(redirect('/dashboard'));$urlToDashBoard = MagicLink::create($action)->url;// Sample 2; Login and view forms to password reset and use guard web$action =newLoginAction(User::first());$action->response(view('password.reset', ['email' =>'user@example.tld']));$urlShowView = MagicLink::create($action)->url;// Sample 3; Login in other guard and redirect default$action =newLoginAction(User::first());$action->guard('customguard');$urlShowView = MagicLink::create($action)->url;// Sample 4; Login and remember me$action =newLoginAction(User::first());$action->remember();$urlShowView = MagicLink::create($action)->url;
This action,DownloadFileAction, permit create a link to download a private file.
The constructor require the file path.
Example:
useMagicLink\Actions\DownloadFileAction;useMagicLink\MagicLink;// Url to download the file storage_app('private_document.pdf')$url = MagicLink::create(newDownloadFileAction('private_document.pdf'))->url;// Download file with other file_name$action =newDownloadFileAction('private_document.pdf','your_document.pdf');$url = MagicLink::create($action)->url;// Download file from other disk$action =newDownloadFileAction('private_document.pdf')->disk('ftp');$url = MagicLink::create($action)->url;
With the actionViewAction, you can provide access to the view. You can usein his constructor the same arguments than methodview of Laravel.
Example:
useMagicLink\Actions\ViewAction;useMagicLink\MagicLink;// Url to view a internal.blade.php$url = MagicLink::create(newViewAction('internal', ['data' =>'Your private custom content',]))->url;
Through theResponseAction action we can access private content without needlogin. Its constructor accepts as argument theHTTP responsewhich will be the response of the request.
Examples:
useMagicLink\Actions\ResponseAction;useMagicLink\MagicLink;$action =newResponseAction(function () { Auth::login(User::first());returnredirect('/change_password');});$urlToCustomFunction = MagicLink::create($action)->url;
MagicLink can directly call a controller via theControllerAction action.
The constructor requires one argument, the name of the controller class. Withthe second argument can call any controller method, by default it will use the__invoke method.
useMagicLink\Actions\ControllerAction;useMagicLink\MagicLink;// Call the method __invoke of the controller$url = MagicLink::create(newControllerAction(MyController::class))->url;// Call the method show of the controller$url = MagicLink::create(newControllerAction(MyController::class,'show'))->url;
You can create your own action class, for them you just need to extend withMagicLink\Actions\ActionAbstract
useMagicLink\Actions\ActionAbstract;class MyCustomActionextends ActionAbstract{publicfunction__construct(publicint$variable) { }publicfunctionrun() {// Do somethingreturnresponse()->json(['success' =>true,'data' =>$this->variable, ]); }}
You can now generate a Magiclink with the custom action
useMagicLink\MagicLink;$action =newMyCustomAction('Hello world');$urlToCustomAction = MagicLink::create($action)->url;
To set the base URL for a magic link, you can use thebaseUrl method. This method ensures that the provided base URL has a trailing slash, making it ready for URL generation.
$magiclink = MagicLink::create($action);$magiclink->baseUrl("http://example.com");$urlShowView =$magiclink->url;// http://example.com/magiclink/...
Optionally you can protect the resources with an access code.You can set the access code with methodprotectWithAccessCodewhich accepts an argument with the access code.
$magiclink = MagicLink::create(newDownloadFileAction('private_document.pdf'));$magiclink->protectWithAccessCode('secret');$urlToSend =$magiclink->url;
You can customize the view of the access code form with the config filemagiclink.php:
'access_code' => ['view' =>'magiclink::access-code',// Change with your view],
This is thedefault view
By default a link will be available for 72 hours after your creation. We canmodify the life time in minutes of the link by the$lifetime optionavailable in thecreate method. This argument accepts the valuenull sothat it does not expire in time.
$lifetime =60;// 60 minutes$magiclink = MagicLink::create(newResponseAction(),$lifetime);$urlToSend =$magiclink->url;
We also have another option$numMaxVisits, with which we can define thenumber of times the link can be visited,null by default indicates that thereare no visit limits.
$lifetime =null;// not expired in the time$numMaxVisits =1;// Only can visit one time$magiclink = MagicLink::create(newResponseAction(),$lifetime,$numMaxVisits);$urlToSend =$magiclink->url;
MagicLink fires two events:
MagicLink\Events\MagicLinkWasCreatedMagicLink\Events\MagicLinkWasVisited
To custom this package you can publish the config file:
php artisan vendor:publish --provider="MagicLink\MagicLinkServiceProvider" --tag="config"
And edit the fileconfig/magiclink.php
To customize the migration files of this package you need to publish the migration files:
php artisan vendor:publish --provider="MagicLink\MagicLinkServiceProvider" --tag="migrations"
You'll find the published files indatabase/migrations/*
When the magicLink is invalid by default the http request return a status 403.You can custom this response with configmagiclink.invalid_response.
To return a response, use classMagicLink\Responses\Response::classsameresponse(), you can send the arguments with options
Example:
'invalid_response' => ['class' =>MagicLink\Responses\Response::class,'options' => ['content' =>'forbidden','status' =>403, ], ],
To return a exception and let the framework handle the response,use classMagicLink\Responses\AbortResponse::class.Sameabort(), you can send the arguments with options.
Example:
'invalid_response' => ['class' =>MagicLink\Responses\AbortResponse::class,'options' => ['message' =>'You Shall Not Pass!','status' =>403, ], ],
Define classMagicLink\Responses\RedirectResponse::class toreturn aredirect()
'invalid_response' => ['class' =>MagicLink\Responses\RedirectResponse::class,'options' => ['to' =>'/not_valid_path','status' =>301, ], ],
Define classMagicLink\Responses\ViewResponse::class toreturn aview()
'invalid_response' => ['class' =>MagicLink\Responses\ViewResponse::class,'options' => ['view' =>'invalid','data' => [], ], ],
Run the tests with:
composertestPlease seeCONTRIBUTING for details.
If you discover any security-related issues, please emailcesargb@gmail.cominstead of using the issue tracker.
The MIT License (MIT). Please seeLicense File for more information.
About
Create link for authenticate in Laravel without password or get private content
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- PHP94.3%
- Blade5.7%