Amplify Javascript Client for Auth is not able to access auth session

0

I am using the code from the aws examples https://docs.amplify.aws/lib/auth/manageusers/q/platform/js/#retrieve-current-authenticated-user and after signing up, signing in, I am trying to update the email. once I call the change email address, I am always getting an error:

Uncaught (in promise) TypeError: user.getSession is not a function which seems to come deep within the aws amplify auth module. Trying to find out what is wrong? If you follow the code below this is the order in which it is used.

async function signUp(username, password) { try { const { user } = await Auth.signUp({ username, password, attributes: { email: username//, // optional //phone_number, // optional - E.164 number convention // other custom attributes }, autoSignIn: { // optional - enables auto sign in after user is confirmed enabled: true, } }); console.log(user); console.log(currentAuthenticatedUserSession()); } catch (error) { console.log('error signing up:', error); } }

async function confirmSignUp(username, code) { try { await Auth.confirmSignUp(username, code); } catch (error) { console.log('error confirming sign up', error); } }

async function signIn(username, password) { try { const user = await Auth.signIn(username, password); user.Session=user.signInUserSession; console.log(user); } catch (error) { console.log('error signing in', error); } }

async function changeEmail(newEmail){ // Send confirmation code to user's old email // Calling this line here seems to not be passing the session as it errors out with the error posted in the beginning above. Auth.updateUserAttributes(currentAuthenticatedUserSession(), {'email':newEmail}) .then((data) => console.log(data)) .catch((err) => console.log(err)); }

//I have made several variations of this function, but nothing works. async function currentAuthenticatedUserSession(){ Auth.currentSession() .then((user) => { console.log(user); return user }) .catch((err) => console.log(err)); }

async function changeEmailComplete(code){ // Collect confirmation code and new email, then Auth.verifyCurrentUserAttributeSubmit('email', code) .then((data) => console.log(data)) .catch((err) => console.log(err)); }

1 Answer
0
Accepted Answer

Found this which solved the problem: https://github.com/aws-amplify/amplify-js/issues/10108

and also updated the changeEmail() to:

async function changeEmail(newEmail){ // Send confirmation code to user's old email const user = await Auth.currentAuthenticatedUser(); await Auth.updateUserAttributes(user, {'email':newEmail}) .then((data) => console.log(data)) .catch((err) => console.log(err)); }

answered a year ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions