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

added GUI to change API keys

This commit is contained in:
Enrico Fasoli 2015-03-09 18:03:25 +01:00
parent 7169bf3a57
commit 19850b1d20
4 changed files with 25 additions and 9 deletions

View File

@ -168,12 +168,16 @@ Template.menu.events
# Account Page # Account Page
Template.account.helpers Template.account.helpers
dateformat: -> if getUser() then return getUser().dateformat dateformat: -> if getUser() then return getUser().dateformat
apikey: -> if getUser() then return getUser().apiKey
Template.account.events Template.account.events
'click #reset-settings': (e,t) -> 'click #reset-settings': (e,t) ->
t.find('#set-date-format').value = "MM/DD/YYYY" t.find('#set-date-format').value = "MM/DD/YYYY"
t.find('#set-api-key').value = ''
'click #save-settings': (e,t) -> 'click #save-settings': (e,t) ->
Meteor.users.update getUser()._id, Meteor.users.update getUser()._id,
$set: dateformat: t.find('#set-date-format').value $set:
dateformat: t.find('#set-date-format').value
apiKey: t.find('#set-api-key').value
showError msg: 'Settings saved', type: 'success' showError msg: 'Settings saved', type: 'success'
'click #btn-logout': -> Meteor.logout logoutCallback 'click #btn-logout': -> Meteor.logout logoutCallback
'click #btn-delete-me': -> deleteAccount() 'click #btn-delete-me': -> deleteAccount()

View File

@ -161,9 +161,17 @@
<template name="account"> <template name="account">
<div align="center"> <div align="center">
<h3 class="cool-header"><i class="fa fa-user fa-2x"></i><br>{{email}}</h3> <h3 class="cool-header"><i class="fa fa-user fa-2x"></i><br>{{email}}</h3>
<p class="lead">Settings</p> <p class="lead">Date Format</p>
<p>You can choose the format used to write and read dates in the note list</p> <p>You can choose the format used to write and read dates in the note list</p>
<input type="text" class="form-control" id="set-date-format" placeholder="Date Format" value="{{dateformat}}"> <input type="text" class="form-control" id="set-date-format" placeholder="Date Format" value="{{dateformat}}">
<hr>
<p class="lead">API Key</p>
<p>If you want to be able to use our <b>RESTful API</b> or an application that uses it (such as our awesome
<a href="https://www.npmjs.com/package/homework-cli">command line client</a>) you need to input an <b>API Key</b>
here that will allow the programs to <u>read, write and delete all your notes</u>.</p>
<p>you can change this key any time
or remove it completely by leaving this field blank.</p>
<input type="text" class="form-control" id="set-api-key" placeholder="Your API Key" value="{{apikey}}">
<div class="btn-group"> <div class="btn-group">
<button type="button" id="save-settings" class="btn btn-primary"> <button type="button" id="save-settings" class="btn btn-primary">
<i class="fa fa-upload"></i> Save</button> <i class="fa fa-upload"></i> Save</button>

View File

@ -4,13 +4,14 @@ if !Meteor.settings.enableAPI? then return
console.log 'RESTful HTTP API enabled' console.log 'RESTful HTTP API enabled'
apiKeyToUser = (key) -> Meteor.users.findOne apiKey: key apiKeyToUser = (key) ->
if key? and key != "" then Meteor.users.findOne apiKey: key
respond = (res, code, obj) -> respond = (res, code, obj) ->
res.writeHead code, 'Content-Type': 'application/json' res.writeHead code, 'Content-Type': 'application/json'
res.end JSON.stringify obj res.end JSON.stringify obj
# GET NOTES
Router.route '/api/:key', where: 'server' Router.route '/api/:key', where: 'server'
# GET NOTES
.get -> .get ->
user = apiKeyToUser @params.key user = apiKeyToUser @params.key
if !user if !user

View File

@ -16,7 +16,7 @@ userValidated = (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 'user', -> Meteor.publish 'user', ->
Meteor.users.find @userId, fields: {dateformat: 1, username: 1} Meteor.users.find @userId, fields: {dateformat: 1, username: 1, apiKey: 1}
# Publish user's notes to each user. # Publish user's notes to each user.
Meteor.publish "notes", (archived) -> Meteor.publish "notes", (archived) ->
if userValidated getUser(@userId) if userValidated getUser(@userId)
@ -57,7 +57,10 @@ Meteor.methods
# Allow users to change their date format # Allow users to change their date format
Meteor.users.allow Meteor.users.allow
update: (id,doc,fields,mod) -> update: (id,doc,fields,mod) ->
if fields[0] == 'dateformat' and fields.length == 1 for i,f of fields
return yes console.log f
return no unless f in ['dateformat','apiKey']
return no
yes
fetch: ['dateformat','apiKey']