mirror of
https://github.com/fazo96/homework.git
synced 2025-01-09 12:10:08 +01:00
must confirm emails to use app now.
This commit is contained in:
parent
31894750cd
commit
ccb9ca92bc
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
run.sh
|
@ -1,16 +1,22 @@
|
||||
# Homework - Client Side
|
||||
notes = new Meteor.Collection "notes"
|
||||
Deps.autorun -> Meteor.subscribe "my-notes" unless not Meteor.userId()
|
||||
user = -> Meteor.user()
|
||||
# Loading (Spinning Cog)
|
||||
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.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 "mail", -> getUser().emails[0].address
|
||||
UI.registerHelper "verified", -> amIValid()
|
||||
|
||||
# User Interface
|
||||
Template.userInfo.events
|
||||
'click #logout': (e,template) -> Meteor.logout()
|
||||
|
||||
Template.userInfo.in = -> Meteor.user().emails[0].address
|
||||
|
||||
# Notes template
|
||||
Template.notes.truncateNoteDesc = (s) -> s
|
||||
#if s.length > 52 then s.slice(0,48)+"..." else s
|
||||
@ -46,7 +52,10 @@ Template.editor.events
|
||||
# Notifications
|
||||
alerts = []
|
||||
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
|
||||
notify = (data) ->
|
||||
alerts.push
|
||||
@ -75,10 +84,20 @@ Template.error.events 'click .close': -> clearError()
|
||||
# "Loading" template
|
||||
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
|
||||
pressLogin = (template) ->
|
||||
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
|
||||
# Login
|
||||
'keypress .login': (e,template) -> if e.keyCode is 13 then pressLogin template
|
||||
@ -97,6 +116,6 @@ Template.auth.events
|
||||
Accounts.createUser {
|
||||
email: mail,
|
||||
password: pass
|
||||
}, (e) -> errCallback e
|
||||
}, errCallback
|
||||
catch err
|
||||
showError msg: err
|
||||
|
@ -11,8 +11,13 @@
|
||||
</h1>
|
||||
</div>
|
||||
<div class="center-block" id="ui-container">
|
||||
{{> error}}
|
||||
{{#if currentUser}}
|
||||
{{> editor}} {{> notes}} {{> userInfo}}
|
||||
{{#if verified}}
|
||||
{{> editor}} {{> notes}} {{> userInfo}}
|
||||
{{else}}
|
||||
{{> verifyEmail}}
|
||||
{{/if}}
|
||||
{{else}}
|
||||
{{> auth}}
|
||||
{{/if}}
|
||||
@ -43,7 +48,6 @@
|
||||
<template name="auth">
|
||||
<div align="center">
|
||||
<p>Register a new Account or login</p>
|
||||
{{> error}}
|
||||
{{#if loggingIn}}
|
||||
{{> loading}}
|
||||
{{else}}
|
||||
@ -53,8 +57,8 @@
|
||||
{{/if}}
|
||||
<p>Password must be at least 8 characters. Email must be a valid email</p>
|
||||
{{#unless working}}
|
||||
<button type="button" id="register" class="btn-auth btn btn-info">Register</button>
|
||||
<button type="button" id="login" class="btn-auth btn btn-info">Login</button>
|
||||
<button type="button" id="register" class="btn-auth btn btn-success">Register</button>
|
||||
<button type="button" id="login" class="btn-auth btn btn-primary">Login</button>
|
||||
{{/unless}}
|
||||
</div>
|
||||
</template>
|
||||
@ -62,11 +66,24 @@
|
||||
<template name="userInfo"><hr>
|
||||
{{#if loggingIn}} {{> loading}} {{/if}}
|
||||
<div align="center">
|
||||
<p>{{in}}</p>
|
||||
<p>{{email}}</p>
|
||||
<button type="button" id="logout" class="btn btn-danger">Logout</button>
|
||||
</div>
|
||||
</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">
|
||||
<div align="center">{{> error }}</div>
|
||||
{{#if note}}
|
||||
@ -98,10 +115,10 @@
|
||||
|
||||
<template name="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>
|
||||
<button type="button" class="close close-error">×</button>
|
||||
</div>
|
||||
</div></div>
|
||||
{{/if}}
|
||||
</template>
|
||||
|
||||
|
@ -52,11 +52,11 @@ input {
|
||||
}
|
||||
|
||||
.editor-title { width: 70%; }
|
||||
|
||||
@media (max-width: 400px) {
|
||||
.editor-title { width: 65%; }
|
||||
}
|
||||
|
||||
|
||||
.close-editor {
|
||||
float: right;
|
||||
margin-top: -28px;
|
||||
@ -71,6 +71,11 @@ input {
|
||||
width: 100px;
|
||||
}
|
||||
|
||||
.btn-ver {
|
||||
width:150px;
|
||||
margin-top:20px;
|
||||
}
|
||||
|
||||
.close-notification, .close-error {
|
||||
float: right;
|
||||
margin-top: -22px;
|
||||
|
@ -1,22 +1,34 @@
|
||||
# Homework - Server Side
|
||||
console.log "Started Homework server!"
|
||||
console.log "Sending emails using "+process.env.MAIL_URL
|
||||
|
||||
notes = new Meteor.Collection "notes"
|
||||
|
||||
getUser = (id) -> Meteor.users.findOne { _id: id }
|
||||
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.test email
|
||||
|
||||
Accounts.config {
|
||||
sendVerificationEmail: false
|
||||
sendVerificationEmail: true
|
||||
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
|
||||
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
|
||||
|
||||
Meteor.publish "my-notes", ->
|
||||
# TODO: Don't publish unless user is validated
|
||||
notes.find( { userId: @userId } ) unless not @userId
|
||||
if userValidated getUser(@userId)
|
||||
notes.find userId: @userId
|
||||
|
||||
# Authentication
|
||||
Accounts.validateNewUser (user) ->
|
||||
@ -28,6 +40,23 @@ Accounts.validateNewUser (user) ->
|
||||
# Methods that the clients can invoke
|
||||
Meteor.methods
|
||||
amIValidated: ->
|
||||
user = Meteor.users.findOne { _id: @userId }
|
||||
return no unless user?
|
||||
userValidated user
|
||||
u = getUser(@userId)
|
||||
return no unless u?
|
||||
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 }
|
||||
|
Loading…
Reference in New Issue
Block a user