Created
May 15, 2018 19:50
-
-
Save justinwaite/fd6e2af51be6fd364b03caaba1fd0fb4 to your computer and use it in GitHub Desktop.
AWS Amplify - Authenticating a user via facebook using React Native and Expo (identity pool, not user pool).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Expo from "expo"; | |
| import { Auth } from "aws-amplify"; | |
| ... | |
| class SignInScreen extends React.Component { | |
| ... | |
| loginWithFacebook = async () => { | |
| const { | |
| type, | |
| token, | |
| expires | |
| } = await Expo.Facebook.logInWithReadPermissionsAsync("1112222333444555", { | |
| permissions: ["public_profile", "email"] | |
| }); | |
| if (type === "success") { | |
| // Get the user's information with the Facebook Graph api. | |
| const response = await fetch( | |
| `https://graph.facebook.com/me?access_token=${token}&fields=id,name,email,first_name,last_name,picture` | |
| ); | |
| const user = await response.json(); | |
| return { | |
| user, | |
| token, | |
| expires_at: expires | |
| }; | |
| } | |
| return null; | |
| } | |
| amplifyFacebookSignIn = async () => { | |
| // Use Expo to login with facebook and get user info. | |
| const { user, token, expires_at } = await this.loginWithFacebook(); | |
| // Use the token to authenticated Amplify. | |
| await Auth.federatedSignIn("facebook", { token, expires_at }, user); | |
| // At any point you can get the user's information using Auth.currentUserInfo() | |
| const userDetails = await Auth.currentUserInfo(); | |
| console.log("Current user info:", userDetails); | |
| // Navigate away or do something... | |
| this.props.navigation.navigate({ routeName: "Main" }); | |
| } | |
| ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment