Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.
Closed
Changes from 4 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
37 changes: 30 additions & 7 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,25 +28,28 @@ class App extends React.Component {
state = {
username: undefined,
};

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

_loginWithAuth0 = async () => {
async _loginWithAuth0() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why You've changed arrow functions into normal ones? When you are changing this from arrow you do not have a this context. That's why you have to .bind(this) at the top in this._handleAuth0Redirect call :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, I just would to uniformise all functions style ^^ So back to arrow functions!

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,
});
Expo.WebBrowser.openBrowserAsync(redirectionURL);
}

_loginWithAuth0Twitter = async () => {
async _loginWithAuth0Twitter() {
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 @@ -54,8 +58,7 @@ class App extends React.Component {
Expo.WebBrowser.openBrowserAsync(redirectionURL);
}

_handleAuth0Redirect = async (event) => {
console.log('yo');
async _handleAuth0Redirect(event) {
if (!event.url.includes('+/redirect')) {
return;
}
Expand All @@ -72,6 +75,26 @@ 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('');
}

async _getNonce() {
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.
*/
Expand Down