This repository was archived by the owner on Aug 22, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMagicLink.php
More file actions
88 lines (70 loc) · 1.82 KB
/
MagicLink.php
File metadata and controls
88 lines (70 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace Codedge\MagicLink;
use Carbon\Carbon;
use Illuminate\Support\Facades\URL;
use Statamic\Contracts\Auth\User;
final class MagicLink
{
/**
* Time, the link expires.
*/
protected Carbon $expireTime;
/**
* Redirect to the page after a successful login. This is either the CP dashboard or a page with protected
* content.
*/
protected string $redirectTo;
protected string $link;
protected string $hash;
protected User $user;
public function __construct(User $user)
{
$this->expireTime = now()->addMinutes(config('statamic-magiclink.expire_time'));
$this->redirectTo = config('statamic-magiclink.url.redirect_on_success');
$this->user = $user;
}
public function setExpireTime(int $expireTime): self
{
$this->expireTime = now()->addMinutes($expireTime);
return $this;
}
public function getExpireTime(): Carbon
{
return $this->expireTime;
}
public function setRedirectTo(string $redirect): self
{
$this->redirectTo = $redirect;
return $this;
}
public function getRedirectTo(): string
{
return $this->redirectTo;
}
public function getHash(): string
{
return $this->hash;
}
public function getLink(): string
{
return $this->link;
}
public function generate(): self
{
$this->link = URL::temporarySignedRoute(
'magiclink.login',
$this->expireTime,
[
'hash' => $this->generateHash(),
'user_email' => $this->user->email(),
]
);
return $this;
}
private function generateHash(): string
{
$this->hash = sha1($this->user->email());
return $this->hash;
}
}