-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
useCrossDomainStorage
hook and integrate it into authentication…
… components * **New Hook**: Create `useCrossDomainStorage` to handle cross-domain storage using `localStorage` and `postMessage` - Implement `set` function to store data in `localStorage` and notify other domains - Implement `get` function to retrieve data from `localStorage` - Implement `handlePostMessage` function to listen for `postMessage` events and update `localStorage` * **AccountSwitcherModal**: Integrate `useCrossDomainStorage` hook - Import and use `get` and `set` functions - Update `fetchUser` function to use `get` - Update `handleBackdropClick` function to use `set` * **SignInButton**: Integrate `useCrossDomainStorage` hook - Import and use `set` function - Update `onClick` function to use `set` * **useOxySession**: Integrate `useCrossDomainStorage` hook - Import and use `get` and `set` functions - Update `fetchSessionData` function to use `get` and `set`
- Loading branch information
Showing
4 changed files
with
45 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect } from "react"; | ||
|
||
const useCrossDomainStorage = () => { | ||
const set = (key: string, value: string) => { | ||
localStorage.setItem(key, value); | ||
window.postMessage({ key, value }, "*"); | ||
}; | ||
|
||
const get = (key: string) => { | ||
return localStorage.getItem(key); | ||
}; | ||
|
||
const handlePostMessage = (event: MessageEvent) => { | ||
if (event.origin !== window.location.origin) { | ||
const { key, value } = event.data; | ||
localStorage.setItem(key, value); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener("message", handlePostMessage); | ||
return () => { | ||
window.removeEventListener("message", handlePostMessage); | ||
}; | ||
}, []); | ||
|
||
return { set, get }; | ||
}; | ||
|
||
export default useCrossDomainStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters