Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create link for authenticate in Laravel without password or get private content

License

NotificationsYou must be signed in to change notification settings

uwpsych/laravel-magiclink

 
 

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.

Latest Version on Packagisttestsstyle-fixQuality ScoreTotal Downloads

Contents

Installation

You can install this package via composer using:

composer require cesargb/laravel-magiclink

Preparing the database

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

Use case

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

Create a MagicLink

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.

Actions

Each MagicLink is associated with an action, which is what will be performedonce the link is visited.

Login Action

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;

Download file Action

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;

View Action

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;

Http Response Action

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;

Controller Action

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;

Custom Action

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;

Custom Base 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/...

Protect with an access code

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;

Custom view for access code

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

Lifetime

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;

Events

MagicLink fires two events:

  • MagicLink\Events\MagicLinkWasCreated
  • MagicLink\Events\MagicLinkWasVisited

Customization

Config

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

Migrations

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/*

Custom response when magiclink is invalid

When the magicLink is invalid by default the http request return a status 403.You can custom this response with configmagiclink.invalid_response.

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,        ],    ],

Abort

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,        ],    ],

Redirect

Define classMagicLink\Responses\RedirectResponse::class toreturn aredirect()

'invalid_response' => ['class'   =>MagicLink\Responses\RedirectResponse::class,'options' => ['to' =>'/not_valid_path','status' =>301,        ],    ],

View

Define classMagicLink\Responses\ViewResponse::class toreturn aview()

'invalid_response' => ['class'   =>MagicLink\Responses\ViewResponse::class,'options' => ['view' =>'invalid','data' => [],        ],    ],

Testing

Run the tests with:

composertest

Contributing

Please seeCONTRIBUTING for details.

Security

If you discover any security-related issues, please emailcesargb@gmail.cominstead of using the issue tracker.

License

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP94.3%
  • Blade5.7%

[8]ページ先頭

©2009-2025 Movatter.jp