Last active
January 25, 2021 16:52
-
-
Save vadiraja/9f4060cf8400aa50e6d5df074a7b2872 to your computer and use it in GitHub Desktop.
Oauth configuration and URL Opener for social login
This file contains 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 { Linking } from 'expo'; | |
import * as WebBrowser from 'expo-web-browser'; | |
/**Amplify documentation has this example */ | |
const urlOpenerExpo = async (url, redirectUrl) => { | |
console.log(">>>>>>>>> in urlOpener") | |
// On Expo, use WebBrowser.openAuthSessionAsync to open the Hosted UI pages. | |
const { type, url: newUrl } = await WebBrowser.openAuthSessionAsync(url, redirectUrl); | |
console.log("Type") | |
console.log(type) | |
console.log(newUrl) | |
if (type === 'success') { | |
await WebBrowser.dismissBrowser(); | |
if (Platform.OS === 'ios') { | |
return Linking.openURL(newUrl); | |
} | |
} | |
}; | |
const oauth = { | |
// Domain name //********.auth.us-east-1.amazoncognito.com | |
domain : '<Cognito Pool OAuth DOMAIN name>', | |
// Authorized scopes | |
scope : ['phone', 'email', 'profile', 'openid','aws.cognito.signin.user.admin'], | |
// Callback URL | |
redirectSignIn : 'expoamplifytest://', // http://www.example.com/signin/ or 'exp://127.0.0.1:19000/--/', 'myapp://main/' | |
// Sign out URL | |
redirectSignOut : 'expoamplifytest://', // 'http://www.example.com/signout/' or 'exp://127.0.0.1:19000/--/', 'myapp://main/' | |
// 'code' for Authorization code grant, | |
// 'token' for Implicit grant | |
// Note that REFRESH token will only be generated when the responseType is code | |
responseType: 'code', | |
// optional, for Cognito hosted ui specified options | |
options: { | |
// Indicates if the data collection is enabled to support Cognito advanced security features. By default, this flag is set to true. | |
AdvancedSecurityDataCollectionFlag : true | |
}, | |
urlOpener: urlOpenerExpo | |
} | |
awsconfig.oauth = oauth; | |
onst expoScheme = "expoamplifytest://" | |
let redirectUrl = Linking.makeUrl(); | |
console.log(redirectUrl) | |
// simulator on localhost and devices on LAN | |
if (redirectUrl.startsWith('exp://1')) { | |
redirectUrl = redirectUrl + '/--/'; | |
} else | |
if (redirectUrl === expoScheme) { | |
// no change required | |
} else { | |
// for expo client | |
redirectUrl = redirectUrl + '/' | |
} | |
awsconfig.oauth.redirectSignIn = redirectUrl; | |
awsconfig.oauth.redirectSignOut = redirectUrl; | |
Amplify.configure(awsconfig); | |
Auth.configure(awsconfig); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment