Completed
Push — master ( 856338...f2b942 )
by Pásztor
05:13
created

AuthController::authenticate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
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
        return response()->json([
40
            'data' => $request->user(),
41
            'meta' => [
42
                'token' => $token,
43
            ],
44
        ]);
45
    }
46
47
    public function register(Request $request)
48
    {
49
        $data = $request->all();
50
51
        if (! array_key_exists('password_confirmation', $request->all())) {
52
            $data['password_confirmation'] = $request->get('password');
53
        }
54
55
        $validator = $this->validator($data);
56
57
        if ($validator->fails()) {
58
            return response()->json([
59
                'error' => $validator->getMessageBag(),
60
            ], 400);
61
        }
62
63
        $user = User::create($data);
64
        $token = $this->auth->attempt($request->only('email', 'password'));
65
66
        return response()->json([
67
            'data' => $user,
68
            'meta' => [
69
                'token' => $token,
70
            ],
71
        ]);
72
    }
73
74
    public function user(Request $request)
75
    {
76
        return response()->json([
77
            'data' => $request->user(),
78
        ]);
79
    }
80
81
    public function logout()
82
    {
83
        $this->auth->invalidate($this->auth->getToken());
84
85
        return response(null, 200);
86
    }
87
88
    /**
89
     * Get a validator for an incoming registration request.
90
     *
91
     * @param array $data
92
     *
93
     * @return \Illuminate\Contracts\Validation\Validator
94
     */
95
    protected function validator(array $data)
96
    {
97
        return Validator::make($data, [
98
            'username' => 'required|between:3,64|unique:users',
99
            'email' => 'required|between:3,64|email|unique:users',
100
            'password' => 'required|between:4,64|confirmed',
101
        ]);
102
    }
103
}
104