Commit d06a6726 authored by KANTAPONG SONG-NGAM's avatar KANTAPONG SONG-NGAM

add

parent d06332d1
Pipeline #80 failed with stages
Schema = require('mongoose').Schema
ThingSchema = new Schema({
Name : {
type : String,
required : true
},
Data : {
type : [{
Value : {
type : Number,
min : 0
},
Time : {
type : Date
}
}]
},
userId : {
type : Schema.Types.ObjectId
}
})
module.exports = mongoose.model('Things', ThingSchema)
Schema = require('mongoose').Schema
bcrypt = require('bcrypt')
SALT_WORK_FACTOR = 10
UserSchema = new Schema({
username : {
type : String,
required : true
},
password : {
type : String,
required : true
}
})
UserSchema.pre('save', function(next) {
user = this
if (!user.isModified('password'))
return next()
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next (err)
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next (err)
user.password = hash
next()
})
})
})
UserSchema.methods.comparePassword = function(candidate, cb) {
bcrypt.compare(candidate, this.password, function(err, isMatch) {
if (err) return cb (err)
cb(null, isMatch)
})
}
module.exports = mongoose.model('Users', UserSchema)
module.exports = {
'secret': 'KolenCompation',
'database': 'mongodb://localhost/Kolen'
}
\ No newline at end of file
This diff is collapsed.
{
"name": "mobile",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node testmongoose.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"jsonwebtoken": "^8.3.0",
"mongoose": "^5.3.4",
"morgan": "^1.9.1"
}
}
express = require('express');
app = express();
bodyParser = require('body-parser');
morgan = require('morgan');
mongoose = require('mongoose');
jwt = require('jsonwebtoken');
config = require('./config');
User = require('./user');
// =======================
// configuration
// =======================
// server setting
var port = process.env.PORT || 8000;
// connect databse
mongoose.connect(config.database,{useNewUrlParser:true});
// application variables
app.set('superSecret', config.secret);
// config for body-parser
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
// log request
app.use(morgan('dev'));
// =======================
// routes
// =======================
app.get('/', function(req, res) {
res.send('Hello! The API is at http://localhost:' + port + '/api');
});
app.get('/setup', function(req, res) {
demo = new User({
username: 'demouser',
password: 'password', // TODO: encrypt password with bcrypt
admin: true
});
demo.save(function(err) {
if (err) throw err;
console.log('User saved successfully');
res.json({ success: true});
});
});
// API ROUTES ================
apiRoutes = express.Router();
// GET(http://localhost:8080/api/)
apiRoutes.get('/', function(req, res) {
res.json({ message: 'Welcome to API routing'});
});
// Authentification Filter
apiRoutes.use(function(req, res, next) {
// get token from body:token or query:token of Http Header:x-access-token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// validate token
if (!token) {
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
if (err) {
return res.json({
success: false,
message: 'Invalid token'
});
}
// if token valid -> save token to request for use in other routes
req.decoded = decoded;
next();
});
});
// GET(http://localhost:8080/api/users)
apiRoutes.get('/users', function(req, res) {
User.find({}, function(err, users) {
if (err) throw err;
res.json(users);
});
});
// apply the routes to our application(prefix /api)
app.use('/api', apiRoutes);
// POST(http://localhost:8080/api/authenticate)
apiRoutes.post('/authenticate', function(req, res) {
// find db by posted name
User.findOne({
username: req.body.username
}, function(err, user) {
if (err) throw err;
// validation
if (!user) {
res.json({
success: false,
message: 'Authentication failed. User not found.'
});
return;
}
if (user.password != req.body.password) {
res.json({
success: false,
message: 'Authentication failed. Wrong password.'
});
return;
}
// when valid -> create token
var token = jwt.sign(user.toJSON(), app.get('superSecret'), {
expiresIn: '24h'
});
res.json({
success: true,
message: 'Authentication successfully finished.',
token: token
});
});
});
// =======================
// start the server
// =======================
app.listen(port);
console.log('started http://localhost:' + port + '/');
mongoose = require('mongoose')
Things = require('./Things')
Users = require('./Users')
mongoose.connect('mongodb://localhost:27017/TestAPI',{useNewUrlParser:true});
testUser = new Users({
username: 'test',
password: 'pass'
})
testUser.save(function(err) {
if (err) throw err
console.log('save data complete')
})
[
{"id":1,"name":"AccSensors","userid":1,"data":[1,2,3,4,5]},
{"id":2,"name":"SexySensors","userid":1,"data":[1,2,3,4,5]},
{"id":3,"name":"Araiensors","userid":2,"data":[1,2,3,4,5]},
{"id":4,"name":"AhoSensors","userid":4,"data":[1,2,3,4,5]},
{"id":5,"name":"AnoSensors","userid":12,"data":[1,2,3,4,5]},
{"id":6,"name":"AaaSensors","userid":3,"data":[1,2,3,4,5]},
{"id":7,"name":"bbbSensors","userid":6,"data":[1,2,3,4,5]},
{"id":8,"name":"ccSensors","userid":1,"data":[1,2,3,4,5]},
{"id":9,"name":"dccSensors","userid":6,"data":[1,2,3,4,5]},
{"id":10,"name":"eccSensors","userid":10,"data":[1,2,3,4,5]},
{"id":11,"name":"fccSensors","userid":7,"data":[1,2,3,4,5]},
{"id":12,"name":"akbSensors","userid":17,"data":[1,2,3,4,5]},
{"id":13,"name":"bnkSensors","userid":9,"data":[1,2,3,4,5]},
{"id":14,"name":"prangSensors","userid":1,"data":[1,2,3,4,5]},
{"id":15,"name":"AssSensors","userid":7,"data":[1,2,3,4,5]},
{"id":16,"name":"HoleSensors","userid":10,"data":[1,2,3,4,5]},
{"id":17,"name":"fdfdSensors","userid":21,"data":[1,2,3,4,5]},
{"id":18,"name":"hgjSensors","userid":1,"data":[1,2,3,4,5]},
{"id":19,"name":"khfSensors","userid":12,"data":[1,2,3,4,5]},
{"id":20,"name":"tttSensors","userid":1,"data":[1,2,3,4,5]},
{"id":21,"name":"bjSensors","userid":14,"data":[1,2,3,4,5]},
{"id":22,"name":"ggtSensors","userid":14,"data":[1,2,3,4,5]},
{"id":23,"name":"okdhSensors","userid":31,"data":[1,2,3,4,5]},
{"id":24,"name":"bsSensors","userid":1,"data":[1,2,3,4,5]},
{"id":25,"name":"cdrsSensors","userid":44,"data":[1,2,3,4,5]}
]
\ No newline at end of file
// get mongoose.Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
UserSchema = new Schema({
username : {
type : String ,
required : true
},
password : {
type : String,
required : true
},
admin : {
type : Boolean,
required : true
}
})
// make user model and export
module.exports = mongoose.model('User', UserSchema);
[
{"id":1,"name":"Anata"},
{"id":2,"name":"fdfda"},
{"id":3,"name":"Sugoi"},
{"id":4,"name":"Ikuiku"},
{"id":6,"name":"Oppai"},
{"id":7,"name":"Bukkake"},
{"id":9,"name":"zxcvb"},
{"id":10,"name":"qwerty"},
{"id":12,"name":"Okamoto"},
{"id":14,"name":"Tenga"},
{"id":17,"name":"Meiki"},
{"id":21,"name":"Itaiyo"},
{"id":31,"name":"Babakb"},
{"id":44,"name":"dgsfe"}
]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment