Skip to content
This repository has been archived by the owner on Jul 18, 2020. It is now read-only.

Get request google Api for userinfo via accessToken! #181

Open
mortezahaidari opened this issue May 20, 2020 · 0 comments
Open

Get request google Api for userinfo via accessToken! #181

mortezahaidari opened this issue May 20, 2020 · 0 comments

Comments

@mortezahaidari
Copy link

mortezahaidari commented May 20, 2020

I am currently using Expo app auth, "https://docs.expo.io/versions/latest/sdk/app-auth/" for authentication of my app.
New to this React Native world, but i would like to request for userinfo from google Api which this method is using. I tried using postman, and it returns the values i would need.
How can i use access token to get request for userinfo from Google Api?

The link below returned every value i needed, with bearer token of course.
'https://www.googleapis.com/oauth2/v2/userinfo'

Here is the code:

import React, { useEffect, useState } from 'react';
import { AsyncStorage, Button, StyleSheet, Text, View } from 'react-native';
import * as AppAuth from 'expo-app-auth';

export default function App() {
let [authState, setAuthState] = useState(null);

useEffect(() => {
(async () => {
let cachedAuth = await getCachedAuthAsync();
if (cachedAuth && !authState) {
setAuthState(cachedAuth);
}
})();
}, []);

return (

Expo AppAuth Example
<Button
title="Sign In with Google "
onPress={async () => {
const _authState = await signInAsync();
setAuthState(_authState);
}}
/>
<Button
title="Sign Out "
onPress={async () => {
await signOutAsync(authState);
setAuthState(null);
}}
/>
{JSON.stringify(authState, null, 2)}

);
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});

let config = {
issuer: 'https://accounts.google.com',
scopes: ['openid', 'profile'],
/* This is the CLIENT_ID generated from a Firebase project */
clientId: '603386649315-vp4revvrcgrcjme51ebuhbkbspl048l9.apps.googleusercontent.com',
};

let StorageKey = '@myapp:CustomGoogleOAuthKey';

export async function signInAsync() {
let authState = await AppAuth.authAsync(config);
await cacheAuthAsync(authState);
console.log('signInAsync', authState);
return authState;
}

async function cacheAuthAsync(authState) {
return await AsyncStorage.setItem(StorageKey, JSON.stringify(authState));
}

export async function getCachedAuthAsync() {
let value = await AsyncStorage.getItem(StorageKey);
let authState = JSON.parse(value);
console.log('getCachedAuthAsync', authState);
if (authState) {
if (checkIfTokenExpired(authState)) {
return refreshAuthAsync(authState);
} else {
return authState;
}
}
return null;
}

function checkIfTokenExpired({ accessTokenExpirationDate }) {
return new Date(accessTokenExpirationDate) < new Date();
}

async function refreshAuthAsync({ refreshToken }) {
let authState = await AppAuth.refreshAsync(config, refreshToken);
console.log('refreshAuth', authState);
await cacheAuthAsync(authState);
return authState;
}

export async function signOutAsync({ accessToken }) {
try {
await AppAuth.revokeAsync(config, {
token: accessToken,
isClientIdProvided: true,
});
await AsyncStorage.removeItem(StorageKey);
return null;
} catch (e) {
alert(Failed to revoke token: ${e.message});
}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant