Distinguishing between Sign Up and Sign In with Meteor.loginWithExternalService()

烂漫一生 提交于 2020-01-24 11:22:40

问题


Meteor provides a set of loginWithExternalService() methods (such as loginWithTwitter()) that lets users either create accounts or log back in with these third-party auth providers.

But is there a way to distinguish between both actions? i.e. let people sign in with Twitter without necessarily letting them sign up with it via the same action?

The practical use case is for a site where sign-ups are restricted and you have a private URL for signing up, but a public page for signing in. I'm looking for a way to prevent people from being able to create accounts simply by signing in for the first time.


回答1:


You could probably hook into Accounts.onCreateUser (server side)

Something like this might help:

server side js

Accounts.onCreateUser(function(options, user) {

  //Check if this user can be created, if not throw an error
  var canCreate = false

  if(!canCreate) 
      throw new Meteor.Error(403, 'You cant sign up', "Sorry you can only sign in but not sign up");

  //Create the user like normal if we can.
  if (options.profile)
    user.profile = options.profile;
  return user;
});

Throwing the error prevents the method from returning and creating an account. It only runs if someone is creating an account and doesn't already have one (external service providers too)

On the client you can handle the error but on the accounts-ui package you get an 'internal server error' message. You could probably customize this to 'you need to be an admin' or something



来源:https://stackoverflow.com/questions/19613950/distinguishing-between-sign-up-and-sign-in-with-meteor-loginwithexternalservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!