TwitchClone / src / hooks / useUser.ts
useUser.ts
Raw
import { useEffect, useState } from "react";
import { useSession } from "next-auth/react";
import { User } from "../types/user";

export function useUser(): User | undefined {
  const { data: sessionData } = useSession();
  const [user, setUser] = useState<User | undefined>();

  useEffect(() => {
    if (sessionData?.user) {
      setUser({
        id: sessionData.user.id,
        username: sessionData.user.name || "",
        email: sessionData.user.email || "",
        // Add any other properties you need for the chat functionality
      });
    } else {
      setUser(undefined);
    }
  }, [sessionData]);

  return user;
}