mirror of
https://github.com/fazo96/homework.git
synced 2025-01-25 14:34:20 +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
|
if Meteor.isServer
|
||||||
#todos.insert { content: "Example" } unless todos.find().fetch().length > 0
|
#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
|
if Meteor.isClient
|
||||||
Meteor.subscribe "todos"
|
Meteor.subscribe "todos"
|
||||||
|
# Notes template
|
||||||
Template.notes.notes = ->
|
Template.notes.notes = ->
|
||||||
todos.find().fetch()
|
todos.find().fetch()
|
||||||
Template.notes.events {
|
Template.notes.events {
|
||||||
'click .delete': ->
|
'click .delete': ->
|
||||||
todos.remove @_id
|
todos.remove @_id
|
||||||
}
|
}
|
||||||
|
# Template for new notes
|
||||||
Template.adder.events {
|
Template.adder.events {
|
||||||
'keypress #newNote': (e,template) ->
|
'keypress #newNote': (e,template) ->
|
||||||
if e.keyCode is 13
|
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 = ""
|
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>
|
<h1>Homework <small>management for students</small></h1>
|
||||||
</div>
|
</div>
|
||||||
<div class="center-block" id="quicknotes">
|
<div class="center-block" id="quicknotes">
|
||||||
{{> notes}}
|
{{> notes}} {{#if currentUser}}
|
||||||
<div align="center"> {{> adder}} </div>
|
<div align="center">
|
||||||
|
{{> adder}} {{> userInfo}}
|
||||||
|
</div>
|
||||||
|
{{/if}} {{#unless currentUser}} {{> auth}} {{/unless}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
@ -26,3 +29,18 @@
|
|||||||
<template name="adder">
|
<template name="adder">
|
||||||
<input type="text" id="newNote" class="form-control" placeholder="Add new note">
|
<input type="text" id="newNote" class="form-control" placeholder="Add new note">
|
||||||
</template>
|
</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