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

Issue on docs. NextJS Logout Button. #18

Closed
Jemeni11 opened this issue Oct 13, 2023 · 2 comments · Fixed by #19
Closed

Issue on docs. NextJS Logout Button. #18

Jemeni11 opened this issue Oct 13, 2023 · 2 comments · Fixed by #19

Comments

@Jemeni11
Copy link
Contributor

Path: /quickstarts/fullstack/next

Next JS - Implement logout functionality

Pages Directory

You can use @teamhanko/hanko-elements to easily manage user logouts. Below, we make a logout button component that you can use anywhere.

import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import { Hanko } from "@teamhanko/hanko-elements";

const hankoApi = process.env.NEXT_PUBLIC_HANKO_API_URL;

export function LogoutBtn() {
  const router = useRouter();
  const [hanko, setHanko] = useState<Hanko>();

  useEffect(() => {
    import("@teamhanko/hanko-elements").then(({ Hanko }) =>
      setHanko(new Hanko(hankoApi ?? ""))
    );
  }, []);

  const logout = async () => {
    try {
      await hanko?.user.logout();
      router.push("/login");
      router.refresh(); <---------- error
      return;
    } catch (error) {
      console.error("Error during logout:", error);
    }
  };

  return <button onClick={logout}>Logout</button>;
}

The docs mention router.refresh() but that method doesn't exist on the Next Router object. I think the author of this meant to use router.reload()

@Ashutosh-Bhadauriya
Copy link
Collaborator

Hey @Jemeni11 thanks for the pr. Yeah you're right, but router.refresh is available in nextjs app directory. And you seem to have replace the router.refresh in both the codes(app directory and pages directory). can you please fix that. Thanks!

@Jemeni11
Copy link
Contributor Author

Hey! Thanks for that, I've seen and corrected my mistake

Ashutosh-Bhadauriya pushed a commit that referenced this issue Oct 13, 2023
* Update next.mdx

Fixes #18

* Update next.mdx

Changed all occurrences of `router.refresh()` to `router.reload()`

* Update next.mdx

Reversed the change to the App directory example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants