## Subscribing Events You can take specific actions when users sign-in or sign-out by subscribing authentication events in your app. Please see our [Hub Module Developer Guide](/lib/utilities/hub) for more information. ## Identity Pool Federation You can alternatively use `Auth.federatedSignIn()` to get AWS credentials directly from Cognito Federated Identities and not use User Pool federation. If you have logged in with `Auth.signIn()` you **can not** call `Auth.federatedSignIn()` as Amplify will perform this federation automatically for you in the background. In general, if you are using Cognito User Pools to manage user Sign-Up and Sign-In, you should only call `Auth.federatedSignIn()` when using OAuth flows or the Hosted UI. ```js import { Auth } from 'aws-amplify'; // To derive necessary data from the provider const { token, // the token you get from the provider domainOrProviderName, // Either the domain of the provider(e.g. accounts.your-openid-provider.com) or the provider name, for now the library only supports 'google', 'facebook', 'amazon', 'developer' expiresIn, // the time in ms which describes how long the token could live user, // the user object you defined, e.g. { username, email, phone_number } identity_id // Optional, the identity id specified by the provider } = getFromProvider(); // arbitrary function async function getCognitoCredentials() { try { const cred = await Auth.federatedSignIn( domainOrProviderName, { token, identity_id, // Optional expires_at: expiresIn * 1000 + new Date().getTime() // the expiration timestamp }, user ); console.log(cred); const authenticatedUser = await Auth.currentAuthenticatedUser(); console.log(authenticatedUser); } catch(err) { console.log(err); } }; ``` Note that this isn't from a Cognito User Pool so the user you get after calling this method is not a *Cognito User*. ### Facebook sign-in (React) ```js import React, { useEffect } from 'react'; import { Auth } from 'aws-amplify'; // To federated sign in from Facebook const SignInWithFacebook = () => { useEffect(() => { if (!window.FB) createScript(); }, []) const signIn = () => { const fb = window.FB; fb.getLoginStatus(response => { if (response.status === 'connected') { getAWSCredentials(response.authResponse); } else { fb.login( response => { if (!response || !response.authResponse) { return; } getAWSCredentials(response.authResponse); }, { // the authorized scopes scope: 'public_profile,email' } ); } }); } const getAWSCredentials = (response) => { const { accessToken, expiresIn } = response; const date = new Date(); const expires_at = expiresIn * 1000 + date.getTime(); if (!accessToken) { return; } const fb = window.FB; fb.api('/me', { fields: 'name,email' }, response => { const user = { name: response.name, email: response.email }; getCognitoCredentials('facebook', { token: accessToken, expires_at }, user); }); } const getCognitoCredentials = async (providerName, federateOptions, user) => { try { const credentials = await Auth.federatedSignIn(providerName, federateOptions, user); console.log(credentials); } catch(err) { console.log(err); } }; const createScript = () => { // load the sdk window.fbAsyncInit = fbAsyncInit; const script = document.createElement('script'); script.src = 'https://connect.facebook.net/en_US/sdk.js'; script.async = true; script.onload = initFB; document.body.appendChild(script); } const initFB = () => { const fb = window.FB; console.log('FB SDK initialized'); } const fbAsyncInit = () => { // init the fb sdk client const fb = window.FB; fb.init({ appId : 'your_facebook_app_id', cookie : true, xfbml : true, version : 'v2.11' }); } return (
); } ``` ### Facebook Sign-in (React Native - Expo) ```javascript import Expo from 'expo'; import React from 'react'; import { Amplify, Auth } from 'aws-amplify'; const App = () => { const signIn = async () => { const { type, token, expires } = await Expo.Facebook.logInWithReadPermissionsAsync('YOUR_FACEBOOK_APP_ID', { permissions: ['public_profile'], }); if (type === 'success') { // sign in with federated identity try { const credentials = await Auth.federatedSignIn('facebook', { token, expires_at: expires}, { name: 'USER_NAME' }) console.log('get aws credentials', credentials); } catch(err) { console.log(err); } } } // ... return (