Skip to content

Commit

Permalink
fix: handles authentication through header
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshnirmalya committed Aug 25, 2020
1 parent 5e06774 commit 543f3e3
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ ENV HASURA_GRAPHQL_CLI_ENVIRONMENT=default
# https://github.com/hasura/graphql-engine/issues/5172#issuecomment-653774367
ENV HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR=DATABASE_URL

ENV HASURA_GRAPHQL_JWT_SECRET=HASURA_GRAPHQL_JWT_SECRET

ENV HASURA_GRAPHQL_ADMIN_SECRET=HASURA_GRAPHQL_ADMIN_SECRET

CMD graphql-engine \
--database-url $DATABASE_URL \
serve \
Expand Down
2 changes: 2 additions & 0 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ services:
- db
environment:
DATABASE_URL: postgres://postgres:@db:5432/postgres
HASURA_GRAPHQL_JWT_SECRET: '{"type":"RS256", "key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0U3NR0eyMehHBlx6DK5s\nJ+Pys9dTWf558kpKVeQvL2oIZEY3LvS3/jdrIm/dU4WrIGPki1r/AWXQAyBZ2FKn\nZROcUWN0IqdmxrC5zTDymuscqhKXqxjSwrwOVWHc+zWWmXCQGmDdoCokXd9ZW66n\nA0BN66MdMC2+d5GrZdKUF305dpzTUdzDE12/XwOEUalCM0703eGu8zFwutLYc3+v\nf2CFOQ1z+rvDQD4N2aZABKTxZRtEkMHljnoyKlF9rljNzT/5N8YQE7qn4pBh6CMa\n1zcSilk9nhgl55n/Kjn2xMieWdIalaOEKw1LqqIjiT1ESkAKfPaIoSSnmTaYy78g\nbwIDAQAB\n-----END PUBLIC KEY-----\n"}'
HASURA_GRAPHQL_ADMIN_SECRET: "secret"
PORT: 8080
ports:
- "8080:8080"
Expand Down
4 changes: 3 additions & 1 deletion backend/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
- updated_at
- author_id
- id
filter: {}
filter:
author_id:
_eq: X-Hasura-User-Id
- table:
schema: public
name: sessions
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/layout/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Container: FC = ({ children }) => {
fontSize="sm"
bg={bgColor[colorMode]}
>
<Box maxW="6xl" mx="auto">
<Box maxW="xl" mx="auto">
{children}
</Box>
</Box>
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const Navbar: NextComponentType = () => {
return (
<Box bg={bgColor[colorMode]}>
<Box p={4} color={color[colorMode]} shadow="lg" pos="relative">
<Box maxW="6xl" mx="auto" w="full">
<Box maxW="xl" mx="auto" w="full">
<Stack
isInline
spacing={4}
Expand Down
7 changes: 4 additions & 3 deletions frontend/lib/with-graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ const WithGraphQL = ({
session: session;
children: ReactNode;
}) => {
const userIdInString = session.id.toString();
const userIdInString =
"Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjUwMzFjMWNiLTZiNDItNDM3MS04NjYyLTA2MTNjOGIxM2Y2ZCIsIm5hbWUiOiJOaXJtYWx5YSBHaG9zaCIsImVtYWlsIjoibmlybWFseWEuZ2hvc2hAYW90LmVkdS5pbiIsInBpY3R1cmUiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS0vQU9oMTRHakVmTGVJMWU5TVUwN0oxX0NQY0lWZTB1M2dUZFBiTDMwbGx0VHAiLCJodHRwczovL2hhc3VyYS5pby9qd3QvY2xhaW1zIjp7IngtaGFzdXJhLWFsbG93ZWQtcm9sZXMiOlsiYWRtaW4iLCJ1c2VyIl0sIngtaGFzdXJhLWRlZmF1bHQtcm9sZSI6InVzZXIiLCJ4LWhhc3VyYS1yb2xlIjoidXNlciIsIngtaGFzdXJhLXVzZXItaWQiOiI1MDMxYzFjYi02YjQyLTQzNzEtODY2Mi0wNjEzYzhiMTNmNmQifSwiaWF0IjoxNTk4MzQzMjAzLjE3MiwiZXhwIjoxNTk4NDI5NjAzLCJzdWIiOiI1MDMxYzFjYi02YjQyLTQzNzEtODY2Mi0wNjEzYzhiMTNmNmQifQ.v4U72YmlDDpYQ6J9bpTtdwHGI8mvRMIcvRl1SmlIPDQ22CXB4iUw_ENut0xLdnYyWCzV91QjQTZtkpxfoTEWs3kxsVvxeZLYMpvlnd61WVA0dVOOVbgtWHXpGVkHBlRITiw4TVGlDpbxFcaeM2dUV2kCF52BI45gxnPF40BCHKKe3MvhdQbYunQP_dCn65Zb6efKyjLri_BQw7hcBjSCrkAokWg8219KH3VfrNl-E6HVFADhq3P2aVUzSCoEKKpNvEyo81jUTpF-3avpffiJN-LDuy_nTdJmxMUoV4CjQbBfJdneFYVKKazKuS0XFzL2ZJ6ODZsK-p4IeGgiBhoy9A";

const subscriptionClient = new SubscriptionClient(
process.env.NEXT_PUBLIC_WS_URL || "ws://localhost:8080/v1/graphql",
{
reconnect: true,
connectionParams: {
headers: { "X-Hasura-User-Id": userIdInString },
headers: { Authorization: userIdInString },
},
},
ws
Expand All @@ -29,7 +30,7 @@ const WithGraphQL = ({
url: process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080/v1/graphql",
fetch,
fetchOptions: {
headers: { "X-Hasura-User-Id": userIdInString },
headers: { Authorization: userIdInString },
},
requestPolicy: "cache-and-network",
exchanges: [
Expand Down
1 change: 1 addition & 0 deletions frontend/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const options = {
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["admin", "user"],
"x-hasura-default-role": "user",
"x-hasura-role": "user",
"x-hasura-user-id": token.id,
},
iat: Date.now() / 1000,
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/feeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const FeedsPage: InferGetServerSidePropsType<typeof getServerSideProps> = ({
return (
<WithGraphQL session={session}>
<Head>
<title>My Account Page</title>
<title>Feeds Page</title>
</Head>
<Page />
</WithGraphQL>
Expand Down

0 comments on commit 543f3e3

Please sign in to comment.