Created
December 5, 2021 16:24
-
-
Save EvanBurbidge/7002197470ce5382015cf76e0eacd04d to your computer and use it in GitHub Desktop.
Example of auth0 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 React from 'react'; | |
import { StyleSheet, Text, View, Button, Alert } 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/auth0-example because I am | |
signed in as the "community" account on Expo and the slug for this app is "auth0-example" (check out app.json). | |
You can open this app in the Expo client and check your logs to find out your redirect URL. | |
*/ | |
const auth0ClientId = 'YOUR-CLIENT-ID'; | |
const auth0Domain = 'https://yourtenant.auth0.com'; | |
/** | |
* Converts an object to a query string. | |
*/ | |
function toQueryString(params) { | |
return '?' + Object.entries(params) | |
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) | |
.join('&'); | |
} | |
const Login = () => { | |
const loginFunc = 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}`); // <-- you will need to set this in your allowed urls in auth0 | |
// 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); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment