-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): supabase-demo. Fix redirects and auth-related issues
GitOrigin-RevId: d0b996abd80094e6fdf4a20729f7bb7e65342d23
- Loading branch information
1 parent
7ed45ef
commit 84cb609
Showing
5 changed files
with
46 additions
and
105 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
99 changes: 15 additions & 84 deletions
99
examples/supabase-demo/components/CodeComponents/LogicComponents.tsx
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 |
---|---|---|
@@ -1,102 +1,33 @@ | ||
import { createSupabaseClient } from "@/util/supabase/component"; | ||
import { usePlasmicCanvasContext } from "@plasmicapp/loader-nextjs"; | ||
import React from "react"; | ||
|
||
export interface RedirectIfProps { | ||
children?: any; | ||
className?: string; | ||
leftExpression?: string; | ||
operator?: any; | ||
redirectUrl?: string; | ||
rightExpression?: string; | ||
testCondition?: boolean; | ||
forcePreview?: boolean; | ||
condition?: any; | ||
onFalse?: () => void; | ||
} | ||
|
||
export function RedirectIf(props: RedirectIfProps) { | ||
const { | ||
children, | ||
className, | ||
leftExpression, | ||
operator, | ||
redirectUrl, | ||
rightExpression, | ||
testCondition, | ||
forcePreview, | ||
} = props; | ||
const supabase = createSupabaseClient(); | ||
|
||
const [loaded, setLoaded] = React.useState<boolean>(false); | ||
const [condition, setCondition] = React.useState<boolean>(false); | ||
const ref = React.createRef<HTMLAnchorElement>(); | ||
const { children, className, onFalse, condition } = props; | ||
const inEditor = usePlasmicCanvasContext(); | ||
|
||
// Reset the condition if expressions change | ||
React.useEffect(() => { | ||
setCondition(false); | ||
}, [leftExpression, rightExpression, operator, children]); | ||
|
||
// Give time for auth to complete | ||
setTimeout(() => { | ||
setLoaded(true); | ||
}, 500); | ||
|
||
// Check if signed out | ||
React.useEffect(() => { | ||
supabase.auth.onAuthStateChange((e) => { | ||
if (e === "SIGNED_OUT") setCondition(false); | ||
}); | ||
}, []); | ||
|
||
const shouldRedirect = React.useCallback( | ||
() => (inEditor && testCondition !== undefined ? testCondition : condition), | ||
[inEditor, testCondition, condition] | ||
); | ||
|
||
// Perform redirect | ||
React.useEffect(() => { | ||
if (shouldRedirect() && loaded && !inEditor) { | ||
ref.current?.click(); | ||
if (inEditor || !onFalse || condition) { | ||
return; | ||
} | ||
}, [loaded, condition, ref, inEditor, testCondition, shouldRedirect]); | ||
onFalse(); | ||
}, [condition, inEditor]); | ||
|
||
// Validation | ||
if (!leftExpression) { | ||
return <p>You need to set the leftExpression prop</p>; | ||
} else if (!operator) { | ||
return <p>You need to set the operator prop</p>; | ||
} else if (operator !== "FALSY" && operator !== "TRUTHY") { | ||
return <p>You need to set the rightExpression prop</p>; | ||
} else if (!redirectUrl) { | ||
return <p>You need to set the redirectUrl prop</p>; | ||
if (typeof condition === "undefined") { | ||
return ( | ||
<p> | ||
Condition needs to be a boolean prop. Try to add exclamation marks to | ||
the value. | ||
</p> | ||
); | ||
} | ||
|
||
// Set the condition | ||
const leftVal = leftExpression; | ||
if (!condition) { | ||
if (operator === "FALSY" && !leftVal) { | ||
setCondition(true); | ||
} else if (operator === "TRUTHY") { | ||
if (!!leftVal) { | ||
setCondition(true); | ||
} | ||
const rightVal = rightExpression ?? ""; | ||
if (leftVal === rightVal) { | ||
setCondition(true); | ||
} | ||
} | ||
} | ||
|
||
if (!loaded) { | ||
return null; | ||
} | ||
|
||
const showChildren = !shouldRedirect() || (inEditor && forcePreview); | ||
|
||
return ( | ||
<div className={className}> | ||
{showChildren && children} | ||
<a href={redirectUrl} hidden={true} ref={ref} /> | ||
</div> | ||
); | ||
return <div className={className}>{children}</div>; | ||
} |
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