Skip to content

Commit f0b3791

Browse files
author
lucifer
committed
fix: not return
1 parent cf5534d commit f0b3791

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

middleware/passport.js

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,71 @@ module.exports = async function checkAuth(ctx, next) {
1616
}
1717
if (ctx.session.user) {
1818
await next();
19-
return;
20-
}
21-
const token = ctx.cookies.get("token");
19+
} else {
20+
const token = ctx.cookies.get("token");
2221

23-
if (token) {
24-
const duserStr = decrypt(token);
25-
if (duserStr) {
26-
try {
27-
const duser = JSON.parse(duserStr);
22+
if (token) {
23+
const duserStr = decrypt(token);
24+
if (duserStr) {
25+
try {
26+
const duser = JSON.parse(duserStr);
2827

29-
if (db.find((q) => q.login === duser.login)) {
30-
ctx.body = duser;
31-
return;
28+
if (db.find((q) => q.login === duser.login)) {
29+
ctx.body = duser;
30+
return;
31+
}
32+
} catch (err) {
33+
console.log("token 解析失败:", err);
3234
}
33-
} catch (err) {
34-
console.log("token 解析失败:", err);
3535
}
3636
}
37-
}
38-
const code = ctx.query.code;
39-
const { access_token } = await fetch(
40-
`https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${secret}`,
41-
{
42-
method: "POST",
37+
const code = ctx.query.code;
38+
const { access_token } = await fetch(
39+
`https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${secret}`,
40+
{
41+
method: "POST",
42+
headers: {
43+
Accept: "application/json",
44+
},
45+
}
46+
).then((res) => res.json());
47+
48+
const user = await fetch("https://api.github.com/user", {
4349
headers: {
4450
Accept: "application/json",
51+
Authorization: `token ${access_token}`,
4552
},
46-
}
47-
).then((res) => res.json());
53+
}).then((res) => res.json());
4854

49-
const user = await fetch("https://api.github.com/user", {
50-
headers: {
51-
Accept: "application/json",
52-
Authorization: `token ${access_token}`,
53-
},
54-
}).then((res) => res.json());
55+
if (db.find((q) => q.login === user.login)) {
56+
ctx.cookies.set(
57+
"token",
58+
encrypt(
59+
Buffer.from(
60+
JSON.stringify({
61+
...user,
62+
pay: true,
63+
}),
64+
"utf8"
65+
)
66+
),
67+
{
68+
httpOnly: false,
69+
expires: new Date(24 * 60 * 60 * 1000 + Date.now()),
70+
}
71+
);
5572

56-
if (db.find((q) => q.login === user.login)) {
57-
ctx.cookies.set(
58-
"token",
59-
encrypt(
60-
Buffer.from(
61-
JSON.stringify({
62-
...user,
63-
pay: true,
64-
}),
65-
"utf8"
66-
)
67-
),
68-
{
69-
httpOnly: false,
70-
expires: new Date(24 * 60 * 60 * 1000 + Date.now()),
71-
}
72-
);
73+
ctx.session.user = {
74+
...user,
75+
pay: true,
76+
};
77+
} else {
78+
ctx.session.user = {
79+
...user,
80+
pay: false,
81+
};
82+
}
7383

74-
ctx.session.user = {
75-
...user,
76-
pay: true,
77-
};
78-
} else {
79-
ctx.session.user = {
80-
...user,
81-
pay: false,
82-
};
84+
await next();
8385
}
84-
85-
await next();
8686
};

0 commit comments

Comments
 (0)