这是我目前用来在后端实现苹果身份验证的导入
const Joi = require('joi')
const logger = require('../common/logger')
const errors = require('../common/errors')
const helper = require('../common/helper')
const models = require('../models')
const _ = require('lodash')
const jwt = require('jsonwebtoken')
const ms = require('ms')
const constants = require('../../app-constants')
const userService = require('./UserService')
const adminCommonService = require('./admin/AdminCommonService')
const User = models.User
const VerificationCode = models.VerificationCode
const fs = require('fs');
const AppleAuth = require('apple-auth');
const config = fs.readFileSync("./config/config");
const auth = new AppleAuth(config, 'config/apple/authkey.p8')
/**apple auth
*/
const config = {
client_id: 'YOUR APP ID',
team_id: 'ZUU8485r88T',
redirect_uri: '', // Leave it blank
key_id: 'YOUR KEY ID',
scope: 'name%20email',
};
const appleAuth = new AppleAuth(
config,
fs.readFileSync('assets/apple/AuthKey_HVC8C93672.p8').toString(),
'text'
);
export const appleAuth = async (req: Request, res: Response){
const { token: appleToken } = req.body;
try {
//retrieve apple Data from AccessToken
const apple = await AppleAuth.accessToken(appleToken)
const appleData = jwt.decode(apple.id_token);
//get apple Id
const{sub:appleID}=appleData;
//count the number of connections of user
const user = await User.findOneAndUpdate(
{appleId},
{ $inc: { nbOfConnections: 1 } },
{upsert: true, new:true,userFindAndModify: false}
);
return res.status(200).json({user});
} catch (err) {
return res.status(500).json({message:err.message || err});
}
};
这是我的package.json
样子:
main": "app.js",
"scripts": {
"start": "node app.js",
"lint": "standard",
"lint:fix": "standard --fix",
"clear-db": "node src/clear-db.js",
"test-data": "node src/test-data.js"
},
"author": "TCSCODER",
"license": "none",
"devDependencies": {
"standard": "^12.0.1"
},
"dependencies": {
"adm-zip": "^0.4.14",
"apple-auth": "^1.0.6",
"aws-sdk": "^2.636.0",
"axios": "^0.19.2",
"bcryptjs": "^2.4.3",
"bluebird": "^3.7.2",
"body-parser": "^1.15.1",
"bootstrap": "^4.5.2",
"config": "^3.0.0",
"core-util-is": "^1.0.2",
"cors": "^2.7.1",
"credit-card-type": "^8.3.0",
"express": "^4.14.0",
"get-parameter-names": "^0.3.0",
"googleapis": "^59.0.0",
"http-status-codes": "^1.4.0",
"joi": "^14.3.1",
"json-diff": "^0.5.4",
"json2csv": "^5.0.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.15",
"mime-types": "^2.1.27",
"moment": "^2.24.0",
"moment-range": "^4.0.2",
"moment-timezone": "^0.5.27",
"mongodb": "^3.6.2",
"mongoose": "^5.8.9",
"mongoose-field-encryption": "^3.0.4",
"morgan": "^1.7.0",
"ms": "^2.1.2",
"multer": "^1.4.2",
"node-schedule": "^1.3.2",
"nodemailer": "^4.7.0",
"nylas": "^4.8.0",
"parse-duration": "^0.4.4",
"stripe": "^8.32.0",
"superagent": "^5.2.1",
"validator": "^10.9.0",
"winston": "^3.1.0"
},
"engines": {
"node": "12.x"
}
}
当我运行 npm start 时出现此错误,有什么想法吗?内部/模块/cjs/loader.js:969 抛出错误;^
Error: Cannot find module '../utils/appleAuth'
Require stack:
我尝试重新安装 npm,删除我的 package.json 文件夹和节点模块文件夹。我也尝试过重新安装库,但没有成功。每次我使用 npm start 我都会得到同样的错误。我对 api 很陌生,希望得到一些帮助。谢谢你!