Skip to content

Instantly share code, notes, and snippets.

@josefaidt
Last active March 2, 2023 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefaidt/a694ca7dc0a56319ddfd4ebafac623a7 to your computer and use it in GitHub Desktop.
Save josefaidt/a694ca7dc0a56319ddfd4ebafac623a7 to your computer and use it in GitHub Desktop.
Sample "login" and "logout" page with Next.js and Amplify UI
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>
}
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