Skip to content

Commit 13a1bd9

Browse files
committed
add dingo and jwt support
1 parent 497fae6 commit 13a1bd9

File tree

18 files changed

+1029
-6015
lines changed

18 files changed

+1029
-6015
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ MAIL_ENCRYPTION=null
3030
PUSHER_APP_ID=
3131
PUSHER_KEY=
3232
PUSHER_SECRET=
33+
34+
API_PREFIX=api
35+
API_VERSION=v1
36+
API_DEBUG=true
37+

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/node_modules
22
/public/storage
3+
/public/vendor
4+
/public/css/*
5+
/public/js/*
6+
/storage/api-docs
37
/vendor
48
/.idea
59
Homestead.json

app/Http/Controllers/Auth/LoginController.php

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,18 @@
44

55
use App\Http\Controllers\Controller;
66
use Illuminate\Foundation\Auth\AuthenticatesUsers;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Lang;
79

810
class LoginController extends Controller
911
{
10-
/*
11-
|--------------------------------------------------------------------------
12-
| Login Controller
13-
|--------------------------------------------------------------------------
14-
|
15-
| This controller handles authenticating users for the application and
16-
| redirecting them to your home screen. The controller uses a trait
17-
| to conveniently provide its functionality to your applications.
18-
|
19-
*/
2012

2113
use AuthenticatesUsers;
2214

23-
/**
24-
* Where to redirect users after login.
25-
*
26-
* @var string
27-
*/
28-
protected $redirectTo = '/home';
15+
public function username()
16+
{
17+
return 'username';
18+
}
2919

3020
/**
3121
* Create a new controller instance.
@@ -36,4 +26,56 @@ public function __construct()
3626
{
3727
$this->middleware('guest', ['except' => 'logout']);
3828
}
29+
30+
/**
31+
* @SWG\Post(
32+
* tags={"auth"},
33+
* path="/auth/token",
34+
* operationId="token",
35+
* summary="Request new JWT Token.",
36+
* description="",
37+
* consumes={"application/json", "application/xml"},
38+
* produces={"application/xml", "application/json"},
39+
* @SWG\Parameter(
40+
* name="username",
41+
* in="query",
42+
* type="string",
43+
* description="ID",
44+
* required=true,
45+
* ),
46+
* @SWG\Parameter(
47+
* name="password",
48+
* in="query",
49+
* type="string",
50+
* description="Password",
51+
* required=true,
52+
* ),
53+
* @SWG\Response(
54+
* response=200,
55+
* description="Token successful",
56+
* )
57+
* )
58+
*/
59+
public function login(Request $request)
60+
{
61+
62+
$credentials = [
63+
'username' => $request['username'],
64+
'password' => $request['password'],
65+
];
66+
67+
$this->validateLogin($request);
68+
69+
if ($token = $this->guard()->attempt($credentials)) {
70+
$this->clearLoginAttempts($request);
71+
return response()->json([
72+
'token' => $token,
73+
]);
74+
}
75+
76+
return response()->json([
77+
'message' => Lang::get('auth.failed'),
78+
], 401);
79+
80+
}
3981
}

app/User.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,39 @@
22

33
namespace App;
44

5+
use Illuminate\Auth\Authenticatable;
56
use Illuminate\Notifications\Notifiable;
6-
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
use Illuminate\Foundation\Auth\Access\Authorizable;
8+
use Illuminate\Auth\Passwords\CanResetPassword;
9+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
710

8-
class User extends Authenticatable
11+
use Tymon\JWTAuth\Contracts\JWTSubject as AuthenticatableUserContract;
12+
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
13+
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
14+
15+
use Illuminate\Support\Facades\Hash;
16+
17+
# For MySQL
18+
use Illuminate\Database\Eloquent\Model;
19+
20+
# For MongoDB
21+
# use Jenssegers\Mongodb\Eloquent\Model;
22+
23+
class User extends Model implements
24+
AuthenticatableContract,
25+
AuthorizableContract,
26+
CanResetPasswordContract,
27+
AuthenticatableUserContract
928
{
10-
use Notifiable;
29+
use Authenticatable, Authorizable, CanResetPassword, Notifiable;
1130

1231
/**
1332
* The attributes that are mass assignable.
1433
*
1534
* @var array
1635
*/
1736
protected $fillable = [
18-
'name', 'email', 'password',
37+
'username', 'password'
1938
];
2039

2140
/**
@@ -24,6 +43,23 @@ class User extends Authenticatable
2443
* @var array
2544
*/
2645
protected $hidden = [
27-
'password', 'remember_token',
46+
'password'
2847
];
48+
49+
/**
50+
* @return mixed
51+
*/
52+
public function getJWTIdentifier()
53+
{
54+
return $this->getKey(); // Eloquent model method
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
public function getJWTCustomClaims()
61+
{
62+
return [];
63+
}
64+
2965
}

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"type": "project",
77
"require": {
88
"php": ">=5.6.4",
9-
"laravel/framework": "5.3.*"
9+
"laravel/framework": "5.3.*",
10+
"dingo/api": "1.0.x@dev",
11+
"tymon/jwt-auth": "1.0.*@dev",
12+
"darkaonline/l5-swagger": "~3.0"
1013
},
1114
"require-dev": {
1215
"fzaninotto/faker": "~1.4",
@@ -39,7 +42,8 @@
3942
"Illuminate\\Foundation\\ComposerScripts::postInstall",
4043
"php artisan optimize",
4144
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
42-
"php artisan key:generate"
45+
"php artisan key:generate",
46+
"php artisan jwt:secret"
4347
],
4448
"post-update-cmd": [
4549
"Illuminate\\Foundation\\ComposerScripts::postUpdate",

0 commit comments

Comments
 (0)