Skip to content

Instantly share code, notes, and snippets.

@dferber90
Created April 16, 2020 15:02
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 dferber90/69f8177a80ffb17e8eb10c5790579e6b to your computer and use it in GitHub Desktop.
Save dferber90/69f8177a80ffb17e8eb10c5790579e6b to your computer and use it in GitHub Desktop.
aws-cognito-next
// 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