|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of laravel.su package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace App\Http\Controllers\Auth; |
|
12
|
|
|
|
|
13
|
|
|
use App\Models\User; |
|
14
|
|
|
use Illuminate\Support\Arr; |
|
15
|
|
|
use App\Http\Controllers\Controller; |
|
16
|
|
|
use Illuminate\Contracts\Auth\Guard; |
|
17
|
|
|
use Illuminate\Contracts\Session\Session; |
|
18
|
|
|
use Illuminate\Contracts\Auth\StatefulGuard; |
|
19
|
|
|
use Tymon\JWTAuth\Providers\JWT\JWTInterface; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ConfirmationController. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ConfirmationController extends Controller |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @param Session $session |
|
29
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
30
|
|
|
*/ |
|
31
|
|
|
public function index(Session $session) |
|
32
|
|
|
{ |
|
33
|
|
|
if (! $session->has('user')) { |
|
34
|
|
|
return redirect()->route('home'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return view('page.auth.confirmed', [ |
|
38
|
|
|
'user' => $session->get('user'), |
|
39
|
|
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $token |
|
44
|
|
|
* @param JWTInterface $crypt |
|
45
|
|
|
* @param Guard $guard |
|
46
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \LogicException |
|
49
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function confirm(string $token, JWTInterface $crypt, Guard $guard) |
|
52
|
|
|
{ |
|
53
|
|
|
$email = Arr::get($crypt->decode($token), 'email'); |
|
54
|
|
|
|
|
55
|
|
|
/** @var User $user */ |
|
56
|
|
|
$user = User::whereEmail($email)->first(); |
|
57
|
|
|
|
|
58
|
|
|
if (! $user) { |
|
59
|
|
|
throw new NotFoundHttpException('User with target id not found'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($user->is_confirmed) { |
|
63
|
|
|
throw new \LogicException('User\'s e-mail already confirmed'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$user->is_confirmed = true; |
|
67
|
|
|
$user->save(); |
|
68
|
|
|
|
|
69
|
|
|
/* @var StatefulGuard $guard */ |
|
70
|
|
|
$guard->login($user, true); |
|
71
|
|
|
|
|
72
|
|
|
return redirect() |
|
73
|
|
|
->route('confirmation.confirmed') |
|
74
|
|
|
->with('user', $user); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|