2015-01-07 5 views
2

The Stormpath documentation 은 PostRegistrationHandler에서 사용자 속성을 수정하는 것에 대해 아무 것도 말하지 않고 이것을 수행 할 수 있어야합니다.Express.js Stormpath PostRegistrationHandler

사용자를 생성 한 후 속성으로 임의의 문자열을 지정하고 싶습니다. 이 무작위 문자열은 내 별도의 Mongo Database에 대한 열쇠가 될 것입니다. 내 app.js, 나는이 :

app.use(stormpath.init(app, { 

postRegistrationHandler: function(account, res, next) { 

// theoretically, this will give a user object a new property, 'mongo_id' 
// which will be used to retrieve user info out of MONGOOOO 
account.customData["mongo_id"] = "54aabc1c79f3e058eedcd2a7"; // <- this is the thing I'm trying to add 

console.log("RESPNSE:\n"+res); 

account.save(); // I know I'm using 'account', instead of user, but the documentation uses account. I don't know how to do this any other way 
next(); 
console.log('User:\n', account, '\njust registered!'); 
}, 

apiKeyId: '~/.stormpath.apiKey.properties', 
//apiKeySecret: 'xxx', 
application: ~removed~, 
secretKey: ~removed~, 
redirectUrl: '/dashboard', 
enableAutoLogin: true 

})); 

내 CONSOLE.LOG 라인에 mongo_id 속성으로 CUSTOMDATA를 인쇄 않습니다 방법을 모르겠어요. 나중에 req.user.customData [ 'mongo_id']를 사용하여 액세스하려고하면 그곳에 없습니다. 계정과 req.user는 서로 달라야합니다. 사용자를 저장하려면 어떻게해야합니까?

+0

질문 : 당신이 라이브러리의 'expandCustomData'옵션을 사용하고 여기에

는 현재 작업 솔루션입니다? – robertjd

+0

expandCustomData를 사용하지 않습니다 – Mike

+0

해당 옵션을 추가 할 수 있습니까? 사후 등록 정보 핸들러 – robertjd

답변

2

저는 위에서 언급 한 라이브러리의 저자입니다. 그래서 조금 도움이 될 것 같습니다. 내가 제대로 작동하도록 코드를 수정 한

=)하는 데 도움이

app.use(stormpath.init(app, { 
    postRegistrationHandler: function(account, res, next) { 
    // The postRegistrationHandler is a special function that returns the account 
    // object AS-IS. This means that you need to first make the account.customData stuff 
    // available using account.getCustomData as described here: 
    // http://docs.stormpath.com/nodejs/api/account#getCustomData 
    account.getCustomData(function(err, data) { 
     if (err) { 
     return next(err); 
     } else { 
     data.mongo_id = '54aabc1c79f3e058eedcd2a7'; 
     data.save(); 
     next(); 
     } 
    }); 
    }, 
    apiKeyId: 'xxx', 
    apiKeySecret: 'xxx', 
    application: ~removed~, 
    secretKey: ~removed~, 
    redirectUrl: '/dashboard', 
    enableAutoLogin: true, 
    expandCustomData: true, // this option makes req.user.customData available by default 
          // everywhere EXCEPT the postRegistrationHandler 
})); 

희망!

0

rdegges가 제공하는 솔루션이 완전히 올바르지 않습니다.

next()에 대한 호출은 customData가 저장을 마친 직후에 호출되어야하며 바로 호출하지 말아야하므로 data.save()의 콜백이어야합니다.

또한 postRegistrationHandler 매개 변수가 account, req, res, next으로 변경된 것 같습니다.

postRegistrationHandler: function(account, req, res, next) { 
    account.getCustomData(function(err, data) { 
     if (err) 
      return next(err); 

     data.mongo_id = '54aabc1c79f3e058eedcd2a7'; 
     data.save(next); 
    }); 
},