From 7439c59f5fd11d1e89fd283b2e3bfc19f3d9619f Mon Sep 17 00:00:00 2001 From: Ryan Glover Date: Wed, 30 Jul 2014 11:18:17 -0500 Subject: [PATCH] Add in a server-only account creation pattern. Add an index route for once the user is authenticated (with a template). Update file tree in README. Add an NPM packages package to import NPM modules. --- README.md | 1 + client/controllers/public/login.coffee | 8 ++--- client/controllers/public/signup.coffee | 18 +++++++---- client/routes/routes-authenticated.coffee | 15 ++++----- client/views/authenticated/index.html | 3 ++ packages/npm-packages/package.js | 7 ++++ server/admin/accounts.coffee | 53 +++++++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 client/views/authenticated/index.html create mode 100644 packages/npm-packages/package.js create mode 100644 server/admin/accounts.coffee diff --git a/README.md b/README.md index b9d37d1..bee811c 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Base comes with a pre-defined file structure common to all projects along with s ---/public ---/server ------/admin +---------accounts.coffee ---------startup.coffee ------/publications ---------example.coffee diff --git a/client/controllers/public/login.coffee b/client/controllers/public/login.coffee index c27a125..307a524 100644 --- a/client/controllers/public/login.coffee +++ b/client/controllers/public/login.coffee @@ -19,17 +19,17 @@ Template.login.helpers( # Events Template.login.events( - 'submit form': (e) -> + 'submit form': (e,t) -> # Prevent form from submitting. e.preventDefault() # Grab the user's details. user = - email: $('[name="emailAddress"]').val() - password: $('[name="password"]').val() + email: t.find('[name="emailAddress"]').value + password: t.find('[name="password"]').value - # Create the user's account. + # Log the user in. Meteor.loginWithPassword(user.email, user.password, (error)-> alert error.reason if error ) diff --git a/client/controllers/public/signup.coffee b/client/controllers/public/signup.coffee index f6a169a..b306ad8 100644 --- a/client/controllers/public/signup.coffee +++ b/client/controllers/public/signup.coffee @@ -19,18 +19,24 @@ Template.signup.helpers( # Events Template.signup.events( - 'submit form': (e) -> + 'submit form': (e,t) -> # Prevent form from submitting. e.preventDefault() # Grab the user's details. user = - email: $('[name="emailAddress"]').val() - password: $('[name="password"]').val() + email: t.find('[name="emailAddress"]').value + password: t.find('[name="password"]').value # Create the user's account. - Accounts.createUser(user, (error)-> - alert error.reason if error - ) + Meteor.call 'createUserAccount', user, (error,response) -> + + # If the account is created successfully, log the user in using the credentials + # from above. + if response.success + Meteor.loginWithPassword(user.email, user.password, (error)-> + alert error.reason if error + ) + ) diff --git a/client/routes/routes-authenticated.coffee b/client/routes/routes-authenticated.coffee index 2eb4c7f..8f10c55 100644 --- a/client/routes/routes-authenticated.coffee +++ b/client/routes/routes-authenticated.coffee @@ -1,11 +1,8 @@ Router.map(-> - ### - @route('exampleAuthenticatedRoute', - path: '/example' - template: 'example' - layoutTemplate: 'customLayoutExample' - onBeforeAction: -> - # Code to run before route goes here. - ) - ### + @route('index', + path: '/' + template: 'index' + onBeforeAction: -> + # Code to run before route goes here. + ) ) diff --git a/client/views/authenticated/index.html b/client/views/authenticated/index.html new file mode 100644 index 0000000..46f1fa8 --- /dev/null +++ b/client/views/authenticated/index.html @@ -0,0 +1,3 @@ + diff --git a/packages/npm-packages/package.js b/packages/npm-packages/package.js new file mode 100644 index 0000000..b4d43cb --- /dev/null +++ b/packages/npm-packages/package.js @@ -0,0 +1,7 @@ +Package.describe({ + summary: "Give Meteor access to NPM modules for our application." +}); + +Npm.depends({ + "fibers": "1.0.1" +}); diff --git a/server/admin/accounts.coffee b/server/admin/accounts.coffee new file mode 100644 index 0000000..c4144cf --- /dev/null +++ b/server/admin/accounts.coffee @@ -0,0 +1,53 @@ +### + Accounts + Server side account creation and manipulation methods. + + NPM Requires: + - Fibers (https://www.npmjs.org/package/fibers) + - Futures (https://www.npmjs.org/package/fibers) + + Configuration: + - forbidClientAccountCreation: Disallow client side account creation. + + Methods: + - createUserAccount: Performs a server-side account creation using the Meteor Accounts Password package. +### + +# NPM Requires +Future = Npm.require('fibers/future'); +Fibers = Npm.require('fibers'); + +# Configuration: +Accounts.config( + forbidClientAccountCreation: true +) + +# Define Methods +Meteor.methods( + + createUserAccount: (user)-> + + # Check values against correct pattern. + pattern = { email: String, password: String } + check(user, pattern) + + # Define the Future so we can return to the client later. + createUserAccount = new Future() + + # Run the Timeout function and create the user. + setTimeout(-> + + # Run Meteor code in a Fiber. + # Note: Accounts.createUser does NOT allow a callback on the server, so we currently + # cannot check to see if an error took place. + Fibers(-> + Accounts.createUser(user) + # Send a faux response back to the client. + createUserAccount.return({"success": "Account successfully created!"}) + ).run() + + ,300) + + # Wait for the process to complete. + createUserAccount.wait() +) -- 2.0.0