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

APIAuthController::authenticate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
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