Tôi có một chương trình phụ trợ Laravel 5 gửi một mã thông báo jwt như một câu trả lời json khi đăng nhập với jwt-auth.Thêm vai trò người dùng vào jwt, laravel 5 jwt-auth
Bây giờ tôi muốn thêm vai trò người dùng đến thẻ JWT rằng laravel gửi, tôi đã thử cách sau:
Đây là bộ điều khiển hiện tại của tôi
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Database\Eloquent\Model;
use App\User;
class AuthenticateController extends Controller
{
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
$user = User::where('email', '=', $credentials['email'])->first();
$customClaims = ['role' => $user->role];
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials, $customClaims)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(compact('token'));
}
}
?>
Có cách nào sạch hơn để làm điều này ?
JWT cho php là tài liệu không hợp lệ. Để làm cho một hệ thống đăng nhập/đăng xuất đơn giản là đau đớn! Xem câu hỏi của tôi về nó. – Maykonn
Tôi đang tìm kiếm điều tương tự, bạn có tìm thấy giải pháp nào không? – kitensei
Tôi đã đi với các giải pháp trên, ngay cả khi nó là một trong nhiều truy vấn. Không thể tìm thấy giải pháp sạch hơn – RVandersteen