67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import { useMsal } from '@azure/msal-react';
|
|
import { useState } from 'react';
|
|
import { loginRequest } from '../config/authConfig';
|
|
import { PersonIcon, ExitIcon } from '@radix-ui/react-icons';
|
|
|
|
export function AuthButton() {
|
|
const { instance, accounts } = useMsal();
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const handleLogin = async () => {
|
|
setIsLoading(true);
|
|
try {
|
|
await instance.loginPopup(loginRequest);
|
|
} catch (error) {
|
|
console.error('Login failed:', error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleLogout = async () => {
|
|
setIsLoading(true);
|
|
try {
|
|
await instance.logoutPopup({
|
|
postLogoutRedirectUri: '/',
|
|
mainWindowRedirectUri: '/',
|
|
});
|
|
} catch (error) {
|
|
console.error('Logout failed:', error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const isAuthenticated = accounts.length > 0;
|
|
const userName = accounts[0]?.username || accounts[0]?.name || 'User';
|
|
|
|
if (isAuthenticated) {
|
|
return (
|
|
<div className="flex items-center gap-3">
|
|
<div className="flex items-center gap-2 text-sm text-[#e4e4e4]">
|
|
<PersonIcon className="h-4 w-4" />
|
|
<span className="hidden sm:inline">{userName}</span>
|
|
</div>
|
|
<button
|
|
onClick={handleLogout}
|
|
disabled={isLoading}
|
|
className="flex items-center gap-2 px-3 py-1.5 text-sm text-[#9d9d9d] hover:text-[#e4e4e4] hover:bg-[#2d2d30] rounded transition-colors disabled:opacity-50"
|
|
>
|
|
<ExitIcon className="h-4 w-4" />
|
|
<span className="hidden sm:inline">로그아웃</span>
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<button
|
|
onClick={handleLogin}
|
|
disabled={isLoading}
|
|
className="px-4 py-1.5 bg-[#0078d4] hover:bg-[#106ebe] text-white text-sm rounded transition-colors disabled:opacity-50"
|
|
>
|
|
{isLoading ? '로딩...' : '로그인'}
|
|
</button>
|
|
);
|
|
}
|