1
0
mirror of https://github.com/fazo96/homework.git synced 2025-01-10 12:14:22 +01:00

must confirm emails to use app now.

This commit is contained in:
fazo96 2014-05-28 18:45:41 +02:00
parent 31894750cd
commit ccb9ca92bc
5 changed files with 92 additions and 21 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
run.sh

View File

@ -1,16 +1,22 @@
# Homework - Client Side # Homework - Client Side
notes = new Meteor.Collection "notes" notes = new Meteor.Collection "notes"
Deps.autorun -> Meteor.subscribe "my-notes" unless not Meteor.userId() Deps.autorun -> Meteor.subscribe "my-notes" unless not Meteor.userId()
user = -> Meteor.user() validateEmail = (email) ->
# Loading (Spinning Cog) expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
expr.test email
getUser = -> Meteor.user()
amIValid = ->
return no unless getUser()
return yes for mail in getUser().emails when mail.verified is yes; no
# Helpers
UI.registerHelper "loggingIn", -> Meteor.loggingIn() UI.registerHelper "loggingIn", -> Meteor.loggingIn()
UI.registerHelper "mail", -> getUser().emails[0].address
UI.registerHelper "verified", -> amIValid()
# User Interface # User Interface
Template.userInfo.events Template.userInfo.events
'click #logout': (e,template) -> Meteor.logout() 'click #logout': (e,template) -> Meteor.logout()
Template.userInfo.in = -> Meteor.user().emails[0].address
# Notes template # Notes template
Template.notes.truncateNoteDesc = (s) -> s Template.notes.truncateNoteDesc = (s) -> s
#if s.length > 52 then s.slice(0,48)+"..." else s #if s.length > 52 then s.slice(0,48)+"..." else s
@ -46,7 +52,10 @@ Template.editor.events
# Notifications # Notifications
alerts = [] alerts = []
alertDep = new Deps.Dependency alertDep = new Deps.Dependency
errCallback = (err) -> showError msg: err.reason errCallback = (err) ->
if err.reason
showError msg: err.reason
else showErrror msg: err
# Show a notification # Show a notification
notify = (data) -> notify = (data) ->
alerts.push alerts.push
@ -75,10 +84,20 @@ Template.error.events 'click .close': -> clearError()
# "Loading" template # "Loading" template
Template.loading.status = -> Meteor.status() Template.loading.status = -> Meteor.status()
# Verify Email
Template.verifyEmail.events
'click #btn-verify': (e,template) ->
Accounts.verifyEmail template.find('#token-field').value, errCallback
'click #btn-resend': ->
Meteor.call 'resendConfirmEmail', errCallback
'click #btn-delete': -> Meteor.call 'deleteMe'
'click #btn-logout': -> Meteor.logout()
# Login and Register # Login and Register
pressLogin = (template) -> pressLogin = (template) ->
mail = template.find('#mail').value; pass = template.find('#pass').value mail = template.find('#mail').value; pass = template.find('#pass').value
Meteor.loginWithPassword mail, pass, (err) -> errCallback err Meteor.loginWithPassword mail, pass, errCallback
Template.auth.events Template.auth.events
# Login # Login
'keypress .login': (e,template) -> if e.keyCode is 13 then pressLogin template 'keypress .login': (e,template) -> if e.keyCode is 13 then pressLogin template
@ -97,6 +116,6 @@ Template.auth.events
Accounts.createUser { Accounts.createUser {
email: mail, email: mail,
password: pass password: pass
}, (e) -> errCallback e }, errCallback
catch err catch err
showError msg: err showError msg: err

View File

