Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. export const loginGoogle = () => {
  2.  
  3. return new Promise(async (resolve, reject) => {
  4.  
  5. const user = await GoogleSignin.signIn().catch(error => {
  6. // console.log('WRONG SIGNIN', error);
  7. console.log('getOpenIdToken',getOpenIdToken);
  8. Alert.alert("13");
  9.  
  10. });
  11.  
  12. if (user) {
  13. const credential = await googleGetCredential();
  14.  
  15. if (credential) {
  16.  
  17. const result = await getOpenIdToken('accounts.google.com',
  18. credential.identityId, credential.idToken);
  19. resolve(result);
  20. Alert.alert("8");
  21.  
  22. return;
  23. }
  24.  
  25. reject('Authentication failed');
  26. console.log('failed');
  27. Alert.alert("9");
  28.  
  29. return;
  30. }
  31.  
  32. return;
  33. });
  34. };
  35.  
  36. export const getOpenIdToken = async (provider, identityId, token) => {
  37.  
  38. const payload = {
  39. IdentityId: identityId,
  40. Logins: {
  41. [provider]: token,
  42. },
  43. };
  44. Alert.alert("10");
  45.  
  46. try {
  47. const rsp = await fetch(COGNITO_IDENTITY_API, {
  48.  
  49. method: 'POST',
  50. headers: new Headers({
  51.  
  52. 'X-Amz-Target': 'AWSCognitoIdentityService.GetOpenIdToken',
  53. 'Content-Type': 'application/x-amz-json-1.1',
  54. random: new Date().valueOf(),
  55. 'cache-control': 'no-cache',
  56. }),
  57. body: JSON.stringify(payload),
  58.  
  59. });
  60.  
  61. if (!rsp.ok) {
  62. logout();
  63. } else {
  64. const json = await rsp.json();
  65.  
  66. return json.Token;
  67. }
  68. } catch (e) {
  69. console.log('Error of getOpenIdToken: ', e);
  70. }
  71. };
  72.  
  73. const googleConfigure = () => {
  74. GoogleSignin.hasPlayServices({ autoResolve: true })
  75. .then(() =>
  76. GoogleSignin.configure({
  77. iosClientId: GOOGLE_SIGNIN_IOS_CLIENT_ID,
  78. webClientId: GOOGLE_SIGNIN_WEBCLIENT_ID,
  79. })
  80. );
  81. };
  82.  
  83. export const init = () => {
  84. googleConfigure();
  85. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement