#ConvexAuth SignOut not redirecting
7 messages · Page 1 of 1 (latest)
Hi @elfin shell. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Convex, you can control the redirection after signing out by using the signOut function with the redirectTo option. Here's an example of how you can do this:
import { signOut } from "@/auth";
export function SignOut() {
return (
<form
action={async () => {
"use server";
await signOut({ redirectTo: "/" });
}}
>
<button type="submit">Sign out</button>
</form>
);
}
In this example, after signing out, the user is redirected to the home page ("/"). You can replace "/" with the route you want to redirect to after sign out.
If you're still facing issues, it might be due to the delay in the propagation of the sign out event. You might want to consider using a useEffect hook that listens to the isAuthenticated state and redirects the user when it changes to false.
Please refer to the source (https://stack.convex.dev/nextauth#tips) for more details.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@elfin shell kapa.ai is still learning and improving, please let us know how it did by reacting below
@tranquil hawk I'm using ```
import { useAuthActions } from "@convex-dev/auth/react";
const { signOut } = useAuthActions()
Let me check the knowledge base for you. One moment! :books:
In Convex, the signOut function from useAuthActions is used to sign out the current user. However, it doesn't handle redirection by itself. You need to handle the redirection in your application logic after the signOut function is called.
Here's an example of how you can do this in a React component:
import { useAuthActions } from "@convex-dev/auth/react";
import { useHistory } from "react-router-dom";
export function SignOutButton() {
const { signOut } = useAuthActions();
const history = useHistory();
const handleSignOut = async () => {
await signOut();
history.push('/signin'); // replace '/signin' with your sign in route
};
return <button onClick={handleSignOut}>Sign out</button>;
}
In this example, useHistory from react-router-dom is used to programmatically navigate to the sign in route after the user is signed out. Replace '/signin' with the actual route to your sign in page.
Please note that this example assumes that you're using react-router-dom for routing in your application. If you're using a different routing library, the way to programmatically navigate to a different route might be different.