Spaces:
Sleeping
Sleeping
missing button
Browse files
patches/src/components/buttons/OAuthLogin.tsx
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState, useEffect } from 'react';
|
| 2 |
+
import Button from './Button';
|
| 3 |
+
import hf from '../../../assets/hf.svg';
|
| 4 |
+
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from '@huggingface/hub';
|
| 5 |
+
|
| 6 |
+
const OAuthLogin = () => {
|
| 7 |
+
const [isSignedIn, setIsSignedIn] = useState(false);
|
| 8 |
+
|
| 9 |
+
useEffect(() => {
|
| 10 |
+
const checkAuthStatus = async () => {
|
| 11 |
+
let oauthResult = localStorage.getItem('oauth');
|
| 12 |
+
if (oauthResult) {
|
| 13 |
+
try {
|
| 14 |
+
oauthResult = JSON.parse(oauthResult);
|
| 15 |
+
} catch {
|
| 16 |
+
oauthResult = null;
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
if (!oauthResult) {
|
| 21 |
+
oauthResult = await oauthHandleRedirectIfPresent();
|
| 22 |
+
if (oauthResult) {
|
| 23 |
+
localStorage.setItem('oauth', JSON.stringify(oauthResult));
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
setIsSignedIn(!!oauthResult);
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
checkAuthStatus();
|
| 31 |
+
}, []);
|
| 32 |
+
|
| 33 |
+
const handleSignIn = async () => {
|
| 34 |
+
let clientId = import.meta.env.VITE_OAUTH_CLIENT_ID;
|
| 35 |
+
window.location.href = await oauthLoginUrl({ clientId });
|
| 36 |
+
};
|
| 37 |
+
|
| 38 |
+
const handleSignOut = () => {
|
| 39 |
+
localStorage.removeItem('oauth');
|
| 40 |
+
window.location.href = window.location.href.replace(/\?.*$/, '');
|
| 41 |
+
setIsSignedIn(false);
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
return (
|
| 45 |
+
<div>
|
| 46 |
+
{isSignedIn ? (
|
| 47 |
+
<div>
|
| 48 |
+
<Button id="signout" imgUrl={hf} onClick={handleSignOut}>Sign out</Button>
|
| 49 |
+
</div>
|
| 50 |
+
) : (
|
| 51 |
+
<Button id="signin" imgUrl={hf} onClick={handleSignIn}>
|
| 52 |
+
Sign in with Hugging Face
|
| 53 |
+
</Button>
|
| 54 |
+
)}
|
| 55 |
+
</div>
|
| 56 |
+
);
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
export default OAuthLogin;
|