2023-12-31 05:41:55 -05:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
import axios from "axios";
|
|
|
|
import * as React from 'react';
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
const UserProfile = () => {
|
2024-01-02 02:26:20 -05:00
|
|
|
const { data: session, status } = useSession();
|
|
|
|
const [user, setUser] = useState<{ id: string, username: string }>()
|
|
|
|
|
|
|
|
let previousUsername = ""
|
2023-12-31 05:41:55 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (status !== "authenticated" || previousUsername == session.user?.name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-02 02:26:20 -05:00
|
|
|
previousUsername = session.user?.name || ""
|
2023-12-31 05:41:55 -05:00
|
|
|
if (session.user) {
|
|
|
|
const fetchData = async () => {
|
2024-01-04 03:56:24 -05:00
|
|
|
if (user) return
|
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
let userData = (await axios.get("/api/account")).data
|
2023-12-31 05:41:55 -05:00
|
|
|
setUser(userData)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchData().catch(console.error)
|
|
|
|
}
|
|
|
|
}, [session])
|
|
|
|
|
|
|
|
return (
|
2024-01-02 02:26:20 -05:00
|
|
|
<div className={cn("px-10 py-6 rounded-md bg-blue-300 overflow-hidden wrap", user == null && "hidden")}>
|
2024-01-04 16:57:32 -05:00
|
|
|
<p className="text-xs text-gray-200">Logged in as:</p>
|
2023-12-31 05:41:55 -05:00
|
|
|
<p>{user?.username}</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default UserProfile;
|