Skip to content

Instantly share code, notes, and snippets.

@SahanAmarsha
Created February 28, 2021 20:55
Show Gist options
  • Save SahanAmarsha/f5320b211d366d2cf4b65bffa35ac974 to your computer and use it in GitHub Desktop.
Save SahanAmarsha/f5320b211d366d2cf4b65bffa35ac974 to your computer and use it in GitHub Desktop.
getServerSideProps.js
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