AuthController::authenticate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Autumn\JWTAuth\Http\Controllers;
4
5
use Validator;
6
use Tymon\JWTAuth\JWTAuth;
7
use Illuminate\Http\Request;
8
use RainLab\User\Models\User;
9
use Tymon\JWTAuth\Exceptions\JWTException;
10
use Illuminate\Routing\Controller as BaseController;
11
12
class AuthController extends BaseController
13
{
14
    protected $auth;
15
16
    public function __construct(JWTAuth $auth)
17
    {
18
        $this->auth = $auth;
19
    }
20
21
    public function authenticate(Request $request)
22
    {
23
        try {
24
            if (! $token = $this->auth->attempt($request->only('email', 'password'))) {
25
                return response()->json([
26
                    'errors' => [
27
                        'root' => 'Could not sign you in with those details.',
28
                    ],
29
                ], 401);
30
            }
31
        } catch (JWTException $e) {
32
            return response()->json([
33
                'errors' => [
34
                    'root' => 'Failed.',
35
                ],
36
            ], $e->getStatusCode());
37
        }
38
39
        $user = $this->auth->authenticate($token);
40
        $user->afterLogin();
41
42
        return response()->json([
43
            'data' => $request->user(),
44
            'meta' => [
45
                'token' => $token,
46
            ],
47
        ]);
48
    }
49
50
    public function register(Request $request)
51
    {
52
        $data = $request->all();
53
54
        if (! array_key_exists('password_confirmation', $request->all())) {
55
            $data['password_confirmation'] = $request->get('password');
56
        }
57
58
        $validator = $this->validator($data);
59
60
        if ($validator->fails()) {
61
            return response()->json([
62
                'error' => $validator->getMessageBag(),
63
            ], 400);
64
        }
65
66
        $user = User::create($data);
67
        $token = $this->auth->attempt($request->only('email', 'password'));
68
69
        return response()->json([
70
            'data' => $user,
71
            'meta' => [
72
                'token' => $token,
73
            ],
74
        ]);
75
    }
76
77
    public function user(Request $request)
78
    {
79
        return response()->json([
80
            'data' => $request->user(),
81
        ]);
82
    }
83
84
    public function logout()
85
    {
86
        $this->auth->invalidate($this->auth->getToken());
87
88
        return response(null, 200);
89
    }
90
91
    /**
92
     * Get a validator for an incoming registration request.
93
     *
94
     * @param array $data
95
     *
96
     * @return \Illuminate\Contracts\Validation\Validator
97
     */
98
    protected function validator(array $data)
99
    {
100
        return Validator::make($data, [
101
            'username' => 'required|between:3,64|unique:users',
102
            'email' => 'required|between:3,64|email|unique:users',
103
            'password' => 'required|between:4,64|confirmed',
104
        ]);
105
    }
106
}
107