-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
32,205 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Utility for user blocking mechanism. | ||
* See: https://g0v.hackmd.io/rf0A7MRfTOC613QZmFehQA#User-Blocking | ||
*/ | ||
|
||
import React, { useContext } from 'react'; | ||
import Cookies from 'js-cookie'; | ||
|
||
const COOKIE_KEY = 'isUserBlocked'; | ||
const COOKIE_TRUE_VALUE = '1'; | ||
const isUserBlockedContext = React.createContext(false); | ||
|
||
/** | ||
* The context provider for decendents to read if the current browser belongs to a blocked user. | ||
* It reads from an independent cookie so that it remains true even after the blocked user is logged | ||
* out. | ||
* | ||
* @param {Object} props | ||
* @param {Object?} props.serverSideCookie - the cookie from server side, set by _document.js | ||
* @returns {React.Provider<boolean>} | ||
*/ | ||
export function IsUserBlockedProvider({ serverSideCookie, children }) { | ||
const { Provider } = isUserBlockedContext; | ||
const cookieValue = serverSideCookie | ||
? serverSideCookie[COOKIE_KEY] | ||
: Cookies.get(COOKIE_KEY); | ||
|
||
return ( | ||
<Provider value={cookieValue === COOKIE_TRUE_VALUE}>{children}</Provider> | ||
); | ||
} | ||
|
||
/** | ||
* Reads from IsUserBlockedProvider | ||
* @returns {boolean} | ||
*/ | ||
export function useIsUserBlocked() { | ||
return useContext(isUserBlockedContext); | ||
} | ||
|
||
/** | ||
* Set cookie when we know this browser is blocked, if not set previously. | ||
* | ||
* After we set cookie, reload the page to update the server-rendered UI. | ||
*/ | ||
export function blockUserBrowserAndRefreshIfNeeded() { | ||
// No-op if cookie is already set | ||
if (Cookies.get(COOKIE_KEY) === COOKIE_TRUE_VALUE) return; | ||
|
||
Cookies.set(COOKIE_KEY, COOKIE_TRUE_VALUE); | ||
location.reload(); | ||
} |
Oops, something went wrong.