๐Ÿค–์ •๋ณด๋ณด์•ˆ/๐Ÿ’ 1๋ถ„์ง€์‹

[Auth.js] ๋กœ๊ทธ์ธ ์งํ›„ useSession undefined ํ˜„์ƒ ํ•ด๊ฒฐ

TwoIceFish 2024. 6. 28. 12:36

 

 

๋ฌธ์ œ์ 

SessionProvider๋กœ ๊ฐ์‹ธ๊ณ  useSession์„ ์‚ฌ์šฉํ•  ์‹œ ์ตœ์ดˆ ๋กœ๊ทธ์ธ ์ƒ์—์„œ๋Š” ๋™์ž‘์œผ๋ฉฐ Hard Reload ์‹œ ๋™์ž‘ํ•˜๋Š” ํ˜„์ƒ์ด ์žˆ์—ˆ๋‹ค.

 

ํ•ด๊ฒฐ์ „

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ์ ์šฉ ์ „

ํ•ด๊ฒฐํ›„

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ์ ์šฉ ํ›„

 

๋ฒ„์ „์ •๋ณด

 "next": "14.2.4",
 "next-auth": "^5.0.0-beta.19",

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

useSession๋Œ€์‹  ์•„๋ž˜์˜ hook์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ธ์ถœํ•œ๋‹ค.๋งค์šฐ ๊ฐ์‚ฌํ•˜๊ฒŒ๋„ ์œ ์ €๋ถ„(bocarw121)๊ป˜์„œ ๋งŒ๋“ค์–ด ์ฃผ์…จ๋‹ค.

์•„๋ž˜ Github Issue ์ฐธ๊ณ  - ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๊ณ ํ†ต๋ฐ›๊ณ  ์žˆ๋‹ค. ๊ทœ๋ชจ๊ฐ€ ์ปค์ง์— ๋”ฐ๋ฅธ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋ถˆ์•ˆ์ •ํ•œ ๋Š๋‚Œ์ด๋‹ค.

 

use-current-session.ts

// This hook doesn't rely on the session provider
export const useCurrentSession = () => {
  const [session, setSession] = useState<Session | null>(null);
  // Changed the default status to loading
  const [status, setStatus] = useState<string>("loading");
  const pathName = usePathname();

  const retrieveSession = useCallback(async () => {
    try {
      const sessionData = await getSession();
      if (sessionData) {
        setSession(sessionData);
        setStatus("authenticated");
        return;
      }

      setStatus("unauthenticated");
    } catch (error) {
      setStatus("unauthenticated");
      setSession(null);
    }
  }, []);

  useEffect(() => {
    // We only want to retrieve the session when there is no session
    if (!session) {
      retrieveSession();
    }

    // use the pathname to force a re-render when the user navigates to a new page
  }, [retrieveSession, session, pathName]);

  return { session, status };
};

 

user-info.tsx

"use client";

import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";

import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { FaUser } from "react-icons/fa";
import { useCurrentUser } from "@/hooks/use-current-user";
import { RiFullscreenExitFill } from "react-icons/ri";
import { logout } from "@/actions/logout";
import { useCurrentSession } from "@/hooks/use-current-session";

export const UserButton = () => {
  const { session, status } = useCurrentSession();
  // const session = useCurrentUser();
  return (
    <DropdownMenu>
      <DropdownMenuTrigger>
        <Avatar className={"bg-white ring-4 ring-green-400"}>
          <AvatarImage src={session?.image || ""} />
          <FaUser />
          <AvatarFallback>{session?.username?.slice(0, 2)}</AvatarFallback>
        </Avatar>
      </DropdownMenuTrigger>
      <DropdownMenuContent className={"w-48"}>
        <DropdownMenuItem onClick={() => logout}>
          <RiFullscreenExitFill />
          Log out
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
};

์ฐธ๊ณ ๋ฌธ์„œ

https://github.com/bocarw121

 

bocarw121 - Overview

bocarw121 has 59 repositories available. Follow their code on GitHub.

github.com

 

https://github.com/nextauthjs/next-auth/issues/9504

 

useSession only getting the session after manually reloading the page · Issue #9504 · nextauthjs/next-auth

Environment System: OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish) CPU: (6) x64 Common KVM processor Memory: 48.93 GB / 62.79 GB Container: Yes Shell: 5.1.16 - /bin/bash Binaries: ...

github.com

 

Authjs ํ•™์Šต์— ๊ฐ•์ถ”!

https://www.youtube.com/watch?v=1MTyCvS05V4&t=28099s