diff --git a/libs/shared/lib/data-access/security/useAuthentication.tsx b/libs/shared/lib/data-access/security/useAuthentication.tsx index fcb507b868c3effc43464d9049899e9ec33be6ce..a14824d963a765f2a79b206cc893a2e241ef6018 100644 --- a/libs/shared/lib/data-access/security/useAuthentication.tsx +++ b/libs/shared/lib/data-access/security/useAuthentication.tsx @@ -1,5 +1,6 @@ import { useAppDispatch, useAuthCache } from '../store'; import { authenticated, changeRoom, UserAuthenticationHeader } from '../store/authSlice'; +import { addInfo, addError } from '../store/configSlice'; const domain = import.meta.env.BACKEND_URL; const userURI = import.meta.env.BACKEND_USER; @@ -16,15 +17,15 @@ export const useAuthentication = () => { const handleError = (err: any) => { console.error(err); + dispatch(addError('Failed to copy: ' + (err.message || 'Unknown error occurred'))); }; const login = () => { fetch(`${domain}${userURI}/headers`, fetchSettings) - .then((res) => { + .then((res) => res .json() .then((res: UserAuthenticationHeader) => { - console.log(res, 'headers'); dispatch( authenticated({ username: res.username, @@ -36,24 +37,47 @@ export const useAuthentication = () => { }), ); }) - .catch(handleError); - }) - .catch(handleError); - }; - - const newShareRoom = () => { - fetch(`${domain}${userURI}/share`, { ...fetchSettings, method: 'POST' }) - .then((res) => - res - .json() - .then((res: { Roomid: string; Sessionid: string }) => { - // TODO: send to backend current state and make redux accordingly - dispatch(changeRoom(res.Roomid)); - }) .catch(handleError), ) .catch(handleError); }; + const copyToClipboard = async (text: string) => { + try { + await navigator.clipboard.writeText(text); + return true; + } catch (err) { + console.error('Failed to copy:', err); + return false; + } + }; + + const newShareRoom = async () => { + try { + // TODO: Implement share room functionality when backend is ready + // fetch(`${domain}${userURI}/share`, { ...fetchSettings, method: 'POST' }) + // .then((res) => + // res + // .json() + // .then((res: { Roomid: string; Sessionid: string }) => { + // dispatch(changeRoom(res.Roomid)); + // }) + // .catch(handleError), + // ) + // .catch(handleError); + + const shareUrl = window.location.href; + const copied = await copyToClipboard(shareUrl); + + if (copied) { + dispatch(addInfo('Link copied to clipboard!')); + } else { + throw new Error('Failed to copy to clipboard'); + } + } catch (error: any) { + handleError(error); + } + }; + return { login, newShareRoom }; -}; +}; \ No newline at end of file