diff --git a/dist/index.js b/dist/index.js index 88b0893..642d565 100644 --- a/dist/index.js +++ b/dist/index.js @@ -15,17 +15,45 @@ let device_service = new Device.Service(); var router = express.Router(); router.post("/signup", (req, res) => { let user = { username: req.body.username, password: req.body.password }; - user_service.signup(user).then(() => res.json(true)).catch(() => res.json(false)); + user_service.signup(user).then(() => res.json({ success: user.username })).catch((reason) => res.json({ error: reason })); }); router.post('/login', function (req, res) { - res.json({ token: 'youmadeit' }); + return user_service.login(req.body.username, req.body.password).then(user => { + let token = session_service.acquire(user.username); + res.json({ token: token }); + }) + .catch(reason => res.json({ error: reason })); +}); +router.post("/lost-password", function (req, res) { + res.json({ success: "An email will be sent to your address." }); }); router.get('/switch', function (req, res) { - res.json({ status: switch_status }); + session_service.retrieve(req.query.token).then(user => res.json({ status: switch_status })) + .catch(reason => res.json({ error: reason })); }); router.post('/switch', function (req, res) { - switch_status = req.body.status; - res.json({ status: switch_status }); + session_service.retrieve(req.query.token).then(user => { + switch_status = req.body.status; + res.json({ status: switch_status }); + }) + .catch(reason => res.json({ error: reason })); +}); +router.post("/history", function (req, res) { + session_service.retrieve(req.query.token).then(user => { + res.json({ user: user, + history: [ + { date: new Date(2016, 8, 1, 10, 10), status: { switch: true, presence: true } }, + { date: new Date(2016, 8, 1, 10, 16), status: { switch: true, presence: true } }, + { date: new Date(2016, 8, 1, 10, 30), status: { switch: false, presence: false } }, + { date: new Date(2016, 8, 1, 11, 10), status: { switch: false, presence: false } }, + { date: new Date(2016, 8, 1, 11, 16), status: { switch: true, presence: false } }, + { date: new Date(2016, 8, 1, 15, 22), status: { switch: true, presence: true } }, + { date: new Date(2016, 8, 1, 15, 30), status: { switch: true, presence: true } }, + { date: new Date(2016, 8, 1, 15, 35), status: { switch: false, presence: true } }, + { date: new Date(2016, 8, 1, 15, 55), status: { switch: true, presence: true } }, + ] }); + }) + .catch(reason => res.json({ error: reason })); }); app.use('/', router); app.listen(port); diff --git a/dist/models/user.js b/dist/models/user.js new file mode 100644 index 0000000..8ecbace --- /dev/null +++ b/dist/models/user.js @@ -0,0 +1,5 @@ +"use strict"; +class Model { +} +exports.Model = Model; +; diff --git a/dist/services/session.js b/dist/services/session.js index f15ddf9..d07ad5b 100644 --- a/dist/services/session.js +++ b/dist/services/session.js @@ -1,5 +1,33 @@ "use strict"; +const Datastore = require("nedb"); +const shortid = require("shortid"); +class Token { +} +exports.Token = Token; +; class Service { + constructor() { + this.db = new Datastore({ filename: "./sessions.db", autoload: true }); + } + acquire(username) { + let token = new Token(); + token.__id = shortid.generate(); + token.username = username; + this.db.insert(token); + return token.__id; + } + retrieve(token) { + return new Promise((resolve, reject) => { + this.db.findOne({ __id: token }, (err, document) => { + if (err) + return reject(err.message); + if (document) { + return resolve(document.username); + } + return reject("Token not found."); + }); + }); + } } exports.Service = Service; ; diff --git a/dist/services/user.js b/dist/services/user.js index cfa120c..87b3916 100644 --- a/dist/services/user.js +++ b/dist/services/user.js @@ -1,31 +1,38 @@ "use strict"; const Datastore = require("nedb"); -class Model { -} -exports.Model = Model; -; class Service { constructor() { this.db = new Datastore({ filename: "./users.db", autoload: true }); } signup(user) { - return new Promise((resolve, reject) => this.db.insert(user, (err, document) => { - if (err) { - return reject(err); - } - return resolve(); - })); + return new Promise((resolve, reject) => { + this.db.findOne({ username: user.username }, (err, doc) => { + if (err) + return reject(err); + if (doc) { + return reject("The user already exists."); + } + this.db.insert(user, (err, document) => { + if (err) { + return reject(err.message); + } + return resolve(true); + }); + }); + }); } - ; login(username, password) { return new Promise((resolve, reject) => this.db.findOne({ username: username, password: password }, (err, document) => { if (err) { - return reject(err); + return reject(err.message); } else { - return resolve(document); + if (document) { + return resolve(document); + } + return reject("Not found."); } })); } diff --git a/package.json b/package.json index 3a0f172..fbcc24b 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "dependencies": { "body-parser": "^1.15.2", "express": "^4.14.0", - "nedb": "^1.8.0" + "nedb": "^1.8.0", + "shortid": "^2.2.6" }, "devDependencies": { "gulp": "^3.9.1", diff --git a/sessions.db b/sessions.db new file mode 100644 index 0000000..3174968 --- /dev/null +++ b/sessions.db @@ -0,0 +1,3 @@ +{"__id":"HkZEj0-j","username":"ignacio","_id":"Iey1FPz1W2a26EJT"} +{"__id":"SkWp9Cbi","username":"ignacio","_id":"uL5rllhEIEVAIjyn"} +{"__id":"B1d8GyGj","username":"ignacio","_id":"TprKNb94X88OnZSQ"} diff --git a/src/index.ts b/src/index.ts index e4a8563..2945fcb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,8 @@ import express = require("express"); import bodyParser = require("body-parser"); +import * as UserModel from "./models/user"; + import * as Device from "./services/device"; import * as User from "./services/user"; import * as Session from "./services/session"; @@ -22,18 +24,47 @@ let device_service = new Device.Service(); var router = express.Router(); router.post("/signup", (req,res) => { - let user: User.Model = { username: req.body.username, password: req.body.password }; - user_service.signup(user).then(() => res.json(true)).catch(() => res.json(false)); + let user: UserModel.Model = { username: req.body.username, password: req.body.password }; + user_service.signup(user).then(() => res.json({success: user.username})).catch((reason) => res.json({error: reason})); }); router.post('/login', function(req, res) { - res.json({ token: 'youmadeit' }); + return user_service.login(req.body.username, req.body.password).then(user => + { + let token = session_service.acquire(user.username); + res.json({ token: token }); + }) + .catch(reason => res.json({ error: reason })); +}); +router.post("/lost-password", function(req,res) { + res.json({success: "An email will be sent to your address."}); }); router.get('/switch', function(req, res) { - res.json({ status: switch_status }); + session_service.retrieve(req.query.token).then(user => res.json({ status: switch_status })) + .catch(reason => res.json({error: reason})); }); router.post('/switch', function(req, res) { - switch_status = req.body.status; - res.json({ status: switch_status }); + session_service.retrieve(req.query.token).then(user => { + switch_status = req.body.status; + res.json({ status: switch_status }); + }) + .catch(reason => res.json({error: reason})); +}); +router.post("/history", function(req,res) { + session_service.retrieve(req.query.token).then(user => { + res.json({user: user, + history: [ + { date: new Date(2016,8,1,10,10), status: {switch: true, presence: true} }, + { date: new Date(2016,8,1,10,16), status: {switch: true, presence: true} }, + { date: new Date(2016,8,1,10,30), status: {switch: false, presence: false} }, + { date: new Date(2016,8,1,11,10), status: {switch: false, presence: false} }, + { date: new Date(2016,8,1,11,16), status: {switch: true, presence: false} }, + { date: new Date(2016,8,1,15,22), status: {switch: true, presence: true} }, + { date: new Date(2016,8,1,15,30), status: {switch: true, presence: true} }, + { date: new Date(2016,8,1,15,35), status: {switch: false, presence: true} }, + { date: new Date(2016,8,1,15,55), status: {switch: true, presence: true} }, + ]}); + }) + .catch(reason => res.json({error: reason})); }); app.use('/', router); diff --git a/src/models/user.ts b/src/models/user.ts new file mode 100644 index 0000000..d10ae00 --- /dev/null +++ b/src/models/user.ts @@ -0,0 +1,4 @@ +export class Model { + username: string; + password: string; +}; diff --git a/src/services/session.ts b/src/services/session.ts index 5487723..a4f2e84 100644 --- a/src/services/session.ts +++ b/src/services/session.ts @@ -1,3 +1,34 @@ +import Datastore = require("nedb"); +import shortid = require("shortid"); + +export class Token { + __id : string; + username : string; + expiration : Date; +}; + export class Service { - + db: Datastore; + constructor() { + this.db = new Datastore({filename: "./sessions.db", autoload: true}); + } + acquire(username: string): string { + let token = new Token(); + token.__id = shortid.generate(); + token.username = username; + this.db.insert(token); + return token.__id; + } + retrieve(token: string): Promise { + return new Promise((resolve,reject) => { + this.db.findOne({__id: token}, (err,document: Token) => { + if (err) + return reject(err.message); + if (document) { + return resolve(document.username); + } + return reject("Token not found."); + }); + }); + } }; diff --git a/src/services/user.ts b/src/services/user.ts index f7c54ed..789c4d3 100644 --- a/src/services/user.ts +++ b/src/services/user.ts @@ -1,32 +1,40 @@ import Datastore = require("nedb"); - -export class Model { - username: string; - password: string; -}; +import * as UserModel from "../models/user"; export class Service { db: Datastore; constructor() { this.db = new Datastore({filename: "./users.db", autoload: true}); } - signup(user: Model): Promise { - return new Promise((resolve,reject) => this.db.insert(user, (err,document) => { - if (err) { - return reject(err); - } - return resolve(); - })); - }; - login(username: string, password: string): Promise { - return new Promise((resolve,reject) => this.db.findOne({ + signup(user: UserModel.Model): Promise { + return new Promise((resolve,reject) => { + this.db.findOne({username: user.username}, (err,doc) => { + if (err) return reject(err); + if (doc) { + return reject("The user already exists."); + } + this.db.insert(user, (err,document) => { + if (err) { + return reject(err.message); + } + return resolve(true); + } + ); + }); + }); + } + login(username: string, password: string): Promise { + return new Promise((resolve,reject) => this.db.findOne({ username: username, password: password }, (err,document) => { if (err) { - return reject(err); + return reject(err.message); } else { - return resolve(document as Model); + if (document) { + return resolve(document as UserModel.Model); + } + return reject("Not found."); } })); }; diff --git a/typings.json b/typings.json index d674ba4..734ffa4 100644 --- a/typings.json +++ b/typings.json @@ -4,6 +4,7 @@ "es6-promise": "registry:npm/es6-promise#3.0.0+20160723033700" }, "globalDependencies": { - "nedb": "registry:dt/nedb#0.0.0+20160505185910" + "nedb": "registry:dt/nedb#0.0.0+20160505185910", + "shortid": "registry:dt/shortid#0.0.0+20160316155526" } } diff --git a/typings/globals/shortid/index.d.ts b/typings/globals/shortid/index.d.ts new file mode 100644 index 0000000..529e95a --- /dev/null +++ b/typings/globals/shortid/index.d.ts @@ -0,0 +1,9 @@ +// Generated by typings +// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/56295f5058cac7ae458540423c50ac2dcf9fc711/shortid/shortid.d.ts +declare module "shortid" { + export function generate(): string; + export function characters(string: string): string; + export function isValid(id: any): boolean; + export function worker(integer: number): void; + export function seed(float: number): void; +} diff --git a/typings/globals/shortid/typings.json b/typings/globals/shortid/typings.json new file mode 100644 index 0000000..29c1a66 --- /dev/null +++ b/typings/globals/shortid/typings.json @@ -0,0 +1,8 @@ +{ + "resolution": "main", + "tree": { + "src": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/56295f5058cac7ae458540423c50ac2dcf9fc711/shortid/shortid.d.ts", + "raw": "registry:dt/shortid#0.0.0+20160316155526", + "typings": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/56295f5058cac7ae458540423c50ac2dcf9fc711/shortid/shortid.d.ts" + } +} diff --git a/typings/index.d.ts b/typings/index.d.ts index 47243db..d977a67 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,5 +1,6 @@ /// /// +/// /// /// /// diff --git a/users.db b/users.db new file mode 100644 index 0000000..86bee47 --- /dev/null +++ b/users.db @@ -0,0 +1,15 @@ +{"_id":"1c966sQXzKWWPAyf"} +{"_id":"3u7L6W7f3zPV9yMb"} +{"username":"nicolas","password":"test123","_id":"A0hn3PZxux1OIyc5"} +{"_id":"B973eCDaoOyKL1A1"} +{"_id":"C7zA558rKbDUgXXU"} +{"_id":"CAPhGDJiwkmwKNl3"} +{"_id":"G5FNaevxTEcMH2y7"} +{"_id":"KCbkAzouj49JHIBn"} +{"_id":"MqWTHG88N6MtHqyg"} +{"_id":"O1Gv1VYFCVbxhwOk"} +{"_id":"QR7QVpxnn9vf3wHP"} +{"username":"ignacio","password":"test123","_id":"XhvPJQWc2w026Xzv"} +{"password":"test123","_id":"cEcFIaxF4iYlQxc6"} +{"_id":"d5AjbtdZz3nWvOl0"} +{"_id":"gWH2ZAKqMkz4vu37"}