-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagicLink.php
executable file
·84 lines (71 loc) · 1.79 KB
/
MagicLink.php
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
<?php
namespace Modulus\Framework;
use Modulus\Http\Session;
use Modulus\Security\Hash;
use Modulus\Hibernate\Model;
use Modulus\Utility\Notification;
use Illuminate\Database\Eloquent\Model as Eloquent;
class MagicLink extends Eloquent
{
use Model;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email', 'token',
];
/**
* $table
*
* @var string
*/
protected $table = 'magic_links';
/**
* Notify user
*
* @param \Modulus\Http\Request $request
* @return void
*/
public static function notify($request, $provider, $musked)
{
$model = config("auth.provider.{$provider}.model");
$token = Hash::random(35);
if ($model::where($musked, $request->input('email'))->first() !== null) {
MagicLink::create([
'email' => $request->input('email'),
'token' => $token,
]);
}
Session::key('_magic', $token);
return ['email' => $request->input('email'), 'token' => $token];
}
/**
* Check when token was generated
*
* @param string $token
* @return boolean
*/
public static function verify($token, $provider, $musked)
{
$userToken = MagicLink::where('token', $token)->first();
if ($userToken == null) return false;
$userEmail = $userToken->email;
if ($userToken->created_at->diffInMinutes() <= config('auth.expire.magic_token')) {
if (Session::has('_magic')) {
if (Session::key('_magic') == $userToken->token) {
$userToken->delete();
Session::delete('_magic');
return config("auth.provider.{$provider}.model")::where($musked, $userEmail)->first();
}
else {
Session::delete('_magic');
return false;
}
}
}
$userToken->delete();
return false;
}
}