1
0
mirror of https://github.com/fazo96/homework.git synced 2025-01-25 14:34:20 +01:00
homework/server/server.coffee

68 lines
2.1 KiB
CoffeeScript
Raw Normal View History

# Homework - Server Side
2014-05-30 10:33:32 +02:00
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
2014-05-28 18:45:41 +02:00
console.log "Started Homework server!"
2014-05-30 08:27:49 +02:00
if process.env.MAIL_URL
console.log "Sending emails using "+process.env.MAIL_URL
else
console.log "Not Sending Emails, please set the MAIL_URL environment variable"
2014-05-28 18:45:41 +02:00
notes = new Meteor.Collection "notes"
2014-05-28 18:45:41 +02:00
getUser = (id) -> Meteor.users.findOne { _id: id }
2014-05-27 16:02:37 +02:00
Accounts.config {
2014-05-28 18:45:41 +02:00
sendVerificationEmail: true
loginExpirationInDays: 1
}
2014-05-28 18:45:41 +02:00
Accounts.emailTemplates.siteName = "Homework App";
Accounts.emailTemplates.verifyEmail.text = (user,url) ->
2014-05-30 11:40:15 +02:00
urlist = url.split('/'); token = urlist[urlist.length-1]
'''Welcome to Homework! To activate your account, click on the \
following link: http://homework.meteor.com/verify/'''+token
2014-05-28 18:45:41 +02:00
2014-05-28 10:19:06 +02:00
# Returns true if the user has verified at least one email address
userValidated = (user) ->
2014-05-28 18:45:41 +02:00
if not user?
2014-05-30 08:27:49 +02:00
console.log "Impossible! Trying to validate null user"
return no
2014-05-28 10:19:06 +02:00
return yes for mail in user.emails when mail.verified is yes; no
2014-05-30 08:27:49 +02:00
# Publish user's notes to each user.
Meteor.publish "my-notes", ->
2014-05-28 18:45:41 +02:00
if userValidated getUser(@userId)
notes.find userId: @userId
# Authentication
Accounts.validateNewUser (user) ->
2014-05-27 16:02:37 +02:00
mail = user.emails[0].address
if Match.test(mail,String) is no or validateEmail(mail) is no
throw new Meteor.Error 403, "Invalid Email"
return yes
2014-05-28 10:19:06 +02:00
# Methods that the clients can invoke
Meteor.methods
2014-05-28 18:45:41 +02:00
resendConfirmEmail: ->
u = getUser(@userId)
if not u
2014-05-30 10:33:32 +02:00
console.log "Validating nonexisting user!"; return no
2014-05-28 18:45:41 +02:00
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 }
2014-05-30 10:33:32 +02:00
return yes
else no