Created
February 28, 2021 20:55
-
-
Save SahanAmarsha/f5320b211d366d2cf4b65bffa35ac974 to your computer and use it in GitHub Desktop.
getServerSideProps.js
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 { getUser } from "../src/graphql/queries"; | |
import {withSSRContext} from "aws-amplify"; | |
export async function getServerSideProps({ req, res }) { | |
const { Auth, API } = withSSRContext({ req }); | |
try { | |
const user = await Auth.currentAuthenticatedUser(); | |
const response = await API.graphql({ | |
query: getUser, | |
variables: { id: user.attributes.sub }, | |
}); | |
if (response.data.getUser) { | |
return { | |
props: { | |
mode: "EDIT", | |
user: response.data.getUser, | |
error: false, | |
}, | |
}; | |
} else { | |
return { | |
props: { | |
mode: "ADD", | |
error: false, | |
}, | |
}; | |
} | |
} catch (err) { | |
console.log(err); | |
return { | |
props: { | |
error: true, | |
}, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment