Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 309349a

Browse files
committed
Hotfix: fix google login
1 parent bcc1a86 commit 309349a

File tree

4 files changed

+171
-102
lines changed

4 files changed

+171
-102
lines changed

velog-backend/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1",
77
"dev": "NODE_PATH=src APP_ENV=local nodemon --inspect src/local --exec babel-node --plugins transform-es2015-modules-commonjs,transform-object-rest-spread,transform-async-to-generator --presets flow",
8-
"dev:local": "NODE_PATH=src POSTGRES_HOST=localhost nodemon --inspect src/local --exec babel-node --plugins transform-es2015-modules-commonjs,transform-object-rest-spread,transform-async-to-generator --presets flow",
8+
"dev:local": "NODE_PATH=src NODE_ENV=development nodemon --inspect src/local --exec babel-node --plugins transform-es2015-modules-commonjs,transform-object-rest-spread,transform-async-to-generator --presets flow",
99
"migrate": "NODE_PATH=src babel-node src/scripts/migrate --plugins transform-es2015-modules-commonjs,transform-object-rest-spread,transform-async-to-generator --presets flow",
1010
"migrate:dev": "NODE_PATH=src POSTGRES_HOST=localhost babel-node src/scripts/migrate --plugins transform-es2015-modules-commonjs,transform-object-rest-spread,transform-async-to-generator --presets flow",
1111
"deploy:dev": "sls deploy --stage dev",
@@ -56,7 +56,7 @@
5656
"feed": "^2.0.1",
5757
"filesize": "^3.6.1",
5858
"github": "^13.1.0",
59-
"googleapis": "^34.0.0",
59+
"googleapis": "^44.0.0",
6060
"joi": "^13.0.2",
6161
"json-diff": "^0.5.2",
6262
"jsonwebtoken": "^8.1.0",
@@ -67,7 +67,7 @@
6767
"koa-router": "^7.3.0",
6868
"lodash": "^4.17.4",
6969
"mailgun-js": "^0.13.1",
70-
"marked": "^0.5.2",
70+
"marked": "^0.7.0",
7171
"mime-types": "^2.1.18",
7272
"nodemon": "^1.12.1",
7373
"pg": "^7.4.1",

velog-backend/src/lib/getSocialProfile.js

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,21 @@ const profileGetters = {
5151
};
5252
});
5353
},
54-
google(accessToken: string): Promise<Profile> {
55-
const plus = google.plus({
56-
version: 'v1',
57-
auth: process.env.GOOGLE_SECRET,
58-
});
59-
return new Promise((resolve, reject) => {
60-
plus.people.get(
61-
{
62-
userId: 'me',
63-
access_token: accessToken,
64-
},
65-
(err, auth) => {
66-
if (err) {
67-
reject(err);
68-
return;
69-
}
70-
const {
71-
id, image, emails, displayName,
72-
} = auth.data;
73-
74-
const profile = {
75-
id,
76-
thumbnail: image.url,
77-
email: emails[0].value,
78-
name: displayName && displayName.split(' (')[0],
79-
};
80-
resolve(profile);
81-
},
82-
);
54+
async google(accessToken: string): Promise<Profile> {
55+
const people = google.people('v1');
56+
const profile = await people.people.get({
57+
access_token: accessToken,
58+
resourceName: 'people/me',
59+
personFields: 'names,emailAddresses,photos',
8360
});
61+
const { data } = profile;
62+
const socialProfile = {
63+
email: data.emailAddresses[0].value || null,
64+
name: data.names[0].displayName || 'emptyname',
65+
thumbnail: data.photos[0].url || null,
66+
id: data.resourceName.replace('people/', ''),
67+
};
68+
return socialProfile;
8469
},
8570
};
8671

velog-backend/src/router/auth/callback/callback.ctrl.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ export const redirectGoogleLogin: Middleware = (ctx) => {
102102
);
103103

104104
const url = oauth2Client.generateAuthUrl({
105-
scope: ['https://www.googleapis.com/auth/userinfo.email'],
105+
scope: [
106+
'https://www.googleapis.com/auth/userinfo.email',
107+
'https://www.googleapis.com/auth/userinfo.profile',
108+
],
106109
state: JSON.stringify({ next: next || '/trending' }),
107110
});
108111

@@ -139,7 +142,7 @@ export const googleCallback: Middleware = async (ctx) => {
139142
}
140143
const { access_token } = tokens;
141144
const hash = crypto.randomBytes(40).toString('hex');
142-
await redisClient.set(hash, access_token, 'EX', 30);
145+
await redisClient.set(hash, access_token, 'EX', 300);
143146
let nextUrl = `${baseUrl}callback?type=google&key=${hash}`;
144147
if (state) {
145148
const { next } = JSON.parse(state);

0 commit comments

Comments
 (0)