Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use AUTH_TYPE.AMAZON_COGNITO_USER_POOLS in config - window is not defined #1

Closed
dihmeetree opened this issue May 30, 2018 · 12 comments

Comments

@dihmeetree
Copy link
Contributor

dihmeetree commented May 30, 2018

I know there was this issue awhile back.. but I can't use the following in my config..

import { Auth } from 'aws-amplify'
import { AUTH_TYPE } from 'aws-appsync/lib/link/auth-link'

export default {
  url: 'https://xxxxxxxxxxxxxxxxxxxxxxxxx-api.us-east-1.amazonaws.com/graphql',
  region: 'us-east-1',
  auth: {
    // Amazon Cognito user pools using AWS Amplify
    type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
    jwtToken: async () =>
      (await Auth.currentSession()).getIdToken().getJwtToken(),
  },
}

It gives the following error: UnhandledPromiseRejectionWarning: ReferenceError: window is not defined

Error

Look's like the error has been addressed here: aws-amplify/amplify-js#882 Is there nothing we can do until this bug is fixed?

@dihmeetree dihmeetree changed the title Can't use the Cognito User Pools auth type - window is not defined Can't use AUTH_TYPE.AMAZON_COGNITO_USER_POOLS in config - window is not defined May 30, 2018
@dihmeetree
Copy link
Contributor Author

Did a little research and it look's like some updates to aws-amplify haven't been pushed yet to a new version.

Did yarn upgrade aws-amplify@0.4.2-unstable.21 and it seemed to fix the window error.

@dabit3
Copy link
Owner

dabit3 commented Jun 3, 2018

@lolcoolkat ver interesting, thanks for pointing this out. I'll check into this and see what we can do :)

@dihmeetree
Copy link
Contributor Author

dihmeetree commented Jun 5, 2018

Btw @dabit3 doing the following doesn't work on the server with SSR...

(Auth.currentSession()).getIdToken().getJwtToken()

So instead of using that function.. I figured i'll just get the accessToken I need manually and pass that directly to the AWSAppSyncClient..

First thing that needs to change is in the AWS Amplify config adding the cookieStorage settings like so:

cookieStorage: {
   domain: 'localhost', //or whatever domain
   path: '/',
   expires: 365,
    secure: false,
 }

We need to extract the accessToken from the cookies..and pass that to the AppSync client. I'm using the following function to extract it out..

The actual thing we are trying to extract looks like this:
Error

export function getValueFromKey(req, string) {
  const cookies = parseCookies(req)
  const key = Object.keys(cookies).filter(k => k.includes(string))
  return key.length ? cookies[key] : ''
}

and calling it like so when initializing Apollo:

 const apollo = initApollo(
        {},
        {
          ...config,
          getToken: () => getValueFromKey(req, 'accessToken'),
        },
      )

This solution seems to be working... but hopefully a solution by aws amplify for supporting SSR would be nice in the future.

I made an issue thread here awhile back before I explored this solution: aws-amplify/amplify-js#951

@dabit3
Copy link
Owner

dabit3 commented Jun 5, 2018

Ok @lolcoolkat I just checked on this and it seems to be an issue with the current implementation of AWS Amplify. This issue is already being tracked as a future enhancement and will be fixed in Amplify in the near future.

I'll leave this open to others looking to use this and possibly use your workaround, and will update as soon as it's fixed in the main AWS Amplify SDK.

Thanks for pointing this out!

@jsierles
Copy link

Did this get fixed?

@masato-io
Copy link

wondering if this is fixed as well.

@Enalmada
Copy link

@dabit3 Is there a place where we can see tracking on this in Amplify?

@sakhmedbayev
Copy link

@dabit3 any news on this front? Please let us know!

@sakhmedbayev
Copy link

We need to extract the accessToken from the cookies..and pass that to the AppSync client. I'm using the following function to extract it out..

@lolcoolkat thanks for sharing your solution. Can you please tell how you placed accessToken to the cookieStorage?

@sakhmedbayev
Copy link

@lolcoolkat do you also tweaking server.js file to make your solution work? Something like showing here aws-amplify/amplify-js#493 (comment) ?

@dabit3
Copy link
Owner

dabit3 commented Jan 16, 2019

To the best of my knowledge this is on the roadmap but there is no specific date yet set for it. For better visibility, consider posting the same issue in the Amplify JS repo, from there the team should be able to give more info than I have.

https://github.com/aws-amplify/amplify-js/issues

@ajhool
Copy link

ajhool commented May 1, 2019

These are some related issues in amplify-js

aws-amplify/amplify-js#3201

aws-amplify/amplify-js#2054

aws-amplify/amplify-js#3015

I consider the lack of support for SSR in Amplify to be a terrible oversight. Particularly because SSR works for most of the components, so it lures you into a false sense of security -- but if Auth doesn't work properly, then the entire framework is useless. The worst part is that the error is silent, so I don't even know where to begin debugging the issue or offering a patch. If this is an issue for you, please be vocal on the amplify github issues section because they are responsive to feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants