From 6b42e24e57be21497d2594ea2f21c24bf4c0b6d2 Mon Sep 17 00:00:00 2001 From: fazo96 Date: Sat, 31 May 2014 18:09:56 +0200 Subject: [PATCH] improved UI, fixed #1, fixed #6 --- client/client.coffee | 119 ++++++++++++++++++++++--------------- client/view/style.css | 19 ++++-- client/view/templates.html | 112 ++++++++++++++++++++-------------- server/accounts.coffee | 22 +++++++ server/server.coffee | 24 +------- 5 files changed, 178 insertions(+), 118 deletions(-) create mode 100644 server/accounts.coffee diff --git a/client/client.coffee b/client/client.coffee index 4da1b4d..769caee 100644 --- a/client/client.coffee +++ b/client/client.coffee @@ -1,28 +1,47 @@ # Homework - Client Side + +# Variables and utility stuff notes = new Meteor.Collection "notes" getUser = -> Meteor.user() -myNotes = -> Meteor.subscribe "my-notes" +deleteAccount = -> + Meteor.call 'deleteMe', (r) -> if r is yes then Router.go 'home' amIValid = -> return no unless getUser() return yes for mail in getUser().emails when mail.verified is yes; no -# Common Helpers +# Common Helpers for the Templates UI.registerHelper "loggingIn", -> Meteor.loggingIn() UI.registerHelper "email", -> getUser().emails[0].address UI.registerHelper "verified", -> amIValid() # Router +### +Important: before rendering and routing, always "dispatch" the user to 'home' +if he doesn't have the permission to access the current route. 'home' then +dispatches the user to the correct landing page. +Routes are client side, but even if by hacking the client you can access pages +without being logged in, it's impossible to inteact with data because +the server checks all the things before providing the data. It's safe. +### Router.configure layoutTemplate: 'layout' loadingTemplate: 'loading' notFoundTemplate: '404' Router.map -> @route 'home', - onBeforeAction: (pause)-> + onBeforeAction: -> + # Dispatch user to the right landing page based on his account status if getUser() if amIValid() is yes then Router.go 'notes' else Router.go 'verifyEmail' + else Router.go 'login' path: '/' - template: 'auth' + @route 'login', + onBeforeAction: -> Router.go 'home' if getUser() + @route 'register', + onBeforeAction: -> Router.go 'home' if getUser() + @route 'account', + onBeforeAction: -> + if not getUser() then Router.go 'home' @route 'notes', waitOn: -> Meteor.subscribe "my-notes" onBeforeAction: -> @@ -38,21 +57,23 @@ Router.map -> onBeforeAction: -> if @params.token? Accounts.verifyEmail @params.token, (err) -> - if err - Router.go 'home' - errCallback err - else Router.go 'notes' - else if not getUser() - Router.go 'home' - else if amIValid() is yes then Router.go 'notes' + if err then errCallback err else Router.go 'home' @route '404', path: '*' -logoutCallback = (err) -> if err then errCallback err else Router.go 'home' - # Client Templates + +# Some utilities +logoutCallback = (err) -> if err then errCallback err else Router.go 'home' +errCallback = (err) -> + if err.reason + showError msg: err.reason + else showErrror msg: err + # Menu -Template.menu.at_home = -> - if Router.current() then return "active" if Router.current().path is "/notes" +Template.menu.events + 'click .go-home': -> Router.go 'home' + 'click .go-account': -> Router.go 'account' + 'click .go-archive': -> Router.go 'archive' # User Interface Template.account.events @@ -76,7 +97,7 @@ Template.notelist.events # Note Editor Template.editor.note = -> Router.current.data() # Only when we're in /note/:_id saveCurrentNote = (t,e) -> - if e and e.keyCode isnt 13 then return; + if e and e.keyCode isnt 13 then return notes.update Router.current().data()._id, $set: title: t.find('.editor-title').value @@ -89,10 +110,6 @@ Template.editor.events # Notifications alerts = [] alertDep = new Deps.Dependency -errCallback = (err) -> - if err.reason - showError msg: err.reason - else showErrror msg: err # Show a notification notify = (data) -> alerts.push @@ -128,33 +145,41 @@ Template.verifyEmail.events if err errCallback err else showError { type:"success", msg: "Confirmation email sent" } - 'click #btn-delete': -> - Meteor.call 'deleteMe', (r) -> if r is yes then Router.go 'home' + 'click #btn-delete': -> deleteAccount() 'click #btn-logout': -> Meteor.logout logoutCallback -# Login and Register -pressLogin = (template) -> - mail = template.find('#mail').value; pass = template.find('#pass').value - Meteor.loginWithPassword mail, pass, errCallback +# Login +loginRequest = (e,template) -> + if e and e.keyCode isnt 13 then return + mail = template.find('#l-mail').value; pass = template.find('#l-pass').value + Meteor.loginWithPassword mail, pass, (err) -> + if err then errCallback err else Router.go 'home' -Template.auth.events - # Login - 'keypress .login': (e,template) -> if e.keyCode is 13 then pressLogin template - 'click #login': (e,template) -> pressLogin template - # Register - 'click #register': (e,template) -> - mail = template.find('#mail').value; pass = template.find('#pass').value - if not mail - showError msg: "Please enter an Email" - else if not pass - showError msg: "Please enter a password" - else if pass.length < 8 - showError msg: "Password too short" - else # Sending actual registration request - try - Accounts.createUser { - email: mail, - password: pass - }, (err) -> if err then errCallback err else Router.go 'confirmEmail' - catch err - showError msg: err +Template.login.events + 'keypress .login': (e,template) -> loginRequest e,template + 'click #login-btn': (e,template) -> loginRequest null,template + +# Register +registerRequest = (e,template) -> + if e and e.keyCode isnt 13 then return + mail = template.find('#r-mail').value; pass = template.find('#r-pass').value + pass2 = template.find('#r-pass-2').value + if not mail + showError msg: "Please enter an Email" + else if not pass + showError msg: "Please enter a password" + else if pass.length < 8 + showError msg: "Password too short" + else if pass2 isnt pass + showError msg: "The passwords don't match" + else # Sending actual registration request + try + Accounts.createUser { + email: mail, + password: pass + }, (err) -> if err then errCallback err else Router.go 'confirmEmail' + catch err + showError msg: err +Template.register.events + 'click #register-btn': (e,t) -> registerRequest null,t + 'keypress .register': (e,t) -> registerRequest e,t diff --git a/client/view/style.css b/client/view/style.css index 02fb03c..940336f 100644 --- a/client/view/style.css +++ b/client/view/style.css @@ -1,6 +1,7 @@ /* Generics and bootstrap classes */ input { text-align:center; + max-width: 400px; } .page-header { @@ -15,10 +16,22 @@ input { .custom-link { color: #999; } .custom-link:hover { color: #101010;} +.cool-header { + margin-bottom: 22px; +} + +.btns-account { + margin-top: 20px; +} + .menu-item { margin-right: 100px; margin-left:100px; } +.in-bt { + margin-bottom: 10px; +} + .note { padding-bottom: 5px; } @@ -71,7 +84,7 @@ input { margin-top: 10px; } -.btn-auth { +.btn-fix { width: 100px; } @@ -106,7 +119,3 @@ input { max-width: 250px; margin-bottom: 10px; } - -#mail { - margin-bottom: 10px; -} diff --git a/client/view/templates.html b/client/view/templates.html index c8096d0..03bbe5b 100644 --- a/client/view/templates.html +++ b/client/view/templates.html @@ -1,4 +1,5 @@ + - - - + + -