Created
April 16, 2020 15:02
-
-
Save dferber90/69f8177a80ffb17e8eb10c5790579e6b to your computer and use it in GitHub Desktop.
aws-cognito-next
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
// pages/index.tsx | |
import React from "react"; | |
import { GetServerSideProps } from "next"; | |
import { | |
AuthTokens, | |
useAuth, | |
useAuthFunctions, | |
getServerSideAuth, | |
} from "../auth"; | |
const Home = (props: { initialAuth: AuthTokens }) => { | |
const auth = useAuth(props.initialAuth); | |
const { login, logout } = useAuthFunctions(); | |
return ( | |
<React.Fragment> | |
{auth ? ( | |
<button type="button" onClick={() => logout()}> | |
sign out | |
</button> | |
) : ( | |
<React.Fragment> | |
<button type="button" onClick={() => login()}> | |
sign in | |
</button> | |
</React.Fragment> | |
)} | |
</React.Fragment> | |
); | |
}; | |
export const getServerSideProps: GetServerSideProps<{ | |
initialAuth: AuthTokens; | |
}> = async (context) => { | |
const initialAuth = getServerSideAuth(context.req); | |
return { props: { initialAuth } }; | |
}; | |
export default Home; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment