问题
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