2

I really hope this is my last question on the Stormpath API for a while. Making the switch from C# and PHP to node has been somewhat challenging.

I am trying to implement social login on the registration page. The pre-built option places it on the Login page only.

Rendering the page and adding elements to the Jade view is simple enough, but I need to get an oauthStateToken. Now I looked at how the login.js code does this:

        var oauth = require('../oauth');        
        var oauthStateToken = oauth.common.resolveStateToken(req, res);
        var formActionUri = (config.web.login.uri + (nextUri ? ('?next=' + nextUri) : ''));

        var hasSocialProviders = _.some(config.web.social, function (socialProvider) {
          return socialProvider.enabled;
        });

        extend(options, {
          form: form,
          formActionUri: formActionUri,
          oauthStateToken: oauthStateToken,
          hasSocialProviders: hasSocialProviders
        });

So essentially it calls the oath files and goes through the code, theoretically, I could do something similar on register.js - essentially (just sample code):

    var oauth = require('../oauth');
    var _ = require('lodash');
    var hasSocialProviders = _.some(config.web.social, function (socialProvider) {
          return socialProvider.enabled;
    });
    var oauthStateToken = '';
    if (hasSocialProviders) {
          oauthStateToken = oauth.common.resolveStateToken(req, res);
    }

    helpers.render(req, res, view, {
        form: helpers.sanitizeFormData(req.body),
        formModel: viewModel.form
        oauthStateToken : oauthStateToken,
        hasSocialProviders: hasSocialProviders
    });

But of course I wouldn't want to meddle with the core files, and was thinking about creating a middleware function, something like app.use('/register', callFunction, router); but I am having some difficulty in conceptualizing how that would work. I seem to understand how stormpath lets me generate an oauth token which I store as a cookie using JWT but I'm having a hard time putting it together into working code without replicating a lot of code already existent in the library.

I can even kinda of get it working by using the REST API but I would like to use it within the express-stormpath library. So some explanation in the context of express-stormpath library would also be enlightening. OR am I overthinking this, should I just fork the library, make the changes to register.js and try for a push upstream?

Thanks again folks,

4

1 回答 1

1

谢谢你的提问!

我找到了一种在您的应用程序中解决此问题的方法,而无需修改 express-stormpath 库。您需要为您的注册路由添加一个路由处理程序,并且在该路由处理程序中,您必须手动完成oAuthStateToken设置工作,然后调用next()以让 express-stormpath 继续处理注册页面的请求。看起来是这样的:

var cookieParser = require('cookie-parser');
var oauth = require('express-stormpath/lib/oauth');

// create your express server, 'app'

app.use('/register', cookieParser(), function (req, res, next) {
  var oauthStateToken = oauth.common.resolveStateToken(req, res);
  req.app.get('stormpathConfig').templateContext = {
    oauthStateToken: oauthStateToken
  };
  next();
});

app.use(stormpath.init(app, {
  // .. other config options
  web: {
    register: {
      view: 'views/register.jade'  // path to your custom registration file
    }
  }
}));

我希望这有帮助!这当然可以更容易,这是图书馆的一个很好的功能要求。

于 2016-05-06T16:44:24.960 回答