Skip to content

Commit

Permalink
document verifyToken & can pass lang into getUserByCode
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Nov 9, 2016
1 parent 8beaa46 commit 432350a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/oauth.js
Expand Up @@ -322,7 +322,17 @@ OAuth.prototype.getUser = function (options, callback) {
});
};

OAuth.prototype._verifyToken = function (openid, accessToken, callback) {
/**
* 检验授权凭证(access_token)是否有效。
* Examples:
* ```
* api.verifyToken(openid, accessToken, callback);
* ```
* @param {String} openid 传入openid
* @param {String} accessToken 待校验的access token
* @param {Function} callback 回调函数
*/
OAuth.prototype.verifyToken = function (openid, accessToken, callback) {
var url = 'https://api.weixin.qq.com/sns/auth';
var info = {
access_token: accessToken,
Expand Down Expand Up @@ -362,16 +372,26 @@ OAuth.prototype._verifyToken = function (openid, accessToken, callback) {
* ]
* }
* ```
* @param {String} code 授权获取到的code
* @param {Object|String} options 授权获取到的code
* @param {Function} callback 回调函数
*/
OAuth.prototype.getUserByCode = function (code, callback) {
OAuth.prototype.getUserByCode = function (options, callback) {
var that = this;

var lang, code;
if (typeof options === 'string') {
code = options;
} else {
lang = options.lang;
code = options.code;
}

this.getAccessToken(code, function (err, result) {
if (err) {
return callback(err);
}
that.getUser(result.data.openid, callback);
var openid = result.data.openid;
that.getUser({openid: openid, lang: lang}, callback);
});
};

Expand Down
21 changes: 21 additions & 0 deletions test/oauth.test.js
Expand Up @@ -432,5 +432,26 @@ describe('oauth.js', function () {
done();
});
});

it('should ok with getUserByCode', function (done) {
var options = {code: 'code', lang: 'en'};
api.getUserByCode(options, function (err, data) {
expect(err).not.to.be.ok();
expect(data).to.have.keys('openid', 'nickname', 'sex', 'province', 'city',
'country', 'headimgurl', 'privilege');
done();
});
});
});

describe('verifyToken', function () {
var api = new OAuth('appid', 'secret');
it('should ok with verifyToken', function (done) {
api.verifyToken('openid', 'access_token', function (err, data) {
expect(err).to.be.ok();
expect(err.message).to.contain('access_token is invalid');
done();
});
});
});
});

0 comments on commit 432350a

Please sign in to comment.