Commit 341ff697 authored by Viral Solani's avatar Viral Solani

sent custom payload with jwt token

parent 7d6c9fee
...@@ -2,10 +2,13 @@ ...@@ -2,10 +2,13 @@
namespace App\Http\Controllers\Api\V1; namespace App\Http\Controllers\Api\V1;
use Illuminate\Http\Request;
use JWTAuth; use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException; use Illuminate\Support\Facades\Hash;
use Validator; use Validator;
use Illuminate\Http\Request;
use App\Models\Access\User\User;
use App\Http\Resources\UserResource;
use Tymon\JWTAuth\Exceptions\JWTException;
class AuthController extends APIController class AuthController extends APIController
{ {
...@@ -16,6 +19,7 @@ class AuthController extends APIController ...@@ -16,6 +19,7 @@ class AuthController extends APIController
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function login(Request $request) public function login(Request $request)
{ {
$validation = Validator::make($request->all(), [ $validation = Validator::make($request->all(), [
...@@ -27,6 +31,48 @@ class AuthController extends APIController ...@@ -27,6 +31,48 @@ class AuthController extends APIController
return $this->throwValidation($validation->messages()->first()); return $this->throwValidation($validation->messages()->first());
} }
$credentials = $request->only('email', 'password');
$user = User::where('email', $credentials['email'])->firstOrFail();
if (!Hash::check($credentials['password'], $user->password)) {
return $this->throwValidation('invalid_credentials');
}
try {
$customClaims = [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'confirmed' => $user->confirmed,
'registered_at' => $user->created_at->toIso8601String(),
'last_updated_at' => $user->updated_at->toIso8601String(),
];
$token = JWTAuth::fromUser($user, $customClaims);
} catch (JWTException $e) {
return $this->respondInternalError($e->getMessage());
}
return $this->respond([
'message' => trans('api.messages.login.success'),
'token' => $token,
]);
}
/*public function login(Request $request)
{
$validation = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required|min:4',
]);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
$credentials = $request->only(['email', 'password']); $credentials = $request->only(['email', 'password']);
try { try {
...@@ -41,7 +87,7 @@ class AuthController extends APIController ...@@ -41,7 +87,7 @@ class AuthController extends APIController
'message' => trans('api.messages.login.success'), 'message' => trans('api.messages.login.success'),
'token' => $token, 'token' => $token,
]); ]);
} }*/
/** /**
* Log the user out (Invalidate the token). * Log the user out (Invalidate the token).
......
...@@ -24,7 +24,6 @@ class UserResource extends Resource ...@@ -24,7 +24,6 @@ class UserResource extends Resource
'role' => optional($this->roles()->first())->name, 'role' => optional($this->roles()->first())->name,
'registered_at' => $this->created_at->toIso8601String(), 'registered_at' => $this->created_at->toIso8601String(),
'last_updated_at' => $this->updated_at->toIso8601String(), 'last_updated_at' => $this->updated_at->toIso8601String(),
]; ];
} }
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "bc9d94beb0a4b88c5eaee8a9a2238bea", "content-hash": "10c3915573d2705afb9d125425e23680",
"packages": [ "packages": [
{ {
"name": "arcanedev/log-viewer", "name": "arcanedev/log-viewer",
...@@ -1414,16 +1414,16 @@ ...@@ -1414,16 +1414,16 @@
}, },
{ {
"name": "laravel/framework", "name": "laravel/framework",
"version": "v5.6.9", "version": "v5.6.11",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel/framework.git", "url": "https://github.com/laravel/framework.git",
"reference": "98fdbb098cf52a74441fe949be121c18e3dbbe6a" "reference": "195ba6a67bdad2a23105c7ab410cd43e0f20bb73"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel/framework/zipball/98fdbb098cf52a74441fe949be121c18e3dbbe6a", "url": "https://api.github.com/repos/laravel/framework/zipball/195ba6a67bdad2a23105c7ab410cd43e0f20bb73",
"reference": "98fdbb098cf52a74441fe949be121c18e3dbbe6a", "reference": "195ba6a67bdad2a23105c7ab410cd43e0f20bb73",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -1434,7 +1434,7 @@ ...@@ -1434,7 +1434,7 @@
"ext-openssl": "*", "ext-openssl": "*",
"league/flysystem": "~1.0", "league/flysystem": "~1.0",
"monolog/monolog": "~1.12", "monolog/monolog": "~1.12",
"nesbot/carbon": "^1.22.1", "nesbot/carbon": "^1.24.1",
"php": "^7.1.3", "php": "^7.1.3",
"psr/container": "~1.0", "psr/container": "~1.0",
"psr/simple-cache": "^1.0", "psr/simple-cache": "^1.0",
...@@ -1548,7 +1548,7 @@ ...@@ -1548,7 +1548,7 @@
"framework", "framework",
"laravel" "laravel"
], ],
"time": "2018-03-07T14:04:49+00:00" "time": "2018-03-09T16:53:27+00:00"
}, },
{ {
"name": "laravel/socialite", "name": "laravel/socialite",
...@@ -1614,16 +1614,16 @@ ...@@ -1614,16 +1614,16 @@
}, },
{ {
"name": "laravel/tinker", "name": "laravel/tinker",
"version": "v1.0.4", "version": "v1.0.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel/tinker.git", "url": "https://github.com/laravel/tinker.git",
"reference": "eb8d3cfb41b7f74fb0ef4724e459d44f4cbb35b8" "reference": "94f6daf2131508cebd11cd6f8632ba586d7ecc41"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel/tinker/zipball/eb8d3cfb41b7f74fb0ef4724e459d44f4cbb35b8", "url": "https://api.github.com/repos/laravel/tinker/zipball/94f6daf2131508cebd11cd6f8632ba586d7ecc41",
"reference": "eb8d3cfb41b7f74fb0ef4724e459d44f4cbb35b8", "reference": "94f6daf2131508cebd11cd6f8632ba586d7ecc41",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -1673,7 +1673,7 @@ ...@@ -1673,7 +1673,7 @@
"laravel", "laravel",
"psysh" "psysh"
], ],
"time": "2018-03-06T13:48:07+00:00" "time": "2018-03-06T17:34:36+00:00"
}, },
{ {
"name": "laravelcollective/html", "name": "laravelcollective/html",
...@@ -2091,20 +2091,20 @@ ...@@ -2091,20 +2091,20 @@
}, },
{ {
"name": "nesbot/carbon", "name": "nesbot/carbon",
"version": "1.23.0", "version": "1.24.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/briannesbitt/Carbon.git", "url": "https://github.com/briannesbitt/Carbon.git",
"reference": "4a874a39b2b00d7e0146cd46fab6f47c41ce9e65" "reference": "ef12cbd8ba5ce624f0a6a95e0850c4cc13e71e41"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/4a874a39b2b00d7e0146cd46fab6f47c41ce9e65", "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/ef12cbd8ba5ce624f0a6a95e0850c4cc13e71e41",
"reference": "4a874a39b2b00d7e0146cd46fab6f47c41ce9e65", "reference": "ef12cbd8ba5ce624f0a6a95e0850c4cc13e71e41",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.3.9",
"symfony/translation": "~2.6 || ~3.0 || ~4.0" "symfony/translation": "~2.6 || ~3.0 || ~4.0"
}, },
"require-dev": { "require-dev": {
...@@ -2140,7 +2140,7 @@ ...@@ -2140,7 +2140,7 @@
"datetime", "datetime",
"time" "time"
], ],
"time": "2018-02-28T09:22:05+00:00" "time": "2018-03-09T15:49:34+00:00"
}, },
{ {
"name": "nikic/php-parser", "name": "nikic/php-parser",
...@@ -3605,16 +3605,16 @@ ...@@ -3605,16 +3605,16 @@
}, },
{ {
"name": "tymon/jwt-auth", "name": "tymon/jwt-auth",
"version": "dev-develop", "version": "1.0.0-rc.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/tymondesigns/jwt-auth.git", "url": "https://github.com/tymondesigns/jwt-auth.git",
"reference": "592e5e10c4e2d40bbf6db7d0e64367847118606a" "reference": "d5220f6a84cbb8300f6f2f0f20aa908d072b4e4b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/tymondesigns/jwt-auth/zipball/592e5e10c4e2d40bbf6db7d0e64367847118606a", "url": "https://api.github.com/repos/tymondesigns/jwt-auth/zipball/d5220f6a84cbb8300f6f2f0f20aa908d072b4e4b",
"reference": "592e5e10c4e2d40bbf6db7d0e64367847118606a", "reference": "d5220f6a84cbb8300f6f2f0f20aa908d072b4e4b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -3676,7 +3676,7 @@ ...@@ -3676,7 +3676,7 @@
"jwt", "jwt",
"laravel" "laravel"
], ],
"time": "2018-03-05T22:18:39+00:00" "time": "2018-02-07T20:55:14+00:00"
}, },
{ {
"name": "unisharp/laravel-filemanager", "name": "unisharp/laravel-filemanager",
...@@ -6089,7 +6089,7 @@ ...@@ -6089,7 +6089,7 @@
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": { "stability-flags": {
"tymon/jwt-auth": 20 "tymon/jwt-auth": 5
}, },
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment