response->json($array) giving out too much input and not just json? Hi,
I'm hoping for a clean response using:
$authentication_array = ['api_token' => $token];
echo response()->json($authentication_array);
Like so:
{"api_token":"n4dmXOAjWwmt3mOR"}
Instead I get this:
HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json Date: Fri, 12 Apr 2019 09:39:51 GMT {"api_token":"n4dmXOAjWwmt3mOR"}
Any idea what I need to edit?
You are seeing the string representation of the response object which includes headers and data. Why are you echoing the response; just return it?
$authentication_array = ['api_token' => $token];
return response()->json($authentication_array);
@TYKUS - Thanks for the response!!!
Oh, I was running this in the "authenticated" section of the LoginController:
protected function authenticated(Request $request, $user)
{
if ($user) {
$token = Str::random(16);
$user->api_token = $token;
$user->save();
$authentication_array = ['api_token' => $token];
return response()->json($authentication_array);
}
}
Which if I return then nothing echos out. Where should I output the api_token after login?
In your function your passing only $user you have to call correct parameter as like below.Hope your using User model.
protected function authenticated(Request $request,User $user)
{
//your codes
}
Which if I return then nothing echos out
Really? The sendLoginResponse() method should return the result of authenticated() if is not falsey. Have you done anything to override other LoginController methods (or AuthenticatesUser trait)?
@TYKUS - Thanks TYKUS. This is my ApiLoginController that i'm overriding some functions. Am I doing something really dumb?:
class ApiAuthentication extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function index(Request $request)
{
$this->login($request);
}
public function login(Request $request)
{
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
protected function sendLoginResponse(Request $request)
{
return $this->authenticated($request, $this->guard()->user());
}
protected function authenticated(Request $request, $user)
{
if ($user) {
$token = Str::random(16);
$user->api_token = $token;
$user->save();
$authentication_array = ['api_token' => $token];
echo(response()->json($authentication_array));
}
}
protected function guard()
{
return Auth::guard();
}
}
Hmm - Maybe it's an issue with guards...
@ASHLEYP - shouldn't your index method return the result from your login method ?
public function index(Request $request)
{
return $this->login($request);
}
@STEREOH - Oh...wow...
Stereoh thankyou so much! I've been trying to find this for nearly 2 hours!
@ASHLEYP - No problem, have fun :)
Don't forget to mark the post as answered !
Please sign in or create an account to participate in this conversation.