aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

Missing check for null user #303

Closed ldgarcia closed 6 years ago

ldgarcia commented 6 years ago

Error:

Object {[ERROR] 07:53.700 Authenticator: TypeError: Cannot read property 'getSession' of null
    at http://localhost:8081/index.bundle?platf…}

Current code:

if (Platform.isReactNative) {
  const that = this;
  return this.getSyncedUser().then(user => {
    return new Promise((resolve, reject) => {
      // null user here results in TypeError
      user.getSession(function(err, session) {
        if (err) {
          reject(err);
        } else {
          resolve(user);
        }
      });
    });
  });
}

Proposed change:

if (Platform.isReactNative) {
  const that = this;
  return this.getSyncedUser().then(user => {
    if (!user) { return Promise.reject('No current user in userPool'); }
    return new Promise((resolve, reject) => {
      user.getSession(function(err, session) {
        if (err) {
          reject(err);
        } else {
          resolve(user);
        }
      });
    });
  });
}

Please see: https://github.com/aws/aws-amplify/blob/72159fb67735c0361e9e61903f4995ff0d95e828/packages/aws-amplify/src/Auth/Auth.ts#L393

mlabieniec commented 6 years ago

@ldgarcia did you want to submit a pr for that update?

ldgarcia commented 6 years ago

@mlabieniec yes, I can also submit the change for #304, if you are ok with those changes.

mlabieniec commented 6 years ago

Looks ok to me, let's see what @powerful23 thinks. you can submit the PR we can review it there, thanks

ldgarcia commented 6 years ago

Ok, will do

powerful23 commented 6 years ago

@ldgarcia Good catch!

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.