我有以下代码
passport.use(new AppleStrategy({
clientID: process.env.APPLE_CLIENT_ID,
teamID: process.env.APPLE_TEAM_ID,
callbackURL: process.env.BASE_SERVER_URL + "/api/user/apple-login-callback",
keyID: process.env.APPLE_KEY_ID,
privateKeyLocation: "./keys/d.p8",
passReqToCallback: true
}, function(req, accessToken, refreshToken, idToken, profile, cb) {
// The idToken returned is encoded. You can use the jsonwebtoken library via jwt.decode(idToken)
// to access the properties of the decoded idToken properties which contains the user's
// identity information.
// Here, check if the idToken.sub exists in your database!
// idToken should contains email too if user authorized it but will not contain the name
// `profile` parameter is REQUIRED for the sake of passport implementation
// it should be profile in the future but apple hasn't implemented passing data
// in access token yet https://developer.apple.com/documentation/sign_in_with_apple/tokenresponse
cb(null, idToken);
// User.login(username, password).then(result => {
// const payload = {
// sub: result[0].id
// };
// // create a token string
// const token = jwt.sign(payload, process.env.JWT_SECRET);
//
// return done(null, token, result[0]);
// }).catch(err => {
// console.dir(err);
// return done(null, false);
// });
}));
不幸的是,我没有从护照上获得用户电子邮件或其他信息。用户被重定向到使用苹果登录,但是当苹果重定向回我的网站时,上面的代码会被触发,但它不会获取用户的电子邮件地址或信息。我做错了什么?
您必须从 req 参数中获取用户信息。但请注意:Apple 只为首次注册提供电子邮件、姓名等。您应该使用唯一的 id 令牌将提供的数据存储在数据库中。随后的每次登录都只会提供 id 令牌,您可以使用它从数据库获取用户数据。
// Check by idToken if user is already registered. If true, call cb function
if (req.body && req.body.user) {
// Register the user to your DB
console.log(req.body.user);
// call cb function
}