I got a bug, I think it's from laravel. Let says there is user1, user2, user3. When user1 and user2 login, the data is returned from that user using $request->user()
and the data shows the correct users.
But when accessing another controller through different api then the data returned by $request->user()
changes to different user. lets says it is user X (yes user X its only 1 user). When user3 login, all flow before works perfectly fine. Even using different controller still return the correct user. Here is my code.
Login function
public function login(LoginRequest $req)
{
$credentials = [
'username' => $req->has('email') ? $req->email : $req->username,
'password' => $req->password
];
try {
if (!Auth::attempt($credentials)) {
return $this->sendError('The provided credentials do not match our records.', 400);
}
$user = $req->user();
return $this->sendSuccess('Login successfully.', $this->authResponse($user));
} catch (\Exception $e) {
return $this->sendError($e->getMessage());
}
}
LoginRequest Validation
public function rules()
{
return [
'email' => 'required|email',
'password' => 'required'
];
}
Routes to get user
Route::middleware(['auth:sanctum'])->group(function(){
Route::get('user', function (Request $request) {
return $request->user();
});
});
I tried changing different code with Auth::user()
still it doesn't return the right user.
i just solved this issue using solution from this question : stackoverflow.com/a/67914315. im sorry if this is a duplicate question