我需要使用内部用户管理系统来验证我的用户。该系统还保存了用户对组、角色和租户的成员资格,这在进行授权时是最有用的。
我看了帐户-角色的代码,但它不适合我。因此我推断我做错了什么。
服务器上有一个新的LoginHandler:
Meteor.startup( function () {
var config = Accounts.loginServiceConfiguration.findOne( {service: 'sertal'} );
if ( !config ) {
Accounts.loginServiceConfiguration.insert( { service: 'sertal' } );
}
} );
Accounts.registerLoginHandler( function ( options ) {
if ( !options.sertal && !options.assertion )
return undefined; // don't handle
var url = "http://dev.sertal.ch/checkCredential";
var request = {
params: {
uname: options.email,
pword: options.password
}
};
var result = Meteor.http.get( url, request );
if ( result.statusCode !== 200 ) {
throw new Meteor.Error( Accounts.LoginCancelledError.numericError, 'Sertal Login Failed' );
} else {
var user = result.data.userrec;
user.groups = result.data.grprec;
user.id = user._id;
return Accounts.updateOrCreateUserFromExternalService( 'sertal', user );
}
} );
在客户端,我在按下登录按钮后使用以下代码:
Accounts.callLoginMethod( {
methodName: 'login',
methodArguments: {sertal: true,
email: $( '#sertal-email' ).val(),
password: $( '#sertal-password' ).val(),
resume: false
},
userCallback: function ( error ) {
if ( error ) {
console.log( "error: " + error );
} else {
$( "#sertalLoginFormDiv" ).dropdown( 'toggle' );
}
}
} );
但是它不会触发LoginHandler。一定是少了什么东西,但我想不出来。
我找不到关于这个主题的任何文档。一个答案也可以是指出一些解释这个过程的文档。
根据我的测试,methodararguments必须是一个对象数组。
另外,从我在日志中看到的,如果methodararguments对象在对象的根处包含一个密码,那么Meteor抛出一个错误"Failed Login {type: 'password',…"
通过将处理程序的所有参数作为对象的一部分,我能够使此工作。像这样,在客户端:
loginRequest = {myLogin:{email: email, password: password}};
Accounts.callLoginMethod({
methodArguments: [loginRequest],
userCallback: callback
});
在客户端执行时,meteor调用我的服务器代码:
Accounts.registerLoginHandler( function("someName", loginRequest{
if(loginRequest.myLogin){
// I get the loginRequestObject, and can attempt to sign in...
}
});