Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
View,
Button,
Linking,
AsyncStorage,
} from 'react-native';
import jwtDecoder from 'jwt-decode';

Expand All @@ -27,14 +28,16 @@ class App extends React.Component {
state = {
username: undefined,
};

componentDidMount() {
Linking.addEventListener('url', this._handleAuth0Redirect);
Linking.addEventListener('url', this._handleAuth0Redirect.bind(this));
}

_loginWithAuth0 = async () => {
const redirectionURL = `${auth0Domain}/authorize` + this._toQueryString({
client_id: auth0ClientId,
response_type: 'token',
response_type: 'id_token',
nonce: await this._getNonce(),
scope: 'openid name',
redirect_uri: redirectUri,
state: redirectUri,
Expand All @@ -45,7 +48,8 @@ class App extends React.Component {
_loginWithAuth0Twitter = async () => {
const redirectionURL = `${auth0Domain}/authorize` + this._toQueryString({
client_id: auth0ClientId,
response_type: 'token',
response_type: 'id_token',
nonce: await this._getNonce(),
scope: 'openid name',
redirect_uri: redirectUri,
connection: 'twitter',
Expand All @@ -55,7 +59,6 @@ class App extends React.Component {
}

_handleAuth0Redirect = async (event) => {
console.log('yo');
if (!event.url.includes('+/redirect')) {
return;
}
Expand All @@ -72,10 +75,30 @@ class App extends React.Component {
this.setState({ username });
}

/**
* Generate a cryptographically random nonce.
* @param {Number} length
*/
_generateRandomString = (length) => {
const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._~';
return [...Array(length)]
.map(() => charset.charAt(Math.floor(Math.random() * charset.length)))
.join('');
}

_getNonce = async () => {
let nonce = await AsyncStorage.getItem('nonce');
if (!nonce) {
nonce = this._generateRandomString(16);
await AsyncStorage.setItem('nonce', nonce);
}
return nonce;
}

/**
* Converts an object to a query string.
*/
_toQueryString(params) {
_toQueryString = (params) => {
return '?' + Object.entries(params)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
Expand Down