Skip to content

Instantly share code, notes, and snippets.

@themeteorchef
Last active November 29, 2016 12:21
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themeteorchef/b8b30db0f08c5b818448 to your computer and use it in GitHub Desktop.
Save themeteorchef/b8b30db0f08c5b818448 to your computer and use it in GitHub Desktop.
Create user accounts that automatically login without using Accounts.createUser on the client.
Template.signup.events(
'submit form': (e,t) ->
# Prevent form from submitting.
e.preventDefault()
# Grab the user's details.
user =
email: t.find('[name="emailAddress"]').value
password: t.find('[name="password"]').value
# Create the user's account.
Meteor.call 'createUserAccount', user, (error) ->
# If the account is created successfully, log the user in using the credentials
# from above.
if error
alert error.reason
else
Meteor.loginWithPassword(user.email, user.password, (error)->
alert error.reason if error
)
)
<template name="signup">
<form id="application-signup" class="signup">
<div class="form-group">
<label for="emailAddress">Email Address</label>
<input type="email" name="emailAddress" class="form-control" placeholder="Email Address">
</div> <!-- end .form-group -->
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" placeholder="Password">
</div> <!-- end .form-group -->
<div class="form-group">
<input type="submit" class="btn btn-success" value="Sign Up">
</div> <!-- end .form-group -->
</form>
<p>Already have an account? <a href="{{pathFor 'login'}}">Log In</a>.</p>
</template>
###
Accounts
Server side account creation and manipulation methods.
Configuration:
- forbidClientAccountCreation: Disallow client side account creation.
Methods:
- createUserAccount: Performs a server-side account creation using the Meteor Accounts Password package.
###
# Configuration:
Accounts.config(
forbidClientAccountCreation: true
)
# Define Methods
Meteor.methods(
createUserAccount: (user)->
# Check values against correct pattern.
pattern = { email: String, password: String }
check(user, pattern)
# Create the user.
Accounts.createUser(user)
)
@themeteorchef
Copy link
Author

This recipe assumes the following:

  • You've given Meteor appropriate access to the Fibers NPM package (see this: https://coderwall.com/p/srvdta – check out the comments for a simplified solution).
  • You've installed the Meteor accounts-password package using meteor add accounts-password.
  • You've installed the check package using meteor add check.
  • (Optional) You've installed the audit-argument-checks package using meteor add audit-argument-checks.

@themeteorchef
Copy link
Author

UPDATE 07.30.14 – Refactored the server side component to remove the need for Futures/Fibers. Was tipped off by a question via Josh Owens that it wasn't necessary. Tested it and this proved to be true. Updated code is above!

@ermosk
Copy link

ermosk commented Feb 16, 2015

What is the purpose of the exception. Is it meant to be handled (i.e. shown on the client) ,is it meant to be put under the sheets and let client validation do it's thing or should it be spewed out in the browser's console?

I am using your code and it seems when password is empty, check throws an Error which I can't catch and is shown in browser console as:

Exception while simulating the effect of invoking 'createUserAccount' Meteor.makeErrorType.errorClass {message: "Match error: One or more properties do not match the schema.", path: "", sanitizedError: Meteor.makeErrorType.errorClass, errorType: "Match.Error", stack: (...)…} Error: Match error: One or more properties do not match the schema.
at SimpleSchema.condition (http://localhost:3000/packages/aldeed_simple-schema.js?8fda161c43c0ba62801a10b0dfcc3eab75c6db88:2450:11)
at checkSubtree (http://localhost:3000/packages/check.js?ac81167b8513b85b926c167bba423981b0c4cf9c:255:17)
at check (http://localhost:3000/packages/check.js?ac81167b8513b85b926c167bba423981b0c4cf9c:67:5)
at Meteor.methods.createUserAccount (http://localhost:3000/both/methods/accounts.js?c418120e76666f0ca774a281caafc39bc2c3a59d:4:27)
at http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4244:25
at _.extend.withValue (http://localhost:3000/packages/meteor.js?81e2f06cff198adaa81b3bc09fc4f3728b7370ec:949:17)
at _.extend.apply (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4235:54)
at _.extend.call (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4113:17)
at Object.Template.PasswordRegister.events.submit form (http://localhost:3000/client/views/shared/accounts/accounts.js?ac573d92938a2b3d6107ea19e50065f7ac5d41b3:36:20)
at null. (http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:3147:18)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment