File tree Expand file tree Collapse file tree 7 files changed +716
-6
lines changed Expand file tree Collapse file tree 7 files changed +716
-6
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Http \Controllers ;
4
+
5
+ use Illuminate \Http \Request ;
6
+
7
+ class AuthController extends Controller
8
+ {
9
+ public function __construct ()
10
+ {
11
+ $ this ->middleware ('auth:api ' , ['except ' => ['login ' ]]);
12
+ }
13
+
14
+ public function login () {
15
+ $ credentials = request (['email ' , 'password ' ]);
16
+
17
+ if (! $ token = auth ()->attempt ($ credentials )) {
18
+ return response ()->json (['error ' => 'Invalid email or password ' ], 401 );
19
+ }
20
+
21
+ return $ this ->respondWithToken ($ token );
22
+ }
23
+
24
+ private function respondWithToken ($ token ) {
25
+ return response ()->json ([
26
+ 'token ' => $ token ,
27
+ 'access_type ' => 'bearer ' ,
28
+ 'expires_in ' => auth ()->factory ()->getTTL () * 60
29
+ ]);
30
+ }
31
+
32
+
33
+ public function logout () {
34
+ auth ()->logout ();
35
+ return response ()->json (['msg ' => 'User successfully logged out ' ]);
36
+ }
37
+
38
+
39
+ public function refresh () {
40
+ return $ this ->respondWithToken (auth ()->refresh ());
41
+ }
42
+
43
+ public function me () {
44
+ return response ()->json (auth ()->user ());
45
+ }
46
+ }
Original file line number Diff line number Diff line change 5
5
use Illuminate \Contracts \Auth \MustVerifyEmail ;
6
6
use Illuminate \Foundation \Auth \User as Authenticatable ;
7
7
use Illuminate \Notifications \Notifiable ;
8
+ use Tymon \JWTAuth \Contracts \JWTSubject ;
8
9
9
- class User extends Authenticatable
10
+ class User extends Authenticatable implements JWTSubject
10
11
{
11
12
use Notifiable;
12
13
@@ -36,4 +37,20 @@ class User extends Authenticatable
36
37
protected $ casts = [
37
38
'email_verified_at ' => 'datetime ' ,
38
39
];
40
+
41
+ /**
42
+ * @inheritDoc
43
+ */
44
+ public function getJWTIdentifier ()
45
+ {
46
+ return $ this ->getKey ();
47
+ }
48
+
49
+ /**
50
+ * @inheritDoc
51
+ */
52
+ public function getJWTCustomClaims ()
53
+ {
54
+ return [];
55
+ }
39
56
}
Original file line number Diff line number Diff line change 13
13
"fruitcake/laravel-cors" : " ^1.0" ,
14
14
"guzzlehttp/guzzle" : " ^6.3" ,
15
15
"laravel/framework" : " ^7.0" ,
16
- "laravel/tinker" : " ^2.0"
16
+ "laravel/tinker" : " ^2.0" ,
17
+ "tymon/jwt-auth" : " ^1.0"
17
18
},
18
19
"require-dev" : {
19
20
"facade/ignition" : " ^2.0" ,
You can’t perform that action at this time.
0 commit comments