diff --git a/with-auth0/.gh-assets/auth0-app-settings.jpg b/with-auth0/.gh-assets/auth0-app-settings.jpg new file mode 100644 index 0000000..20d1b9d Binary files /dev/null and b/with-auth0/.gh-assets/auth0-app-settings.jpg differ diff --git a/with-auth0/App.js b/with-auth0/App.js new file mode 100644 index 0000000..2de0279 --- /dev/null +++ b/with-auth0/App.js @@ -0,0 +1,97 @@ +import React from 'react'; +import { Alert, Button, StyleSheet, Text, View } from 'react-native'; +import { AuthSession } from 'expo'; +import jwtDecode from 'jwt-decode'; + +// You need to swap out the Auth0 client id and domain with the one from your Auth0 client. +// In your Auth0 client, you need to also add a url to your authorized redirect urls. +// +// For this application, I added https://auth.expo.io/@arielweinberger/with-auth0 because I am +// signed in as the "arielweinberger" account on Expo and the name/slug for this app is "with-auth0". +// +// You can open this app in the Expo client and check your logs to find out your redirect URL. + +const auth0ClientId = ''; +const auth0Domain = 'https://arielweinberger.eu.auth0.com'; + +export default class App extends React.Component { + state = { + name: null, + }; + + login = async () => { + // Retrieve the redirect URL, add this to the callback URL list + // of your Auth0 application. + const redirectUrl = AuthSession.getRedirectUrl(); + console.log(`Redirect URL: ${redirectUrl}`); + + // Structure the auth parameters and URL + const queryParams = toQueryString({ + client_id: auth0ClientId, + redirect_uri: redirectUrl, + response_type: 'id_token', // id_token will return a JWT token + scope: 'openid profile', // retrieve the user's profile + nonce: 'nonce', // ideally, this will be a random value + }); + const authUrl = `${auth0Domain}/authorize` + queryParams; + + // Perform the authentication + const response = await AuthSession.startAsync({ authUrl }); + console.log('Authentication response', response); + + if (response.type === 'success') { + this.handleResponse(response.params); + } + }; + + handleResponse = (response) => { + if (response.error) { + Alert('Authentication error', response.error_description || 'something went wrong'); + return; + } + + // Retrieve the JWT token and decode it + const jwtToken = response.id_token; + const decoded = jwtDecode(jwtToken); + + const { name } = decoded; + this.setState({ name }); + }; + + render() { + const { name } = this.state; + + return ( + + {name + ? You are logged in, {name}! + :