我有一个用户登录的方法,但是我想在用户Transfomer上传递他的令牌,怎么办? 我正在使用Fractal。
在我使用普通的json响应之前,该响应将返回数据和令牌。
// Authcontroller
公共功能authenticate(Request $ request){
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (!$token = JWTAuth::attempt($credentials)) {
return $this->response->errorUnauthorized('dados incorretos');
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return $this->response->withError('could_not_create_token', 500);
}
$user = getAuthUser();
if (!$user->active) {
return $this->response->errorUnauthorized('usuário não ativo');
}
$user = $user->token;
// all good so return the token
return $this->response->withItem($user, new UserTransformer());
}
// UserTransformer
public function transform(User $user)
{
return [
'id' => (int)$user->id,
'name' => $user->name,
'email' => $user->email,
'photo_url' => $user->photo_url,
'active' => $user->active,
'phone_number' => $user->phone_number,
'company_id' => $user->company_id,
'token' => $user->token,
];
}