@ -11,8 +11,13 @@
</h1> </h1>
</div> </div>
<div class="center-block" id="ui-container"> <div class="center-block" id="ui-container">
{{> error}}
{{#if currentUser}} {{#if currentUser}}
{{#if verified}}
{{> editor}} {{> notes}} {{> userInfo}} {{> editor}} {{> notes}} {{> userInfo}}
{{else}}
{{> verifyEmail}}
{{/if}}
{{else}} {{else}}
{{> auth}} {{> auth}}
{{/if}} {{/if}}
@ -43,7 +48,6 @@
<template name="auth"> <template name="auth">
<div align="center"> <div align="center">
<p>Register a new Account or login</p> <p>Register a new Account or login</p>
{{> error}}
{{#if loggingIn}} {{#if loggingIn}}
{{> loading}} {{> loading}}
{{else}} {{else}}
@ -53,8 +57,8 @@
{{/if}} {{/if}}
<p>Password must be at least 8 characters. Email must be a valid email</p> <p>Password must be at least 8 characters. Email must be a valid email</p>
{{#unless working}} {{#unless working}}
<button type="button" id="register" class="btn-auth btn btn-info">Register</button> <button type="button" id="register" class="btn-auth btn btn-success">Register</button>
<button type="button" id="login" class="btn-auth btn btn-info">Login</button> <button type="button" id="login" class="btn-auth btn btn-primary">Login</button>
{{/unless}} {{/unless}}
</div> </div>
</template> </template>
@ -62,11 +66,24 @@
<template name="userInfo"><hr> <template name="userInfo"><hr>
{{#if loggingIn}} {{> loading}} {{/if}} {{#if loggingIn}} {{> loading}} {{/if}}
<div align="center"> <div align="center">
<p>{{in}}</p> <p>{{email}}</p>
<button type="button" id="logout" class="btn btn-danger">Logout</button> <button type="button" id="logout" class="btn btn-danger">Logout</button>
</div> </div>
</template> </template>
<template name="verifyEmail">
<div align="center">
<p>Insert the token you received by email at the address {{mail}} here:</p>
<input type="text" id="token-field" class="form-control" placeholder="Token">
<div align="center" class="buttons">
<button type="button" class="btn btn-warning btn-ver" id="btn-resend">Resend Email</button>
<button type="button" class="btn btn-success btn-ver" id="btn-verify">Verify Email</button>
<button type="button" class="btn btn-danger btn-ver" id="btn-delete">Delete Account</button>
<br><button type="button" class="btn btn-primary btn-ver" id="btn-logout">Logout</button>
</div>
</div>
</template>
<template name="editor"> <template name="editor">
<div align="center">{{> error }}</div> <div align="center">{{> error }}</div>
{{#if note}} {{#if note}}
@ -98,10 +115,10 @@
<template name="error"> <template name="error">
{{#if error}} {{#if error}}
<div class="alert alert-{{error.type}} error"> <div align="center"><div class="alert alert-{{error.type}} error">
<p align="center">{{error.msg}}</p> <p align="center">{{error.msg}}</p>
<button type="button" class="close close-error">&times;</button> <button type="button" class="close close-error">&times;</button>
</div> </div></div>
{{/if}} {{/if}}
</template> </template>

View File

@ -52,11 +52,11 @@ input {
} }
.editor-title { width: 70%; } .editor-title { width: 70%; }
@media (max-width: 400px) { @media (max-width: 400px) {
.editor-title { width: 65%; } .editor-title { width: 65%; }
} }
.close-editor { .close-editor {
float: right; float: right;
margin-top: -28px; margin-top: -28px;
@ -71,6 +71,11 @@ input {
width: 100px; width: 100px;
} }
.btn-ver {
width:150px;
margin-top:20px;
}
.close-notification, .close-error { .close-notification, .close-error {
float: right; float: right;
margin-top: -22px; margin-top: -22px;

View File

@ -1,22 +1,34 @@
# Homework - Server Side # Homework - Server Side
console.log "Started Homework server!"
console.log "Sending emails using "+process.env.MAIL_URL
notes = new Meteor.Collection "notes" notes = new Meteor.Collection "notes"
getUser = (id) -> Meteor.users.findOne { _id: id }
validateEmail = (email) -> validateEmail = (email) ->
expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/ expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
expr.test email expr.test email
Accounts.config { Accounts.config {
sendVerificationEmail: false sendVerificationEmail: true
loginExpirationInDays: 1 loginExpirationInDays: 1
} }
Accounts.emailTemplates.siteName = "Homework App";
Accounts.emailTemplates.verifyEmail.text = (user,url) ->
token = url.split('/'); token = token[token.length-1]
'''Welcome to Homework! To activate your account, log in then provide the
following token: '''+token
# Returns true if the user has verified at least one email address # Returns true if the user has verified at least one email address
userValidated = (user) -> userValidated = (user) ->
if not user?
throw new Meteor.Exception "Impossible! Trying to validate null user"
return yes for mail in user.emails when mail.verified is yes; no return yes for mail in user.emails when mail.verified is yes; no
Meteor.publish "my-notes", -> Meteor.publish "my-notes", ->
# TODO: Don't publish unless user is validated if userValidated getUser(@userId)
notes.find( { userId: @userId } ) unless not @userId notes.find userId: @userId
# Authentication # Authentication
Accounts.validateNewUser (user) -> Accounts.validateNewUser (user) ->
@ -28,6 +40,23 @@ Accounts.validateNewUser (user) ->
# Methods that the clients can invoke # Methods that the clients can invoke
Meteor.methods Meteor.methods
amIValidated: -> amIValidated: ->
user = Meteor.users.findOne { _id: @userId } u = getUser(@userId)
return no unless user? return no unless u?
userValidated user userValidated u
resendConfirmEmail: ->
u = getUser(@userId)
if not u
console.log "Validating nonexisting user!!"; return no
if userValidated(u) is no
Accounts.sendVerificationEmail @userId
console.log "Sent verification email to "+u.emails[0].address
return yes
else
console.log "User "+u.emails[0].address+" already validated."
return no
deleteMe: ->
if @userId
Meteor.users.remove @userId
# Automagically log out the user by invalidating every token he has
Meteor.users.update {_id: @userId},
{$set : { "resume.loginTokens" : [] } }, { multi: yes }