@shun-js/user 0.4.4 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shun-js/user",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
4
4
|
"description": "user",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"shun.js",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"access": "public",
|
|
35
35
|
"registry": "https://registry.npmjs.org/"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "a3707c20f4632d54ba051ff2431e667128bc9782"
|
|
38
38
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// model
|
|
2
|
+
const { addUserInfo, isUserInfoExists } = require('../model/UserInfoModel.js');
|
|
3
|
+
|
|
4
|
+
// google
|
|
5
|
+
const { getGoogleUserinfo } = require('../util/google.js');
|
|
6
|
+
|
|
7
|
+
// util
|
|
8
|
+
const { loginORRegUser } = require('../util/user.js');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* userGoogle
|
|
12
|
+
* @param {*} req
|
|
13
|
+
* @param {*} res
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
exports.userGoogle = async (req, res) => {
|
|
17
|
+
const methodName = 'userGoogle';
|
|
18
|
+
|
|
19
|
+
// fallback url
|
|
20
|
+
const fallbackUrl = global.QZ_CONFIG.google.fallbackUrl;
|
|
21
|
+
|
|
22
|
+
// const
|
|
23
|
+
const code = req.body.code;
|
|
24
|
+
if (!code) {
|
|
25
|
+
const msg = 'need code';
|
|
26
|
+
req.logger.warn(methodName, msg, req.body);
|
|
27
|
+
res.jsonFail(msg);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// userinfo
|
|
32
|
+
const googleUserinfo = await getGoogleUserinfo(req, code);
|
|
33
|
+
if (!googleUserinfo) {
|
|
34
|
+
req.logger.error(methodName, 'googleUserinfo is null');
|
|
35
|
+
res.redirect(fallbackUrl);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// user item
|
|
40
|
+
const userItem = await loginORRegUser(req, res, googleUserinfo.email);
|
|
41
|
+
if (!userItem) return;
|
|
42
|
+
req.logger.info(methodName, 'github login or reg ok');
|
|
43
|
+
|
|
44
|
+
// user info
|
|
45
|
+
const isUserInfoExistsRes = await isUserInfoExists(req, res, userItem.id);
|
|
46
|
+
if (!isUserInfoExistsRes) return;
|
|
47
|
+
if (isUserInfoExistsRes.length === 0) {
|
|
48
|
+
const addUserInfoRes = await addUserInfo(req, res, userItem.id, googleUserinfo);
|
|
49
|
+
if (!addUserInfoRes) return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// r
|
|
53
|
+
const finalUser = {
|
|
54
|
+
userid: userItem.id,
|
|
55
|
+
usertoken: userItem.usertoken,
|
|
56
|
+
};
|
|
57
|
+
res.jsonSuccess('登录成功!', finalUser);
|
|
58
|
+
};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const { getUserInfoById } = require('../model/UserInfoModel.js');
|
|
3
3
|
|
|
4
4
|
// util
|
|
5
|
-
const { getUserAccessToken } = require('../util/user.js');
|
|
5
|
+
const { getUserAccessToken, setUserAccessToken } = require('../util/user.js');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* userInfo
|
|
@@ -28,6 +28,7 @@ exports.userInfo = async (req, res) => {
|
|
|
28
28
|
|
|
29
29
|
// access token
|
|
30
30
|
const accessToken = await getUserAccessToken(req, userid);
|
|
31
|
+
await setUserAccessToken(req, userid, accessToken);
|
|
31
32
|
getUserInfoByIdRes.accessToken = accessToken;
|
|
32
33
|
|
|
33
34
|
// r
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// ajax
|
|
2
|
+
const { get, post } = require('qiao-ajax');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* getGoogleUserinfo
|
|
6
|
+
* @param {*} req
|
|
7
|
+
* @param {*} code
|
|
8
|
+
*/
|
|
9
|
+
exports.getGoogleUserinfo = async (req, code) => {
|
|
10
|
+
const methodName = 'getGoogleUserinfo';
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
// get token
|
|
14
|
+
const tokenUrl = global.QZ_CONFIG.google.tokenUrl;
|
|
15
|
+
const tokenConfig = {
|
|
16
|
+
params: {
|
|
17
|
+
client_id: global.QZ_CONFIG.google.clientID,
|
|
18
|
+
client_secret: global.QZ_CONFIG.google.clientSecret,
|
|
19
|
+
redirect_uri: global.QZ_CONFIG.google.callbackUrl,
|
|
20
|
+
grant_type: 'authorization_code',
|
|
21
|
+
code: code,
|
|
22
|
+
},
|
|
23
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
24
|
+
};
|
|
25
|
+
const tokenResponse = await post(tokenUrl, tokenConfig);
|
|
26
|
+
|
|
27
|
+
// check
|
|
28
|
+
if (!tokenResponse) {
|
|
29
|
+
req.logger.error(methodName, 'tokenResponse is null');
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (tokenResponse.status !== 200) {
|
|
33
|
+
req.logger.error(methodName, 'tokenResponse.status !== 200', tokenResponse.status);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (!tokenResponse.data) {
|
|
37
|
+
req.logger.error(methodName, 'tokenResponse.data is null');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!tokenResponse.data.access_token || !tokenResponse.data.token_type) {
|
|
41
|
+
req.logger.error(methodName, 'tokenResponse.data.access_token is null or tokenResponse.data.token_type is null');
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// get user
|
|
46
|
+
req.logger.info(methodName, 'get token success');
|
|
47
|
+
const userUrl = global.QZ_CONFIG.google.userUrl;
|
|
48
|
+
const userConfig = {
|
|
49
|
+
headers: {
|
|
50
|
+
Authorization: `${tokenResponse.data.token_type} ${tokenResponse.data.access_token}`,
|
|
51
|
+
Accept: 'application/json',
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
const userResponse = await get(userUrl, userConfig);
|
|
55
|
+
|
|
56
|
+
// check
|
|
57
|
+
if (!userResponse) {
|
|
58
|
+
req.logger.error(methodName, 'userResponse is null');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (userResponse.status !== 200) {
|
|
62
|
+
req.logger.error(methodName, 'userResponse.status !== 200', userResponse.status);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (!userResponse.data) {
|
|
66
|
+
req.logger.error(methodName, 'userResponse.data is null');
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
req.logger.info(methodName, 'get user success');
|
|
70
|
+
|
|
71
|
+
// r
|
|
72
|
+
return userResponse.data;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
req.logger.error(methodName, 'error', error);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// {
|
|
79
|
+
// login: 'uikoo9',
|
|
80
|
+
// id: 10345351,
|
|
81
|
+
// node_id: 'MDQ6VXNlcjEwMzQ1MzUx',
|
|
82
|
+
// avatar_url: 'https://avatars.githubusercontent.com/u/10345351?v=4',
|
|
83
|
+
// gravatar_id: '',
|
|
84
|
+
// url: 'https://api.github.com/users/uikoo9',
|
|
85
|
+
// html_url: 'https://github.com/uikoo9',
|
|
86
|
+
// followers_url: 'https://api.github.com/users/uikoo9/followers',
|
|
87
|
+
// following_url: 'https://api.github.com/users/uikoo9/following{/other_user}',
|
|
88
|
+
// gists_url: 'https://api.github.com/users/uikoo9/gists{/gist_id}',
|
|
89
|
+
// starred_url: 'https://api.github.com/users/uikoo9/starred{/owner}{/repo}',
|
|
90
|
+
// subscriptions_url: 'https://api.github.com/users/uikoo9/subscriptions',
|
|
91
|
+
// organizations_url: 'https://api.github.com/users/uikoo9/orgs',
|
|
92
|
+
// repos_url: 'https://api.github.com/users/uikoo9/repos',
|
|
93
|
+
// events_url: 'https://api.github.com/users/uikoo9/events{/privacy}',
|
|
94
|
+
// received_events_url: 'https://api.github.com/users/uikoo9/received_events',
|
|
95
|
+
// type: 'User',
|
|
96
|
+
// user_view_type: 'private',
|
|
97
|
+
// site_admin: false,
|
|
98
|
+
// name: null,
|
|
99
|
+
// company: null,
|
|
100
|
+
// blog: '',
|
|
101
|
+
// location: null,
|
|
102
|
+
// email: 'uikoo9@qq.com',
|
|
103
|
+
// hireable: null,
|
|
104
|
+
// bio: null,
|
|
105
|
+
// twitter_username: null,
|
|
106
|
+
// notification_email: 'uikoo9@qq.com',
|
|
107
|
+
// public_repos: 27,
|
|
108
|
+
// public_gists: 0,
|
|
109
|
+
// followers: 91,
|
|
110
|
+
// following: 4,
|
|
111
|
+
// created_at: '2014-12-30T03:11:05Z',
|
|
112
|
+
// updated_at: '2026-01-29T09:08:41Z',
|
|
113
|
+
// private_gists: 0,
|
|
114
|
+
// total_private_repos: 33,
|
|
115
|
+
// owned_private_repos: 33,
|
|
116
|
+
// disk_usage: 1026748,
|
|
117
|
+
// collaborators: 4,
|
|
118
|
+
// two_factor_authentication: true,
|
|
119
|
+
// plan: {
|
|
120
|
+
// name: 'free',
|
|
121
|
+
// space: 976562499,
|
|
122
|
+
// collaborators: 0,
|
|
123
|
+
// private_repos: 10000
|
|
124
|
+
// }
|
|
125
|
+
// }
|
package/server/util/user.js
CHANGED
|
@@ -78,3 +78,17 @@ exports.getUserAccessToken = async (req, userid) => {
|
|
|
78
78
|
await req.redis.set(acKey, newAccessToken);
|
|
79
79
|
return newAccessToken;
|
|
80
80
|
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* setUserAccessToken
|
|
84
|
+
* @param {*} req
|
|
85
|
+
* @param {*} userid
|
|
86
|
+
* @param {*} token
|
|
87
|
+
*/
|
|
88
|
+
exports.setUserAccessToken = async (req, userid, token) => {
|
|
89
|
+
const tokenKey = `token-${token}`;
|
|
90
|
+
const tokenValue = await req.redis.get(tokenKey);
|
|
91
|
+
if (tokenValue) return;
|
|
92
|
+
|
|
93
|
+
await req.redis.set(tokenKey, userid);
|
|
94
|
+
};
|