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.44k stars 2.13k forks source link

Amplify stop session event is not received in pinpoint #1954

Closed PrasannaLearnyst closed 6 years ago

PrasannaLearnyst commented 6 years ago

Configuration


Amplify.configure({
  Auth: {
    identityPoolId: COGNITO_IDENTITY_POOL_ID,
    mandatorySignIn: false,
    region: REGION,
  },
  refreshHandlers: {
    'developer': async () => {
      await refreshToken();
    }
  },
  AWSPinpoint: {
    appId: MOBILE_ANALYTICS_APP_ID,
    region: REGION,
    mandatorySignIn: false,
    autoSessionRecord: true
  }
});

const federatedAuth = async ({ identity_id, token }, user) => {
  let federatedCreden = await Auth.federatedSignIn(
    'developer',
    {
      token,
      identity_id,
      expires_at: COGNITO_TOKEN_EXPIRY_TIME
    },
    user
    ).catch(err =>{
      console.log('federated sign in : ', err);
    });

    let { name, id, school_id, email } = user;

    await Analytics.updateEndpoint({
      UserId: id+''
    });
}

=============

Describe the bug

  1. The _session_start events are automatically pushed to pinpoint but unable to receive the _session_stop, do it need to send manually

  2. AWS auth credentials are not getting updated on init

Error stack

