Skip to content

Commit

Permalink
fix: add better error handling around sample client and redirect URIs (
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Sep 5, 2018
1 parent 65751ff commit f38dd0c
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion samples/sampleclient.js
Expand Up @@ -33,15 +33,35 @@ if (fs.existsSync(keyPath)) {
keys = keyFile.installed || keyFile.web;
}

const invalidRedirectUri = `The provided keyfile does not define a valid
redirect URI. There must be at least one redirect URI defined, and this sample
assumes it redirects to 'http://localhost:3000/oauth2callback'. Please edit
your keyfile, and add a 'redirect_uris' section. For example:
"redirect_uris": [
"http://localhost:3000/oauth2callback"
]
`;

class SampleClient {
constructor (options) {
this._options = options || { scopes: [] };

// validate the redirectUri. This is a frequent cause of confusion.
if (!keys.redirect_uris || keys.redirect_uris.length === 0) {
throw new Error(invalidRedirectUri);
}
const redirectUri = keys.redirect_uris[keys.redirect_uris.length - 1];
const parts = url.parse(redirectUri, false);
if (redirectUri.length === 0 || parts.port !== '3000' || parts.hostname !== 'localhost' || parts.path !== '/oauth2callback') {
throw new Error(invalidRedirectUri);
}

// create an oAuth client to authorize the API call
this.oAuth2Client = new google.auth.OAuth2(
keys.client_id,
keys.client_secret,
keys.redirect_uris[0]
redirectUri
);
}

Expand Down

0 comments on commit f38dd0c

Please sign in to comment.