The issue you're encountering is due to the fact that Auth::logoutOtherDevices($password) is not available when using Sanctum's session-based authentication. This method is part of the Illuminate\Auth\SessionGuard, which is used by the default web guard, but not by Sanctum's RequestGuard.
To achieve the desired functionality of logging out from other devices when a password is changed, you can manually handle this by invalidating all other sessions for the user. Here's a step-by-step solution:
-
Use the
Sessionfacade to manage sessions:You can manually delete all sessions for the user except the current one. This requires storing session IDs in the database and associating them with users.
-
Create a migration for sessions table:
If you haven't already, ensure you have a
sessionstable. You can create it using the following command:php artisan session:table php artisan migrate -
Update the
passwordUpdatemethod inAuthController:Modify the method to manually delete other sessions for the user:
use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Session; public function passwordUpdate(Request $request) { $user = Auth::user(); if (! Hash::check($request->get('password'), $user->password)) { return response()->json(['error' => 'Incorrect password provided.'], 400); } try { $user->password = Hash::make($request->get('passwordNew')); $user->save(); // Get the current session ID $currentSessionId = Session::getId(); // Delete all other sessions for the user DB::table('sessions') ->where('user_id', $user->id) ->where('id', '!=', $currentSessionId) ->delete(); } catch (\Exception $e) { return response()->json(['error' => 'An error occurred while updating the password.'], 500); } return response()->json(['success' => 'You have updated your password successfully.']); } -
Ensure session driver is set to
database:Make sure your
SESSION_DRIVERin.envis set todatabase:SESSION_DRIVER=database -
Update the
Sessionmodel if necessary:If you have a custom
Sessionmodel, ensure it has auser_idcolumn and is properly associated with theUsermodel.
By following these steps, you should be able to log out users from other devices when they change their password, even when using Sanctum's session-based authentication.