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

No session when fetching during SSR #97

Closed
raggesilver opened this issue Jun 4, 2024 · 4 comments · Fixed by #99
Closed

No session when fetching during SSR #97

raggesilver opened this issue Jun 4, 2024 · 4 comments · Fixed by #99

Comments

@raggesilver
Copy link
Contributor

raggesilver commented Jun 4, 2024

I've noticed that $fetching during SSR results in a 401 when calling getUserSession/requireUserSession in the server.

GitHub setup:

import { AUTHORIZED_REDIRECT } from "~/lib/constants";
import { getOrCreateUser } from "~/server/services/user";

export default oauth.githubEventHandler({
  config: {
    emailRequired: true,
  },
  async onSuccess(event, { user: profile }) {
    const user = await getOrCreateUser("github", profile);
    await setUserSession(event, {
      user,
    });
    return sendRedirect(event, AUTHORIZED_REDIRECT);
  },
  // Optional, will return a json error and 401 status code by default
  onError(event, error) {
    console.error("GitHub OAuth error:", error, event);
    return sendRedirect(event, "/");
  },
});

API route setup:

import { getAllWorkspaces } from "~/server/services/workspace";

export default defineEventHandler(async (event) => {
  const session = await getUserSession(event);

  if (!session.user) {
    console.log("No user", JSON.stringify(event.node.req, null, 2));

    throw createError({ status: 401, message: "Unauthorized" });
  }

  return getAllWorkspaces(session.user.id);
});

Use in component:

const client = useQueryClient();
const { data, isLoading } = useQuery(
  {
    queryKey: ["workspaces"],
    queryFn: () =>
      $fetch("/api/workspace").catch((err) => {
        // Prints error in SSR
        console.error(err);
        throw err;
      }),
  },
  client,
);

No error is thrown in the client, and the data is fetched correctly. Erros on the server are only printed once (during SSR). Am I doing something wrong?

Package versions
nuxt-auth-utils@0.0.25
nuxt@3.11.2
nitropack@2.9.6
h3@1.11.1

node: v21.7.3

Full repo.

Copy link
Owner

atinux commented Jun 5, 2024

@raggesilver could you try with useRequestFest() instead?

See more context in nuxt/nuxt#24813

@raggesilver
Copy link
Contributor Author

That works! I take it from the issue you linked this is a bug in Nuxt, right? If not, it would be good to have this in the README.

Thanks

Copy link
Owner

atinux commented Jun 5, 2024

It's not directly a bug but something we can improve in Nuxt for the long term.

Would you be happy to help me improve this readme to explain the usage of useRequestFetch() for this module?

@raggesilver
Copy link
Contributor Author

For sure! I'll open a PR tomorrow.

raggesilver added a commit to raggesilver/nuxt-auth-utils that referenced this issue Jun 10, 2024
atinux added a commit that referenced this issue Jun 10, 2024
* Include SSR instructions in the README, fixes #97

* Update README.md

---------

Co-authored-by: Sébastien Chopin <seb@nuxtlabs.com>
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