Last active
March 2, 2023 22:42
-
-
Save josefaidt/a694ca7dc0a56319ddfd4ebafac623a7 to your computer and use it in GitHub Desktop.
Sample "login" and "logout" page with Next.js and Amplify UI
This file contains hidden or 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
import { useEffect } from 'react' | |
import { useRouter } from 'next/router' | |
import { useAuthenticator, Authenticator } from '@aws-amplify/ui-react' | |
export default function LoginPage() { | |
const { user, route } = useAuthenticator((context) => [ | |
context.user, | |
context.route, | |
]) | |
const router = useRouter() | |
useEffect(() => { | |
if (user) { | |
router.push('/') | |
} | |
}, []) | |
useEffect(() => { | |
if (route === 'authenticated') { | |
router.push('/') | |
} | |
}, [route]) | |
return <Authenticator></Authenticator> | |
} |
This file contains hidden or 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
import { useEffect } from 'react' | |
import { useRouter } from 'next/router' | |
import { useAuthenticator, Authenticator } from '@aws-amplify/ui-react' | |
export default function LogoutPage() { | |
const { user, signOut } = useAuthenticator((context) => [ | |
context.user, | |
context.signOut, | |
]) | |
const router = useRouter() | |
async function handleSignOut() { | |
signOut() | |
router.push('/') | |
} | |
useEffect(() => { | |
if (!user) { | |
router.push('/') | |
} else { | |
handleSignOut() | |
} | |
}, []) | |
return <></> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment