无法在解析服务器中使用 GitHub 进行第三方身份验证



我正在尝试使用 GitHub 在本地主机解析服务器中设置一个简单的第三方身份验证示例。我阅读了解析指南,维基,以及旧问题和网站(解析开源之前和之后(。几乎所有事情都正常工作,但最后一部分:GitHub访问令牌和Parse.User之间的链接。

这是我的客户端和服务器代码。

客户端代码(使用 hello.js 用于连接 github 并获取access_token(:

<html>
<body>
<script src="src/hello.polyfill.js"></script>
<script src="src/hello.js"></script>
<script src="src/modules/github.js"></script>
<script src="https://npmcdn.com/parse/dist/parse.min.js"></script>
<button onclick="hello('github').login()">Login with GitHub</button>
<div id='profile'></div>
<script>
const parseClientID = "[MY_PARSE_APP_ID]";
const githubClientID = "[MY_GITHUB_APP_ID]";
Parse.initialize(parseClientID);
Parse.setURL = "http://localhost:1337/parse";
var provider = {
authenticate(options) {if (options.success) {options.success(this, {});}},
restoreAuthentication(authData) {},
getAuthType() {return 'github';},
deauthenticate() {}
};
let authData = {authData: {access_token: 'REPLACED_ON_THE_FLY', id: githubClientID}};
hello.init({github: githubClientID}, {
oauth_proxy: 'http://localhost:3000/proxy',
redirect_uri: 'http://localhost:3000/redirect'
});
// after loging in, when github calls back, this part of the code tries to
// link the github data with a Parse.User
hello.on('auth.login', (auth) => {
authData.authData.access_token = auth.authResponse.access_token;
var user = new Parse.User();
user._linkWith(provider, authData).then(usr=>console.log(usr), err=>console.log(err));
});
</script>
</body>
</html>

服务器代码(没有什么花哨的,标准的解析服务器路由,以及与hello.js交谈的oauthshim(:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var ParseServer = require('parse-server').ParseServer;
var oauthshim = require('oauth-shim');
var app = express();
app.get('/', (req, res) => {res.render('index');});
app.get('/redirect', (req, res) => {res.render('redirect');});
var api = new ParseServer({
"appId": "[MY_PARSE_APP_ID]",
"masterKey": "[MY_PARSE_MASTER_KEY]",
"appName": "connect",
"databaseURI": "mongodb://127.0.0.1:27017/parse",
"serverURL": "http://localhost:1337/parse",
"auth": {"github": {"id":"[MY_GITHUB_APP_ID]","access_token":"spaceholder"}}
});
app.use('/parse', api);
oauthshim.init([{
client_id: '[MY_GITHUB_APP_ID]',
client_secret: '[MY_GITHUB_SECRET]',
grant_url: 'https://github.com/login/oauth/access_token',
domain: 'http://localhost:3000/redirect'
}]);
app.use('/proxy', oauthshim);
app.listen(1337, function() {console.log('parse-server running on port 1337.');});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) { next(createError(404));});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;

客户端显示单个"登录"按钮。单击时,它连接到 github,获取访问令牌,然后用于 user._linkWith((。

此时,我在 Web 控制台中收到此错误:

error: Github auth is invalid for this user. code=101, message=Github auth is invalid for this user.

我认为我没有正确编写身份验证对象,但是仅从解析服务器网站(https://docs.parseplatform.org/parse-server/guide/#oauth-and-3rd-party-authentication(指南的"自定义身份验证"部分就无法弄清楚如何做到这一点。

提前谢谢你!

我已经实现了 github 登录,但与您的方式不同,但我认为它应该可以工作,过程应该相同。

您需要获取访问令牌,然后使用该令牌获取 github 用户 ID https://api.github.com/user。最后调用_linkWith方法。

在服务器端,您无需添加身份验证配置。 您的服务器应该是:

var api = new ParseServer({
"appId": "[MY_PARSE_APP_ID]",
"masterKey": "[MY_PARSE_MASTER_KEY]",
"appName": "connect",
"databaseURI": "mongodb://127.0.0.1:27017/parse",
"serverURL": "http://localhost:1337/parse",
});

在客户端,您无需配置提供程序。就这样称呼_linkWith:

hello.on('auth.login', (auth) => {
// get the github user id before
const authData = {
id: 'your github user id'
access_token: 'your access token'
}
const user = new Parse.User()
return user._linkWith('github', { authData }).then(user => {
// do what you want with user
})

希望这对您有所帮助。

最新更新