Commit 62a1d9344abbcd0f14447439a3cc92b6236b2030
1 parent
7439c59f5f
Exists in
master
Refactor server side component for sign up flow to remove need for Futures/Fibers.
Showing
2 changed files
with
6 additions
and
29 deletions
Show diff stats
client/controllers/public/signup.coffee
1 | ### | 1 | ### |
2 | Controller: Signup | 2 | Controller: Signup |
3 | Template: /client/views/public/signup.html | 3 | Template: /client/views/public/signup.html |
4 | ### | 4 | ### |
5 | 5 | ||
6 | # Created | 6 | # Created |
7 | Template.signup.created = -> | 7 | Template.signup.created = -> |
8 | # Code to run when template is created goes here. | 8 | # Code to run when template is created goes here. |
9 | 9 | ||
10 | # Rendered | 10 | # Rendered |
11 | Template.signup.rendered = -> | 11 | Template.signup.rendered = -> |
12 | # Code to run when template is rendered goes here. | 12 | # Code to run when template is rendered goes here. |
13 | 13 | ||
14 | # Helpers | 14 | # Helpers |
15 | Template.signup.helpers( | 15 | Template.signup.helpers( |
16 | example: -> | 16 | example: -> |
17 | # Code to run for helper function. | 17 | # Code to run for helper function. |
18 | ) | 18 | ) |
19 | 19 | ||
20 | # Events | 20 | # Events |
21 | Template.signup.events( | 21 | Template.signup.events( |
22 | 'submit form': (e,t) -> | 22 | 'submit form': (e,t) -> |
23 | 23 | ||
24 | # Prevent form from submitting. | 24 | # Prevent form from submitting. |
25 | e.preventDefault() | 25 | e.preventDefault() |
26 | 26 | ||
27 | # Grab the user's details. | 27 | # Grab the user's details. |
28 | user = | 28 | user = |
29 | email: t.find('[name="emailAddress"]').value | 29 | email: t.find('[name="emailAddress"]').value |
30 | password: t.find('[name="password"]').value | 30 | password: t.find('[name="password"]').value |
31 | 31 | ||
32 | # Create the user's account. | 32 | # Create the user's account. |
33 | Meteor.call 'createUserAccount', user, (error,response) -> | 33 | Meteor.call 'createUserAccount', user, (error) -> |
34 | 34 | ||
35 | # If the account is created successfully, log the user in using the credentials | 35 | # If the account is created successfully, log the user in using the credentials |
36 | # from above. | 36 | # from above. |
37 | if response.success | 37 | if error |
38 | alert error.reason | ||
39 | else | ||
38 | Meteor.loginWithPassword(user.email, user.password, (error)-> | 40 | Meteor.loginWithPassword(user.email, user.password, (error)-> |
39 | alert error.reason if error | 41 | alert error.reason if error |
40 | ) | 42 | ) |
41 | 43 | ||
42 | ) | 44 | ) |
43 | 45 |
server/admin/accounts.coffee
1 | ### | 1 | ### |
2 | Accounts | 2 | Accounts |
3 | Server side account creation and manipulation methods. | 3 | Server side account creation and manipulation methods. |
4 | 4 | ||
5 | NPM Requires: | ||
6 | - Fibers (https://www.npmjs.org/package/fibers) | ||
7 | - Futures (https://www.npmjs.org/package/fibers) | ||
8 | |||
9 | Configuration: | 5 | Configuration: |
10 | - forbidClientAccountCreation: Disallow client side account creation. | 6 | - forbidClientAccountCreation: Disallow client side account creation. |
11 | 7 | ||
12 | Methods: | 8 | Methods: |
13 | - createUserAccount: Performs a server-side account creation using the Meteor Accounts Password package. | 9 | - createUserAccount: Performs a server-side account creation using the Meteor Accounts Password package. |
14 | ### | 10 | ### |
15 | 11 | ||
16 | # NPM Requires | ||
17 | Future = Npm.require('fibers/future'); | ||
18 | Fibers = Npm.require('fibers'); | ||
19 | |||
20 | # Configuration: | 12 | # Configuration: |
21 | Accounts.config( | 13 | Accounts.config( |
22 | forbidClientAccountCreation: true | 14 | forbidClientAccountCreation: true |
23 | ) | 15 | ) |
24 | 16 | ||
25 | # Define Methods | 17 | # Define Methods |
26 | Meteor.methods( | 18 | Meteor.methods( |
27 | 19 | ||
28 | createUserAccount: (user)-> | 20 | createUserAccount: (user)-> |
29 | 21 | ||
30 | # Check values against correct pattern. | 22 | # Check values against correct pattern. |
31 | pattern = { email: String, password: String } | 23 | pattern = { email: String, password: String } |
32 | check(user, pattern) | 24 | check(user, pattern) |
33 | 25 | ||
34 | # Define the Future so we can return to the client later. | 26 | # Create the user. |
35 | createUserAccount = new Future() | 27 | Accounts.createUser(user) |
36 | |||
37 | # Run the Timeout function and create the user. | ||
38 | setTimeout(-> | ||
39 | |||
40 | # Run Meteor code in a Fiber. | ||
41 | # Note: Accounts.createUser does NOT allow a callback on the server, so we currently | ||
42 | # cannot check to see if an error took place. | ||
43 | Fibers(-> | ||
44 | Accounts.createUser(user) | ||
45 | # Send a faux response back to the client. | ||
46 | createUserAccount.return({"success": "Account successfully created!"}) | ||
47 | ).run() | ||
48 | |||
49 | ,300) | ||
50 | |||
51 | # Wait for the process to complete. | ||
52 | createUserAccount.wait() | ||
53 | ) | 28 | ) |
54 | 29 |