11const fetch = require ( "node-fetch" ) ;
22const { encrypt, decrypt } = require ( "../utils/crypto" ) ;
33const { fail } = require ( "../utils/request" ) ;
4-
5- const secret = process . env . secret ;
6-
7- const clientId = "c16b80e7b58a5a007157" ;
8- const db = [
9- {
10- login : "azl397985856123" ,
11- } ,
12- {
13- login : "Yueqi-19" ,
14- } ,
15- ] ;
4+ const { secret, db, clientId } = require ( "../config/index" ) ;
165
176module . exports = async function checkAuth ( ctx , next ) {
187 if ( ! ctx . session ) {
@@ -21,6 +10,7 @@ module.exports = async function checkAuth(ctx, next) {
2110 if ( ctx . session . user ) {
2211 await next ( ) ;
2312 } else {
13+ // 1. 如果有 token ,则说明是之前种植过的,直接解析(如果是别人伪造的则会解析失败)
2414 const token = ctx . cookies . get ( "token" ) ;
2515
2616 if ( token ) {
@@ -37,6 +27,7 @@ module.exports = async function checkAuth(ctx, next) {
3727 }
3828 }
3929 }
30+ // 2. 如果没有 token,就必须有 code,因此这个时候需要拿 code 去 github 登录,取用户的信息。
4031 const code = ctx . query . code ;
4132 if ( ! code ) {
4233 ctx . body = fail ( { message : "请先登录~" , code : 91 } ) ;
@@ -46,6 +37,7 @@ module.exports = async function checkAuth(ctx, next) {
4637 return ;
4738 }
4839 try {
40+ // 3. 根据 code 获取用户信息
4941 const { access_token } = await fetch (
5042 `https://github.com/login/oauth/access_token?code=${ code } &client_id=${ clientId } &client_secret=${ secret } ` ,
5143 {
@@ -66,59 +58,28 @@ module.exports = async function checkAuth(ctx, next) {
6658 // user.login 存在表示登录成功
6759 if ( user . login ) {
6860 // 付费用户
69- if ( db . find ( ( q ) => q . login === user . login ) ) {
70- // TODO: 如果不在组织中,自动邀请进 Github 组织
71- // see #1 https://octokit.github.io/rest.js/v18#orgs-check-membership
72- // see #2 https://github.com/octokit/octokit.js
73- // see #3 https://github.com/thundergolfer/automated-github-organization-invites/blob/bb1bb3d42a330716f4dd5c49256245e4bde27489/web_app.rb
74- ctx . session . user = {
75- ...user ,
76- pay : true ,
77- } ;
78-
79- ctx . cookies . set (
80- "token" ,
81- encrypt (
82- Buffer . from (
83- JSON . stringify ( {
84- ...user ,
85- pay : true ,
86- } ) ,
87- "utf8"
88- )
89- ) ,
90- {
91- httpOnly : false ,
92- expires : new Date ( 24 * 60 * 60 * 1000 + Date . now ( ) ) ,
93- }
94- ) ;
95- } else {
96- ctx . session . user = {
97- ...user ,
98- pay : false ,
99- } ;
100-
101- ctx . cookies . set (
102- "token" ,
103- encrypt (
104- Buffer . from (
105- JSON . stringify ( {
106- ...user ,
107- pay : false ,
108- } ) ,
109- "utf8"
110- )
111- ) ,
112- {
113- httpOnly : false ,
114- expires : new Date ( 24 * 60 * 60 * 1000 + Date . now ( ) ) ,
115- }
116- ) ;
117- }
61+ const u = {
62+ ...user ,
63+ pay : ! ! db . find ( ( q ) => q . login === user . login ) ,
64+ } ;
65+ // TODO: 如果不在组织中,自动邀请进 Github 组织
66+ // see #1 https://octokit.github.io/rest.js/v18#orgs-check-membership
67+ // see #2 https://github.com/octokit/octokit.js
68+ // see #3 https://github.com/thundergolfer/automated-github-organization-invites/blob/bb1bb3d42a330716f4dd5c49256245e4bde27489/web_app.rb
69+ ctx . session . user = u ;
70+ ctx . cookies . set (
71+ "token" ,
72+ encrypt ( Buffer . from ( JSON . stringify ( u ) , "utf8" ) ) ,
73+ {
74+ httpOnly : false ,
75+ expires : new Date ( 24 * 60 * 60 * 1000 + Date . now ( ) ) , // 一天后过期,后期考虑延长时间
76+ }
77+ ) ;
11878 }
11979
12080 await next ( ) ;
12181 } catch ( err ) {
82+ // 4. 登录过程中出错,会跳转至此
12283 ctx . body = fail ( { message : "登录失败, code 码已失效~" , code : 93 } ) ;
12384 }
12485 }
0 commit comments