Completed
Push — fixes ( 6830e4...798510 )
by Tony
03:34
created

APIAuthController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 18 3
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use App\Http\Requests;
6
use Illuminate\Http\Request;
7
use JWTAuth;
8
use Tymon\JWTAuth\Exceptions\JWTException;
9
10
class APIAuthController extends Controller
11
{
12
    public function authenticate(Request $request)
13
    {
14
        // grab credentials from the request
15
        $credentials = $request->only('username', 'password');
16
17
        try {
18
            // attempt to verify the credentials and create a token for the user
19
            if (!$token = JWTAuth::attempt($credentials)) {
20
                return response()->json(['error' => 'invalid_credentials'], 401);
21
            }
22
        } catch (JWTException $e) {
23
            // something went wrong whilst attempting to encode the token
24
            return response()->json(['error' => 'could_not_create_token'], 500);
25
        }
26
27
        // all good so return the token
28
        return response()->json(compact('token'));
29
    }
30
}
31