我正在尝试使用GitHub在localhost解析服务器中设置第三方身份验证的简单示例。我阅读了解析指南,Wiki,旧问题和网站(解析的开放源代码发布之前和之后)。几乎所有东西都在工作,但最后一部分是: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.
我认为我没有正确编写auth对象,但仅从解析服务器网站(https://docs.parseplatform.org/parse-server/guide/#oauth-and-3rd-party-authentication)中的“指南”的“自定义身份验证”部分就无法弄清楚该如何做。
提前谢谢!
答案 0 :(得分:0)
我已经实现了github登录,但是使用的方式与您不同,但是我认为它应该可以正常运行,过程应该是相同的。
您需要获取访问令牌,接下来需要在https://api.github.com/user处获得令牌的github用户ID。最后调用_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
})
希望这会对您有所帮助。