forked from Keyri-Co/session-lock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyTokenButton.js
More file actions
41 lines (35 loc) · 987 Bytes
/
Copy pathCopyTokenButton.js
File metadata and controls
41 lines (35 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useState, useEffect } from 'react';
function CopyTokenButton() {
const [buttonText, setButtonText] = useState('Copy JWT to clipboard');
useEffect(() => {
let timeoutId;
if (buttonText === 'Copied!') {
timeoutId = setTimeout(() => {
setButtonText('Copy JWT to clipboard');
}, 3000);
}
return () => {
clearTimeout(timeoutId);
};
}, [buttonText]);
const copyTokenToClipboard = async () => {
try {
const token = localStorage.getItem('token');
if (token) {
await navigator.clipboard.writeText(token);
setButtonText('Copied!');
}
} catch (error) {
console.error('Failed to copy token:', error);
}
};
return (
<button
onClick={copyTokenToClipboard}
className='bg-[#5A8958] hover:bg-[#61955F] text-white font-bold py-2 px-4 rounded-lg focus:outline-none focus:shadow-outline'
>
{buttonText}
</button>
);
}
export default CopyTokenButton;