Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanctum still logs in after logging out & deleting tokens in a scenario where you dont refresh browser #538

Open
bloodykheeng opened this issue Oct 30, 2024 · 1 comment

Comments

@bloodykheeng
Copy link

Sanctum Version

4.0

Laravel Version

11.0

PHP Version

8.2

Database Driver & Version

No response

Description

After logging out and i dont refresh the browser sanctum still logs me in
i have a laravel api and react front end

Steps To Reproduce

This is my log out in laravel api ive tried all solutions they have failed thats why u see many comments there


    public function logout(Request $request)
    {
        /** @var \App\Models\User */
        $user = Auth::user(); // Get the authenticated user

        $userToken = $user->token();
        $userToken->revoke();

        // // // Delete all tokens for the user
        // // $user->tokens()->delete();

        // // delete the current token that was used for the request
        // $request->user()->currentAccessToken()->delete();

        // //
        // session()->flush();

        // Auth::logout();

        // $request->session()->invalidate();

        // $request->session()->regenerateToken();

        return response()->json(['message' => 'Logged out successfully']);
    }

react
uill see in my react i had to first reload the browser which isnt cool
window.location.reload();


  // Logout mutation function
    const logoutMutation = useMutation({
        mutationFn: logout,
        onSuccess: (data) => {
            setLogoutMutationIsLoading(false);

            // clear local storage
            // localStorage.removeItem("access_token");
            // localStorage.removeItem("refresh_token");

            // Set localStorage items to null
            // localStorage.setItem("access_token", "null");
            // localStorage.setItem("refresh_token", "null");

            localStorage.removeItem("access_token");
            localStorage.removeItem("refresh_token");

            // Reset the user state to null
            queryClient.resetQueries();
            queryClient.clear();
            queryClient.refetchQueries();

            // Remove the Authorization header from Axios
            axios.defaults.headers.common["Authorization"] = null;

            // Remove the Authorization header from Axios
            delete axios.defaults.headers.common["Authorization"];

            // Set loading state to false
            console.log("logout response is : ", data);
            navigate("/login");
            window.location.reload();
            // Display a success toast message
            toast.success("Goodbye 👋");
        },
        onError: (error) => {
            setLogoutMutationIsLoading(false);
            // Display an error toast message
            toast.error("Logout Error");

            // Log the error
            console.log("Logout errors ", error);
        },
    });

@bloodykheeng bloodykheeng changed the title Sanctum still logs in after deleting tokens & you dont refresh browser Sanctum still logs in after logging out & deleting tokens in a scenario where you dont refresh browser Oct 30, 2024
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants