From 4f3a40c44f62eadc4ed76a0b2873c50a58c6d2e3 Mon Sep 17 00:00:00 2001 From: powerful23 Date: Thu, 10 May 2018 10:55:55 -0700 Subject: [PATCH 1/3] minor change --- packages/aws-amplify/src/Auth/Auth.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/aws-amplify/src/Auth/Auth.ts b/packages/aws-amplify/src/Auth/Auth.ts index 13a71493b1e..9a96c8388c6 100644 --- a/packages/aws-amplify/src/Auth/Auth.ts +++ b/packages/aws-amplify/src/Auth/Auth.ts @@ -1235,7 +1235,7 @@ export default class AuthClass { } - private _setCredentialsFromFederation(params) { + private async _setCredentialsFromFederation(params) { const { provider, token, identity_id, user, expires_at } = params; const domains = { 'google': 'accounts.google.com', @@ -1263,7 +1263,11 @@ export default class AuthClass { region }); - Cache.setItem('federatedInfo', { provider, token, identity_id, user, expires_at }, { priority: 1 }); + try { + await Cache.setItem('federatedInfo', { provider, token, identity_id, user, expires_at }, { priority: 1 }); + } catch (e) { + logger.debug('Failed to cache federated info with', e); + } return this._loadCredentials(credentials, 'federated', true, user); } @@ -1271,7 +1275,7 @@ export default class AuthClass { const that = this; return new Promise((res, rej) => { credentials.getPromise().then( - () => { + async () => { logger.debug('Load credentials successfully', credentials); that.credentials = credentials; that.credentials.authenticated = authenticated; @@ -1281,7 +1285,11 @@ export default class AuthClass { { id: this.credentials.identityId }, rawUser ); - Cache.setItem('federatedUser', that.user, { priority: 1 }); + try { + await Cache.setItem('federatedUser', that.user, { priority: 1 }); + } catch (e) { + logger.debug('Failed to cache federated user with', e); + } } res(that.credentials); }, From dff0ad5621e2d02da6110289e05126d5ddb28865 Mon Sep 17 00:00:00 2001 From: powerful23 Date: Thu, 10 May 2018 14:38:35 -0700 Subject: [PATCH 2/3] minor change --- .../src/Auth/Authenticator.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/aws-amplify-react-native/src/Auth/Authenticator.js b/packages/aws-amplify-react-native/src/Auth/Authenticator.js index 8e6e5329a89..1182b6520f1 100644 --- a/packages/aws-amplify-react-native/src/Auth/Authenticator.js +++ b/packages/aws-amplify-react-native/src/Auth/Authenticator.js @@ -16,7 +16,8 @@ import { View, TouchableWithoutFeedback, Keyboard } from 'react-native'; import { Auth, Analytics, - Logger + Logger, + Hub } from 'aws-amplify'; import AmplifyTheme from '../AmplifyTheme'; import AmplifyMessageMap from '../AmplifyMessageMap'; @@ -65,12 +66,20 @@ export default class Authenticator extends React.Component { this.handleStateChange = this.handleStateChange.bind(this); this.checkUser = this.checkUser.bind(this); + this.onHubCapsule = this.onHubCapsule.bind(this); + + Hub.listen('auth', this); } componentWillMount() { this.checkUser(); } + onHubCapsule(capsule) { + const { channel, payload, source } = capsule; + if (channel === 'auth') { this.checkUser(); } + } + handleStateChange(state, data) { logger.debug('authenticator state change ' + state); if (state === this.state.authState) { return; } From d2da6b6800faeabb128bf4b11a016ecd3ac5ba31 Mon Sep 17 00:00:00 2001 From: powerful23 Date: Fri, 11 May 2018 16:49:46 -0700 Subject: [PATCH 3/3] docs update --- docs/media/authentication_guide.md | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/docs/media/authentication_guide.md b/docs/media/authentication_guide.md index 381967d4efc..8ee45065c94 100644 --- a/docs/media/authentication_guide.md +++ b/docs/media/authentication_guide.md @@ -316,6 +316,45 @@ return ( ) ``` + +For React Native, you can use `Auth.federatedSignIn()` to get your federated identity from Cognito. You need to provide a valid JWT token from the third provider. You can also use it with `Authenticator` so that your login status will be automatically persisted by that component. + +Federated Sign in with Facebook Example: +```js +import Expo from 'expo'; +import Amplify, { Auth } from 'aws-amplify'; +import { Authenticator } from 'aws-amplify-react-native'; + +export default class App extends React.Component { + async signIn() { + const { type, token, expires } = await Expo.Facebook.logInWithReadPermissionsAsync('YOUR_FACEBOOK_APP_ID', { + permissions: ['public_profile'], + }); + if (type === 'success') { + // sign in with federated identity + Auth.federatedSignIn('facebook', { token, expires_at: expires}, { name: 'USER_NAME' }) + .then(credentials => { + console.log('get aws credentials', credentials); + }).catch(e => { + console.log(e); + }); + } + } + + // ... + + render() { + return ( + + + +