|
1
|
|
|
<?php namespace JobApis\JobsToMail\Jobs\Auth; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Http\Request; |
|
4
|
|
|
use JobApis\JobsToMail\Http\Messages\FlashMessage; |
|
5
|
|
|
use JobApis\JobsToMail\Models\Token; |
|
6
|
|
|
use JobApis\JobsToMail\Models\User; |
|
7
|
|
|
use JobApis\JobsToMail\Repositories\Contracts\UserRepositoryInterface; |
|
8
|
|
|
|
|
9
|
|
|
class LoginUserWithToken |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Number of days until tokens "expire" |
|
13
|
|
|
*/ |
|
14
|
|
|
const DAYS_TO_EXPIRE = 7; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string $token |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $token; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a new job instance. |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function __construct($token = null) |
|
25
|
|
|
{ |
|
26
|
3 |
|
$this->token = $token; |
|
27
|
3 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Login to/Confirm a user's account using a token |
|
31
|
|
|
* |
|
32
|
|
|
* @param UserRepositoryInterface $users |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* |
|
35
|
|
|
* @return FlashMessage |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public function handle(UserRepositoryInterface $users, Request $request) |
|
38
|
|
|
{ |
|
39
|
3 |
|
$token = $users->getToken($this->token, self::DAYS_TO_EXPIRE); |
|
40
|
3 |
|
if ($token) { |
|
41
|
|
|
// Log in the user |
|
42
|
2 |
|
$user = $this->loginUser($token, $request); |
|
43
|
|
|
|
|
44
|
|
|
// Send them a flash message response |
|
45
|
2 |
|
if ($users->confirm($user)) { |
|
46
|
|
|
// If token is confirm token, show: |
|
47
|
1 |
|
return new FlashMessage( |
|
48
|
1 |
|
'alert-success', |
|
49
|
1 |
|
'Your email address has been confirmed. |
|
50
|
|
|
Look for new jobs in your inbox tomorrow.' |
|
51
|
|
|
); |
|
52
|
|
|
} else { |
|
53
|
|
|
// If token is login token, show: |
|
54
|
1 |
|
return new FlashMessage( |
|
55
|
1 |
|
'alert-success', |
|
56
|
1 |
|
'Welcome back! You are now logged in.' |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
1 |
|
return new FlashMessage( |
|
61
|
1 |
|
'alert-danger', |
|
62
|
1 |
|
'That token is invalid or expired. Please login to generate a new token.' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Handles the login and destruction of tokens |
|
68
|
|
|
* |
|
69
|
|
|
* @param Token $token |
|
70
|
|
|
* @param Request $request |
|
71
|
|
|
* |
|
72
|
|
|
* @return User |
|
73
|
|
|
*/ |
|
74
|
2 |
|
private function loginUser(Token $token, Request $request) |
|
75
|
|
|
{ |
|
76
|
|
|
// Invalidate the user's current session |
|
77
|
2 |
|
$request->session()->invalidate(); |
|
78
|
|
|
|
|
79
|
|
|
// Log in the user by token |
|
80
|
2 |
|
$request->session()->put('user', $token->user->toArray()); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// Destroy the token |
|
83
|
2 |
|
$token->delete(); |
|
84
|
|
|
|
|
85
|
2 |
|
return $token->user; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.