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

updateSession doesn't persist the new session #127

Open
Hebilicious opened this issue Oct 30, 2023 Discussed in #123 · 1 comment
Open

updateSession doesn't persist the new session #127

Hebilicious opened this issue Oct 30, 2023 Discussed in #123 · 1 comment

Comments

@Hebilicious
Copy link
Owner

Discussed in #123

Originally posted by miguelakira October 26, 2023
I tried using updateSession to update the session with an arbitrary data. It works, but when I call the session object again somewhere else, it no longer contains the updated data.

const { session, updateSession } = useAuth()
await updateSession((session: Session) => {
    return { ...session, newData: 'something' };
});

and somewhere else, const { session } = useAuth() will give me the session without newData

@Hebilicious Hebilicious added bug Something isn't working needs reproduction labels Oct 30, 2023
@DarkLink13
Copy link

I had the same problem, I think it's because only the store is updated and after making any other actions the Auth session callback updates the session store from the cookies.

const session = useState("auth:session", () => null);
  const cookies = useState("auth:cookies", () => ({}));
  const status = useState("auth:session:status", () => "unauthenticated");
  const sessionToken = computed(() => cookies.value?.["next-auth.session-token"] ?? "");
  const user = computed(() => session.value?.user ?? null);
  watch(session, (newSession) => {
    if (newSession === null)
      return status.value = "unauthenticated";
    if (Object.keys(newSession).length)
      return status.value = "authenticated";
  });
  const updateSession = (u) => {
    session.value = typeof u === "function" ? produce(session.value, u) : u;
  };
  const removeSession = () => {
    cookies.value = null;
    updateSession(null);
  };

As a workaround I used removeSession, and signIn again in order to get updated data from provider

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