[DEBUG] 05:39.335 AuthClass - failed to get or parse item aws-amplify-federatedInfo: SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at AuthClass.currentUserCredentials (blob:http://localhost:8081/...) at Credentials._keepAlive (blob:http://localhost:8081/...) at Credentials._pickupCredentials (blob:http://localhost:8081/...:86) at Credentials.get (blob:http://localhost:8081/...25) at AWSPinpointProvider._getCredentials (blob:http://localhost:8081/...:39) at AWSPinpointProvider.<anonymous> (blob:http://localhost:8081/...:151681:45) at step (blob:http://localhost:8081/...:151483:31) at Object.next (blob:http://localhost:8081/...:151413:24) at blob:http://localhost:8081/...:151385:75

**Expected behavior**
1. The _session_stop is expected to receive automatically on app killed or closed.
2. The credentials are expected to update automatically when app is init
3. How can i restore the UserID on app restart / continue in next app open / next session

Smartphone (please complete the following information):

powerful23 commented 6 years ago

@PrasannaLearnyst

  1. Could you check if you have this message when the app is switched to background: https://github.com/aws-amplify/amplify-js/blob/master/packages/analytics/src/trackers/SessionTracker-rn.ts#L73

  2. The credentials do get updated when the request is sent. The debug info you pasted is not an error message.

  3. Why do you want to restore the UserId? Are you using your own? By default Amplify would use the identityId from the aws credentials as the UserId.

PrasannaLearnyst commented 6 years ago

@powerful23 thanks for the reply

  1. Could you check if you have this message when the app is switched to background No i'm unable to get "App has come to inactive/background, recording stop session"

  2. The credentials do get updated when the request is sent. The debug info you pasted is not an error message. Yes, its not a error, i have checked Auth.ts. Its credentials are taken from storage and as per the docs the storage will be using asyncstorage in react native but its not done on app closed and reopen

    After federated developer auth is success, the next app opens has to take the credentials from aws-amplify-federatedInfo its not taking from there.

please correct me if i'm wrong.

  1. Why do you want to restore the UserId? Are you using your own? By default Amplify would use the identityId from the aws credentials as the UserId.

I need to send user related data to the pinpoint where it pushes to kinesis -> s3. Is there option to set global attributes where those attributes will be send with all events to pinpoint.

==== Amplify: "version": "1.1.6"

"dependencies": { "@aws-amplify/analytics": "^1.2.3", "@aws-amplify/api": "^1.0.17", "@aws-amplify/auth": "^1.2.6", "@aws-amplify/cache": "^1.0.15", "@aws-amplify/core": "^1.0.15", "@aws-amplify/interactions": "^1.0.15", "@aws-amplify/pubsub": "^1.0.15", "@aws-amplify/storage": "^1.0.15", "@aws-amplify/xr": "^0.1.5" }

Update: This is the things i'm doing on init

let credentials = await Auth.currentCredentials()
  .catch(err => console.log(err));

on success/ fail

Amplify.configure({
    Auth: {
      identityPoolId: COGNITO_IDENTITY_POOL_ID,
      mandatorySignIn: false,
      region: REGION,
    },
    refreshHandlers: {
      'developer': async () => {
        await refreshToken();
      }
    },
    AWSPinpoint: {
      appId: MOBILE_ANALYTICS_APP_ID,
      region: REGION,
      mandatorySignIn: false,
      autoSessionRecord: true
    }
  });

what is the recommended approach to configure because the session_start request is made before restore of credentials

i need to restore my federated developer credential on app restart

powerful23 commented 6 years ago

@PrasannaLearnyst I am not understanding how you are initing. The desired way is first configuring Amplify(by the way the configuration is not correct):

Amplify.configure({
    Auth: {
        identityPoolId: COGNITO_IDENTITY_POOL_ID,
        mandatorySignIn: false,
        region: REGION,
        refreshHandlers: {
        }
    },
    Analytics: {
         AWSPinpoint: {
              appId: MOBILE_ANALYTICS_APP_ID,
              region: REGION,
              autoSessionRecord: true
         }
     }
});

The library will automatically get a credentials(guest or authenticated based on whether you are signed in or not) before sending the start session event.

PrasannaLearnyst commented 6 years ago

@powerful23

Sorry got it.

When i checked the AsyncStorage there are credentials, but the amplify when retrieve the credentials its getting error message as

{[DEBUG] 02:17.526 AuthClass - failed to get or parse item aws-amplify-federatedInfo: SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at AuthCla…}

======= error stack

"SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at AuthClass.currentUserCredentials (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:183232:38) at Credentials._keepAlive (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:150737:47) at Credentials._pickupCredentials (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:150716:86) at Credentials.get (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:150704:25) at AWSPinpointProvider._getCredentials (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:152128:39) at AWSPinpointProvider. (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:151769:45) at step (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:151571:31) at Object.next (blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:151501:24) at blob:http://localhost:8081/984e91b9-08ce-47cf-9554-ce49f3bc6fee:151473:75"

PrasannaLearnyst commented 6 years ago

@powerful23

In this file Auth.ts#L1012

you are getting the user details but its not stored in this key, rather its stored in key

aws-amplify-cachefederatedInfo
or 
@MemoryStorage:aws-amplify-federatedInfo

can you please cross check once

PrasannaLearnyst commented 6 years ago

@powerful23 Sorry,

  1. just checked code of Auth.ts, its getting data from reactnative.ts StorageHelper class.

The StorageHelper.getItem it will be taken from datastorage, for the datastorage the source is sync method of StorageHelper.

the sync is not called, how to make the data sync.

  1. Could you check if you have this message when the app is switched to background No i'm unable to get "App has come to inactive/background, recording stop session"

thanks

PrasannaLearnyst commented 6 years ago

@powerful23 can you please provide me any guidance on this i'm importing all the classes from

import Amplify, { Auth, Analytics } from 'aws-amplify';

for react native do i need to import from any other package.

I have changed my config as you said

Amplify.configure({
    Auth: {
        identityPoolId: COGNITO_IDENTITY_POOL_ID,
        mandatorySignIn: false,
        region: REGION,
        refreshHandlers: {
        }
    },
    Analytics: {
         AWSPinpoint: {
              appId: MOBILE_ANALYTICS_APP_ID,
              region: REGION,
              autoSessionRecord: true
         }
     }
});

i've checked in cache the details persist, kindly help me in restore of credentials on app restart

powerful23 commented 6 years ago

@PrasannaLearnyst yes, the way you import is correct. We will test if stop session events are sent as expected.

PrasannaLearnyst commented 6 years ago

@powerful23 no, the stop session events are not sent as expected

PrasannaLearnyst commented 6 years ago

@powerful23

any update?

powerful23 commented 6 years ago

@PrasannaLearnyst confirmed. Will mark this as bug.

PrasannaLearnyst commented 6 years ago

@powerful23 thanks

can you please review this pull request or whats the process of getting it reviewed.

https://github.com/aws-amplify/amplify-js/pull/1987

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.