mirror of
https://github.com/fazo96/homework.git
synced 2025-01-09 12:10:08 +01:00
added authentication
This commit is contained in:
parent
54be536454
commit
50c529d027
49
app.coffee
49
app.coffee
@ -2,19 +2,64 @@ todos = new Meteor.Collection "todos"
|
||||
|
||||
if Meteor.isServer
|
||||
#todos.insert { content: "Example" } unless todos.find().fetch().length > 0
|
||||
Meteor.publish "todos", -> todos.find()
|
||||
# Accounts
|
||||
###
|
||||
Accounts.registerLoginHandler (req) ->
|
||||
return null unless req.mail and req.password
|
||||
return null unless req.mail.length > 4 and req.pass.length >= 8
|
||||
user = Meteor.users.findOne { mail: req.mail }
|
||||
if not user
|
||||
user = Meteor.insert { mail: req.mail, password: req.password }
|
||||
{ id: user._id }
|
||||
###
|
||||
|
||||
Accounts.config {
|
||||
sendVerificationEmail: true
|
||||
loginExpirationInDays: 1
|
||||
}
|
||||
|
||||
Meteor.publish "todos", ->
|
||||
todos.find( { userId: @userId } )
|
||||
|
||||
if Meteor.isClient
|
||||
Meteor.subscribe "todos"
|
||||
# Notes template
|
||||
Template.notes.notes = ->
|
||||
todos.find().fetch()
|
||||
Template.notes.events {
|
||||
'click .delete': ->
|
||||
todos.remove @_id
|
||||
}
|
||||
# Template for new notes
|
||||
Template.adder.events {
|
||||
'keypress #newNote': (e,template) ->
|
||||
if e.keyCode is 13
|
||||
todos.insert { content: template.find('#newNote').value }
|
||||
console.log Meteor.userId()
|
||||
todos.insert {
|
||||
content: template.find('#newNote').value
|
||||
userId: Meteor.userId()
|
||||
}
|
||||
template.find('#newNote').value = ""
|
||||
}
|
||||
# Auth template
|
||||
Template.auth.events {
|
||||
'keypress .login': (e,template) ->
|
||||
if e.keyCode is 13
|
||||
# Login
|
||||
mail = template.find('#mail').value; pass = template.find('#pass').value
|
||||
Accounts.loginWithPassword mail, pass, (err) ->
|
||||
if err then console.log err else console.log "OK"
|
||||
'click #login': (e,template) ->
|
||||
mail = template.find('#mail').value; pass = template.find('#pass').value
|
||||
Meteor.loginWithPassword mail, pass, (err) ->
|
||||
if err then console.log err else console.log "OK"
|
||||
'click #register': (e,template) ->
|
||||
mail = template.find('#mail').value; pass = template.find('#pass').value
|
||||
Accounts.createUser { email: mail, password: pass }, (err) ->
|
||||
if err then console.log err else console.log "OK"
|
||||
}
|
||||
# User Logged In
|
||||
Template.userInfo.events {
|
||||
'click #logout': (e,template) ->
|
||||
Meteor.logout()
|
||||
}
|
||||
|
22
index.html
22
index.html
@ -4,8 +4,11 @@
|
||||
<h1>Homework <small>management for students</small></h1>
|
||||
</div>
|
||||
<div class="center-block" id="quicknotes">
|
||||
{{> notes}}
|
||||
<div align="center"> {{> adder}} </div>
|
||||
{{> notes}} {{#if currentUser}}
|
||||
<div align="center">
|
||||
{{> adder}} {{> userInfo}}
|
||||
</div>
|
||||
{{/if}} {{#unless currentUser}} {{> auth}} {{/unless}}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
@ -26,3 +29,18 @@
|
||||
<template name="adder">
|
||||
<input type="text" id="newNote" class="form-control" placeholder="Add new note">
|
||||
</template>
|
||||
|
||||
<template name="auth">
|
||||
<p align="center">
|
||||
Register a new Account or login
|
||||
</p>
|
||||
<input type="text" id="mail" class="form-control login" placeholder="Email">
|
||||
<input type="text" id="pass" class="form-control login" placeholder="Password">
|
||||
<br>
|
||||
<button type="button" id="register" class="btn btn-info">Register</button>
|
||||
<button type="button" id="login" class="btn btn-info">Login</button>
|
||||
</template>
|
||||
|
||||
<template name="userInfo">
|
||||
<button type="button" id="logout" class="btn btn-danger">Logout</button>
|
||||
</template>
|
||||
|
Loading…
Reference in New Issue
Block a user