|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Claudio Cardinale <[email protected]> |
|
5
|
|
|
* Date: 18/11/15 |
|
6
|
|
|
* Time: 16.23 |
|
7
|
|
|
* This program is free software; you can redistribute it and/or |
|
8
|
|
|
* modify it under the terms of the GNU General Public License |
|
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
10
|
|
|
* of the License, or (at your option) any later version. |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program; if not, write to the Free Software |
|
17
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Tymon\JWTAuth\Support\auth; |
|
21
|
|
|
|
|
22
|
|
|
use Illuminate\Http\JsonResponse; |
|
23
|
|
|
use Illuminate\Http\Request; |
|
24
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class RegistersUsers |
|
28
|
|
|
* @package Tymon\JWTAuth\Support\auth |
|
29
|
|
|
* @author Claudio Cardinale <[email protected]> |
|
30
|
|
|
* @copyright 2015 Claudio Cardinale |
|
31
|
|
|
* @version 1.0.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
trait RegistersUsers |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Handle a registration request for the application. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Illuminate\Http\Request $request |
|
39
|
|
|
* @return \Illuminate\Http\Response |
|
40
|
|
|
*/ |
|
41
|
|
|
public function postRegister(Request $request) |
|
42
|
|
|
{ |
|
43
|
|
|
$validator = $this->validator($request->all()); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if ($validator->fails()) { |
|
46
|
|
|
$this->throwValidationException( |
|
|
|
|
|
|
47
|
|
|
$request, $validator |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$token = JWTAuth::fromUser($this->create($request->all()), $this->customClaims()); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$response = new JsonResponse(['token' => $token], 200); |
|
54
|
|
|
$response->header('Authorization', 'Bearer ' . $token); |
|
55
|
|
|
return $response; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the custom claims |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function customClaims() |
|
64
|
|
|
{ |
|
65
|
|
|
return property_exists($this, 'custom') ? $this->custom : []; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.