-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
82 lines (73 loc) · 1.93 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Application } from "egg";
const debug = require("debug")("egg-passport-weapp");
const assert = require("assert");
const Strategy = require("passport-weapp").Strategy;
function mountOneClient(
config: any,
app: Application,
client: string = "weapp"
) {
config.passReqToCallback = true;
assert(config.key, "[egg-passport-weapp] config.passportWeapp.key required");
assert(
config.secret,
"[egg-passport-weapp] config.passportWeapp.secret required"
);
app.passport.use(
client,
new Strategy(
{
...config,
appID: config.key,
appSecret: config.secret
},
(
req: any,
accessToken: string,
refreshToken: string,
profile: any,
expires_in: number,
verified: any
) => {
console.log("arguments = ", {
accessToken,
refreshToken,
profile,
expires_in,
verified
});
const user = {
provider: "weapp",
id: profile.unionid || profile.openid,
name: profile.unionid || profile.openid,
displayName: profile.unionid || profile.openid,
photo: "",
gender:
profile.gender === "FEMALE"
? "female"
: profile.gender === "MALE"
? "male"
: "unknown",
accessToken,
refreshToken,
session_key: profile.session_key,
profile
};
debug("%s %s get user: %j", req.method, req.url, user);
console.log("do verifying...", app.passport.doVerify);
app.passport.doVerify(req, user, verified);
}
)
);
}
export default (app: Application) => {
const config = app.config.passportWeapp;
if (config.clients) {
for (const client in config.clients) {
const c = config.clients[client];
mountOneClient(c, app, client);
}
} else {
mountOneClient(config, app);
}
};