Skip to content

Commit b27e3c8

Browse files
author
lucifer
committed
feat: erro catch
1 parent 336f426 commit b27e3c8

File tree

3 files changed

+143
-44
lines changed

3 files changed

+143
-44
lines changed

middleware/passport.js

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,56 +48,59 @@ module.exports = async function checkAuth(ctx, next) {
4848
ctx.body = fail({ message: "code 码无效,请重新登录", code: 92 });
4949
return;
5050
}
51-
const { access_token } = await fetch(
52-
`https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${secret}`,
53-
{
54-
method: "POST",
51+
try {
52+
const { access_token } = await fetch(
53+
`https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${secret}`,
54+
{
55+
method: "POST",
56+
headers: {
57+
Accept: "application/json",
58+
},
59+
}
60+
).then((res) => res.json());
61+
62+
const user = await fetch("https://api.github.com/user", {
5563
headers: {
5664
Accept: "application/json",
65+
Authorization: `token ${access_token}`,
5766
},
58-
}
59-
).then((res) => res.json());
60-
if (!access_token) return;
67+
}).then((res) => res.json());
6168

62-
const user = await fetch("https://api.github.com/user", {
63-
headers: {
64-
Accept: "application/json",
65-
Authorization: `token ${access_token}`,
66-
},
67-
}).then((res) => res.json());
69+
// user.login 存在表示登录成功
70+
if (user.login) {
71+
ctx.cookies.set(
72+
"token",
73+
encrypt(
74+
Buffer.from(
75+
JSON.stringify({
76+
...user,
77+
pay: true,
78+
}),
79+
"utf8"
80+
)
81+
),
82+
{
83+
httpOnly: false,
84+
expires: new Date(24 * 60 * 60 * 1000 + Date.now()),
85+
}
86+
);
6887

69-
// user.login 存在表示登录成功
70-
if (user.login) {
71-
ctx.cookies.set(
72-
"token",
73-
encrypt(
74-
Buffer.from(
75-
JSON.stringify({
76-
...user,
77-
pay: true,
78-
}),
79-
"utf8"
80-
)
81-
),
82-
{
83-
httpOnly: false,
84-
expires: new Date(24 * 60 * 60 * 1000 + Date.now()),
88+
if (db.find((q) => q.login === user.login)) {
89+
ctx.session.user = {
90+
...user,
91+
pay: true,
92+
};
93+
} else {
94+
ctx.session.user = {
95+
...user,
96+
pay: false,
97+
};
8598
}
86-
);
87-
88-
if (db.find((q) => q.login === user.login)) {
89-
ctx.session.user = {
90-
...user,
91-
pay: true,
92-
};
93-
} else {
94-
ctx.session.user = {
95-
...user,
96-
pay: false,
97-
};
9899
}
99-
}
100100

101-
await next();
101+
await next();
102+
} catch (err) {
103+
ctx.body = fail({ message: "登录失败, code 码已失效~", code: 93 });
104+
}
102105
}
103106
};

static/lectures/lectures-by-id.json

Lines changed: 96 additions & 1 deletion
Large diffs are not rendered by default.

utils/crypto.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
return encrypted.toString("hex");
1111
},
1212
decrypt(content) {
13+
if (!content) return "";
1314
const decipher = crypto.createDecipheriv(
1415
algorithm,
1516
secret.slice(0, 32),

0 commit comments

Comments
 (0)