Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linking a magic link with a user #115

Open
uoc-kpf opened this issue Jul 18, 2024 · 2 comments
Open

linking a magic link with a user #115

uoc-kpf opened this issue Jul 18, 2024 · 2 comments

Comments

@uoc-kpf
Copy link

uoc-kpf commented Jul 18, 2024

It would be really useful when managing links to be able to associate them with the users that they are generated for.
I looked at the MagicLinkWasVisited event but it is fired before the user is logged in. I also looked at the action classes but they don't accept arbitrary extra parameters to save with the magic link data.

Any ideas on a good solution for this?

@boudydegeer
Copy link

Hi, I just came across this repo, so not used it yet.
But it seems like you can do it your self already.
According to the documentation here for example you could do something like this:

<?php
use MagicLink\Actions\LoginAction;
use MagicLink\MagicLink;

// Sample 1; Login and redirect to dashboard
$user = User::first();
$action = (new LoginAction($user))->response(redirect('/dashboard'));
$magicLink = MagicLink::createFor($action);
$urlToDashBoard = $magicLink->url;

UserLinks::create([
  'user_id' => $user->id,
  'magicklink_id' => $$magicLink->id,
  'visited_at' => null
]);

And then just create a Listener to listen to the MagicLinkWasVisited event to mark that link as visited_at!

<?php
namespace App\Listeners;

use MagicLink\Events\MagicLinkWasVisited;

class WhenMagicLinkWasVisited
{
  /**
  * Handle the event.
  */
  public function handle(MagicLinkWasVisited $event): void
  {
    UserMagicLinks::first(["magiclink_id" => $event->magicLink->id])->update(["visited_at" => now()]);
  }
}

Obviously it would be awesome to have that functionality build in, maybe I will do a PR with that approach.

@cesargb Do you think its a feature you would like to include?

I hope that helped @uoc-kpf !

@uoc-kpf
Copy link
Author

uoc-kpf commented Jul 29, 2024

@boudydegeer Many thanks for your suggestions.

I came up with a similar workaround. It would be good to have this included as a feature (along with defaults for max number of visits and link lifetime in the config file).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants