Skip to content

Commit

Permalink
fix: use URL constructor in the samples (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Nov 5, 2018
1 parent 5c0a5f6 commit 1809e13
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
5 changes: 2 additions & 3 deletions samples/oauth2.js
Expand Up @@ -17,7 +17,6 @@ const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const opn = require('opn');
const destroyer = require('server-destroy');

Expand Down Expand Up @@ -61,10 +60,10 @@ async function authenticate(scopes) {
.createServer(async (req, res) => {
try {
if (req.url.indexOf('/oauth2callback') > -1) {
const qs = querystring.parse(url.parse(req.url).query);
const qs = new url.URL(req.url).searchParams;
res.end('Authentication successful! Please return to the console.');
server.destroy();
const {tokens} = await oauth2Client.getToken(qs.code);
const {tokens} = await oauth2Client.getToken(qs.get('code'));
oauth2Client.credentials = tokens;
resolve(oauth2Client);
}
Expand Down
9 changes: 4 additions & 5 deletions samples/sampleclient.js
Expand Up @@ -20,7 +20,6 @@
const {google} = require('googleapis');
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const opn = require('opn');
const destroyer = require('server-destroy');
const fs = require('fs');
Expand Down Expand Up @@ -54,12 +53,12 @@ class SampleClient {
throw new Error(invalidRedirectUri);
}
const redirectUri = keys.redirect_uris[keys.redirect_uris.length - 1];
const parts = url.parse(redirectUri, false);
const parts = new url.URL(redirectUri);
if (
redirectUri.length === 0 ||
parts.port !== '3000' ||
parts.hostname !== 'localhost' ||
parts.path !== '/oauth2callback'
parts.pathname !== '/oauth2callback'
) {
throw new Error(invalidRedirectUri);
}
Expand All @@ -86,12 +85,12 @@ class SampleClient {
.createServer(async (req, res) => {
try {
if (req.url.indexOf('/oauth2callback') > -1) {
const qs = querystring.parse(url.parse(req.url).query);
const qs = new url.URL(req.url).searchParams;
res.end(
'Authentication successful! Please return to the console.'
);
server.destroy();
const {tokens} = await this.oAuth2Client.getToken(qs.code);
const {tokens} = await this.oAuth2Client.getToken(qs.get('code'));
this.oAuth2Client.credentials = tokens;
resolve(this.oAuth2Client);
}
Expand Down

0 comments on commit 1809e13

Please sign in to comment.