@shun-js/user 0.3.1 → 0.3.3

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.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "user",
5
5
  "keywords": [
6
6
  "shun.js",
@@ -21,9 +21,10 @@
21
21
  "app.js"
22
22
  ],
23
23
  "dependencies": {
24
- "@shun-js/shun-config": "^0.3.0",
24
+ "@shun-js/shun-config": "^0.3.1",
25
25
  "@shun-js/shun-service": "^0.3.1",
26
- "qiao-encode": "^5.0.1",
26
+ "qiao-ajax": "^5.2.4",
27
+ "qiao-encode": "^5.0.6",
27
28
  "qiao-log": "^5.1.9",
28
29
  "qiao-mysql": "^5.8.6",
29
30
  "qiao-redis": "^5.8.9",
@@ -33,5 +34,5 @@
33
34
  "access": "public",
34
35
  "registry": "https://registry.npmjs.org/"
35
36
  },
36
- "gitHead": "58d6627e7b7c82baa950ada76cb9fb4c3bbe0650"
37
+ "gitHead": "a916a86ad6f2253239e446c2f8a9890a1fa6164d"
37
38
  }
@@ -0,0 +1,12 @@
1
+ // service
2
+ const service = require('../service/UserGithubService.js');
3
+
4
+ /**
5
+ * controller
6
+ */
7
+ module.exports = (app) => {
8
+ // user github
9
+ app.post('/user/github', (req, res) => {
10
+ service.userGithub(req, res);
11
+ });
12
+ };
@@ -0,0 +1,36 @@
1
+ // github
2
+ const { getGithubUserinfo } = require('../util/github.js');
3
+
4
+ /**
5
+ * userGithub
6
+ * @param {*} req
7
+ * @param {*} res
8
+ * @returns
9
+ */
10
+ exports.userGithub = async (req, res) => {
11
+ const methodName = 'userGithub';
12
+
13
+ // fallback url
14
+ const fallbackUrl = global.QZ_CONFIG.github.fallbackUrl;
15
+
16
+ // const
17
+ const code = req.body.code;
18
+ if (!code) {
19
+ const msg = 'need code';
20
+ req.logger.warn(methodName, msg, req.body);
21
+ res.jsonFail(msg);
22
+ return;
23
+ }
24
+
25
+ // userinfo
26
+ const githubUserinfo = await getGithubUserinfo(req, code);
27
+ if (!githubUserinfo) {
28
+ req.logger.error(methodName, 'githubUserinfo is null');
29
+ res.redirect(fallbackUrl);
30
+ return;
31
+ }
32
+ req.logger.info(methodName, 'githubUserinfo', githubUserinfo);
33
+
34
+ // r
35
+ res.jsonSuccess('登录成功!');
36
+ };
@@ -0,0 +1,126 @@
1
+ // ajax
2
+ const { get } = require('qiao-ajax');
3
+
4
+ /**
5
+ * getGithubUserinfo
6
+ * @param {*} req
7
+ * @param {*} code
8
+ */
9
+ exports.getGithubUserinfo = async (req, code) => {
10
+ const methodName = 'getGithubUserinfo';
11
+
12
+ try {
13
+ // get token
14
+ const tokenUrl = global.QZ_CONFIG.github.tokenUrl;
15
+ const tokenConfig = {
16
+ params: {
17
+ client_id: global.QZ_CONFIG.github.clientID,
18
+ client_secret: global.QZ_CONFIG.github.clientSecret,
19
+ redirect_uri: global.QZ_CONFIG.github.callbackUrl,
20
+ code: code,
21
+ },
22
+ headers: {
23
+ Accept: 'application/json',
24
+ },
25
+ };
26
+ const tokenResponse = await get(tokenUrl, tokenConfig);
27
+
28
+ // check
29
+ if (!tokenResponse) {
30
+ req.logger.error(methodName, 'tokenResponse is null');
31
+ return;
32
+ }
33
+ if (tokenResponse.status !== 200) {
34
+ req.logger.error(methodName, 'tokenResponse.status !== 200', tokenResponse.status);
35
+ return;
36
+ }
37
+ if (!tokenResponse.data) {
38
+ req.logger.error(methodName, 'tokenResponse.data is null');
39
+ return;
40
+ }
41
+ if (!tokenResponse.data.access_token || !tokenResponse.data.token_type) {
42
+ req.logger.error(methodName, 'tokenResponse.data.access_token is null or tokenResponse.data.token_type is null');
43
+ return;
44
+ }
45
+
46
+ // get user
47
+ req.logger.info(methodName, 'get token success');
48
+ const userUrl = global.QZ_CONFIG.github.userUrl;
49
+ const userConfig = {
50
+ headers: {
51
+ Authorization: `${tokenResponse.data.token_type} ${tokenResponse.data.access_token}`,
52
+ Accept: 'application/json',
53
+ },
54
+ };
55
+ const userResponse = await get(userUrl, userConfig);
56
+
57
+ // check
58
+ if (!userResponse) {
59
+ req.logger.error(methodName, 'userResponse is null');
60
+ return;
61
+ }
62
+ if (userResponse.status !== 200) {
63
+ req.logger.error(methodName, 'userResponse.status !== 200', userResponse.status);
64
+ return;
65
+ }
66
+ if (!userResponse.data) {
67
+ req.logger.error(methodName, 'userResponse.data is null');
68
+ return;
69
+ }
70
+ req.logger.info(methodName, 'get user success');
71
+
72
+ // r
73
+ return userResponse.data;
74
+ } catch (error) {
75
+ req.logger.error(methodName, 'error', error);
76
+ }
77
+ };
78
+
79
+ // {
80
+ // login: 'uikoo9',
81
+ // id: 10345351,
82
+ // node_id: 'MDQ6VXNlcjEwMzQ1MzUx',
83
+ // avatar_url: 'https://avatars.githubusercontent.com/u/10345351?v=4',
84
+ // gravatar_id: '',
85
+ // url: 'https://api.github.com/users/uikoo9',
86
+ // html_url: 'https://github.com/uikoo9',
87
+ // followers_url: 'https://api.github.com/users/uikoo9/followers',
88
+ // following_url: 'https://api.github.com/users/uikoo9/following{/other_user}',
89
+ // gists_url: 'https://api.github.com/users/uikoo9/gists{/gist_id}',
90
+ // starred_url: 'https://api.github.com/users/uikoo9/starred{/owner}{/repo}',
91
+ // subscriptions_url: 'https://api.github.com/users/uikoo9/subscriptions',
92
+ // organizations_url: 'https://api.github.com/users/uikoo9/orgs',
93
+ // repos_url: 'https://api.github.com/users/uikoo9/repos',
94
+ // events_url: 'https://api.github.com/users/uikoo9/events{/privacy}',
95
+ // received_events_url: 'https://api.github.com/users/uikoo9/received_events',
96
+ // type: 'User',
97
+ // user_view_type: 'private',
98
+ // site_admin: false,
99
+ // name: null,
100
+ // company: null,
101
+ // blog: '',
102
+ // location: null,
103
+ // email: 'uikoo9@qq.com',
104
+ // hireable: null,
105
+ // bio: null,
106
+ // twitter_username: null,
107
+ // notification_email: 'uikoo9@qq.com',
108
+ // public_repos: 27,
109
+ // public_gists: 0,
110
+ // followers: 91,
111
+ // following: 4,
112
+ // created_at: '2014-12-30T03:11:05Z',
113
+ // updated_at: '2026-01-29T09:08:41Z',
114
+ // private_gists: 0,
115
+ // total_private_repos: 33,
116
+ // owned_private_repos: 33,
117
+ // disk_usage: 1026748,
118
+ // collaborators: 4,
119
+ // two_factor_authentication: true,
120
+ // plan: {
121
+ // name: 'free',
122
+ // space: 976562499,
123
+ // collaborators: 0,
124
+ // private_repos: 10000
125
+ // }
126
+ // }