hermes-web/components/navigation/userprofile.tsx

42 lines
1.1 KiB
TypeScript
Raw Normal View History

"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 = ""
useEffect(() => {
if (status !== "authenticated" || previousUsername == session.user?.name) {
return
}
2024-01-02 02:26:20 -05:00
previousUsername = session.user?.name || ""
if (session.user) {
const fetchData = async () => {
2024-01-02 02:26:20 -05:00
var userData = (await axios.get("/api/account")).data
setUser(userData)
2024-01-02 02:26:20 -05:00
console.log(userData)
}
fetchData().catch(console.error)
2024-01-02 02:26:20 -05:00
// TODO: check session if impersonation is in use.
}
}, [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")}>
<p className="text-xs text-gray-400">Logged in as:</p>
<p>{user?.username}</p>
</div>
);
}
export default UserProfile;