Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add better error handling around sample client and redirect URIs #1285

Merged
merged 3 commits into from Sep 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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