NextStorage causes Cognito to fail parse the user data
iwatakeshi opened this issue · comments
Hi, for context, I am calling Auth.currentAuthenticatedUser()
for each page to determine whether the user is signed in. However, the following error occurs:
[DEBUG] 28:27.908 AuthClass - getting user data failed SyntaxError: Unexpected token % in JSON at position 1
at JSON.parse (<anonymous>)
at CognitoUser.getUserData (CognitoUser.js:1125)
at Auth.js:1031
at CognitoUser.getSession (CognitoUser.js:1226)
at Auth.js:1020
At first I thought it was CognitoUser.js
causing the issue but with further investigation, CognitoUser
will get the userData
from the storage and then simply use JSON.parse()
to parse the userData
.
The input, userData
, looks something like this before CognitoUser
parses it:
"{%22UserAttributes%22:[omitted]%2C%22Username%22:%22[omitted]%22}"
Hopefully this helps.
Thank you
Would you be able to set up a minimal reproducible case using something like codesandbox? Or paste the result of Auth.currentAuthenticatedUser()
?
Posting your configuration here might help as well.
I'm not sure where the { "UserAttributes": "...", "Username": "..." }
portion of the response is within the CognitorUser object. I don't see either of those keys in the response from Auth.currentAuthenticatedUser()
on my end.
I do see these properties in the response, but with no parsing issues:
{
username: 'ryan', // Lowercase instead of pascal case
attributes: {
sub: '123',
email_verified: true,
email: 'me@email.com'
}
}
@ryanwalters I'll try to reproduce something for you when I get some time 😅. I can however show you the entire user object (with omissions):
{
"username": "[omitted]",
"pool": {
"userPoolId": "[omitted]",
"clientId": "[omitted]",
"client": {
"endpoint": "[omitted]",
"userAgent": "aws-amplify/0.1.x js"
},
"advancedSecurityDataCollectionFlag": true,
"storage": {
"CognitoIdentityServiceProvider.[omitted].LastAuthUser": "[omitted]",
"CognitoIdentityServiceProvider.[omitted].[omitted].clockDrift": "0",
"CognitoIdentityServiceProvider.[omitted].[omitted].refreshToken": "[omitted]",
"amplify-signin-with-hostedUI": "false",
"CognitoIdentityServiceProvider.[omitted].[omitted].accessToken": "[omitted]",
"CognitoIdentityServiceProvider.[omitted].[omitted].userData": "{\"UserAttributes\":[{\"Name\":\"sub\",\"Value\":\"[omitted]\"},{\"Name\":\"custom:last_name\",\"Value\":\"[omitted]\"},{\"Name\":\"email_verified\",\"Value\":\"true\"},{\"Name\":\"custom:first_name\",\"Value\":\"[omitted]\"},{\"Name\":\"email\",\"Value\":\"[omitted]\"}],\"Username\":\"[omitted]\"}",
"CognitoIdentityServiceProvider.[omitted].[omitted].idToken": "[omitted]",
"aws.cognito.identity-id.[omitted]:[omitted]": "[omitted]]",
"aws.cognito.identity-providers.[omitted]:[omitted]": "[omitted]"
}
},
"Session": null,
"client": {
"endpoint": "[omitted]",
"userAgent": "aws-amplify/0.1.x js"
},
"signInUserSession": {
"idToken": {
"jwtToken": "[omitted]",
"payload": {
"sub": "[omitted]",
"email_verified": true,
"iss": "[omitted]",
"cognito:username": "[omitted]",
"aud": "[omitted]",
"custom:last_name": "[omitted]",
"event_id": "[omitted]",
"custom:first_name": "[omitted]",
"token_use": "id",
"auth_time": 1581358544,
"exp": 1581564534,
"iat": 1581560934,
"email": "[omitted]"
}
},
"refreshToken": {
"token": "[omitted]"
},
"accessToken": {
"jwtToken": "[omitted]",
"payload": {
"sub": "[omitted]",
"event_id": "[omitted]",
"token_use": "access",
"scope": "aws.cognito.signin.user.admin",
"auth_time": 1581358544,
"iss": "[omitted]",
"exp": 1581564534,
"iat": 1581560934,
"jti": "[omitted]",
"client_id": "[omitted]",
"username": "[omitted]"
}
},
"clockDrift": 0
},
"authenticationFlowType": "USER_SRP_AUTH",
"storage": {
"CognitoIdentityServiceProvider.[omitted].LastAuthUser": "[omitted]",
"CognitoIdentityServiceProvider.[omitted].[omitted].clockDrift": "0",
"CognitoIdentityServiceProvider.[omitted].[omitted].refreshToken": "[omitted]",
"amplify-signin-with-hostedUI": "false",
"CognitoIdentityServiceProvider.[omitted].[omitted].accessToken": "[omitted]",
"CognitoIdentityServiceProvider.[omitted].[omitted].userData": "{\"UserAttributes\":[{\"Name\":\"sub\",\"Value\":\"[omitted]\"},{\"Name\":\"custom:last_name\",\"Value\":\"[omitted]\"},{\"Name\":\"email_verified\",\"Value\":\"true\"},{\"Name\":\"custom:first_name\",\"Value\":\"[omitted]\"},{\"Name\":\"email\",\"Value\":\"[omitted]\"}],\"Username\":\"[omitted]\"}",
"CognitoIdentityServiceProvider.[omitted].[omitted].idToken": "[omitted]",
"aws.cognito.identity-id.[omitted]:[omitted]": "[omitted]",
"aws.cognito.identity-providers.[omitted]:[omitted]": "cognito-idp.[omitted].amazonaws.com/[omitted]"
},
"keyPrefix": "CognitoIdentityServiceProvider.[omitted]",
"userDataKey": "CognitoIdentityServiceProvider.[omitted].[omitted].userData",
"attributes": {
"sub": "[omitted]",
"custom:last_name": "[omitted]",
"email_verified": true,
"custom:first_name": "[omitted]",
"email": "[omitted]"
},
"preferredMFA": "NOMFA"
}
Also, this output is when I don't use the storage.
Great, looking forward to a repro!
In the meantime, what version of @aws-amplify/auth
are you using? Can you post your Auth.configure
setup?
It seems like something is trying to parse the CognitoIdentityServiceProvider.[poolId].[username].userData
cookie and failing.
What does your CognitoIdentityServiceProvider.[poolId].[username].userData
cookie value look like?
For reference, mine looks something like:
%7B%2522UserAttributes%2522%3A%5B%7B%2522Name%2522%3A%2522sub%2522%252C%2522Value%2522%3A%2522e8cea59a-ce83-4387-8a32-34284340792c%2522%7D%252C%7B%2522Name%2522%3A%2522email_verified%2522%252C%2522Value%2522%3A%2522true%2522%7D%252C%7B%2522Name%2522%3A%2522email%2522%252C%2522Value%2522%3A%2522me%40email.com%2522%7D%5D%252C%2522Username%2522%3A%2522ryan%2522%7D
Sure. The version is 2.2.4
and the Auth.configure is:
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Auth } from 'aws-amplify'
import NextStorage from 'amplify-auth-next-storage'
import awsconfig from '../aws-exports'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function configureAuth(context?: any) {
Auth.configure({
...awsconfig
// storage: new NextStorage(context, {
// domain: process.env.NODE_ENV === 'production' ? '[omitted]' : 'localhost',
// expires: 365,
// path: '/',
// secure: process.env.NODE_ENV === 'production'
// })
})
}
For the cookie, I'll get back to you on that.
Update:
Sorry, I needed to create a test user. Here's the cookie:
%7B%2522UserAttributes%2522%3A%5B%7B%2522Name%2522%3A%2522sub%2522%252C%2522Value%2522%3A%252253e3ae1f-20bb-4138-8a8f-81e31976a47d%2522%7D%252C%7B%2522Name%2522%3A%2522custom%3Alast_name%2522%252C%2522Value%2522%3A%2522Doe%2522%7D%252C%7B%2522Name%2522%3A%2522email_verified%2522%252C%2522Value%2522%3A%2522true%2522%7D%252C%7B%2522Name%2522%3A%2522custom%3Afirst_name%2522%252C%2522Value%2522%3A%2522John%2522%7D%252C%7B%2522Name%2522%3A%2522email%2522%252C%2522Value%2522%3A%2522test.user%40blah.co%2522%7D%5D%252C%2522Username%2522%3A%252253e3ae1f-20bb-4138-8a8f-81e31976a47d%2522%7D
Can you try again with amplify-auth-next-storage@1.1.1
? I wasn't able to reproduce your issue, but I spotted a possible culprit.
@ryanwalters Nice! Seems to be fixed! It's enabled and works on refresh. Thanks!
Awesome! I'll go ahead and close this out. Feel free to reopen if there are still issues.