a
This commit is contained in:
parent
daa4b79765
commit
269627597e
5
api.MD
5
api.MD
@ -171,10 +171,7 @@ http://localhost/api/v0/sensor-data/data?week=1&sensorid=1&locationid=1&page=2&p
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
curl localhost/api/v0/user/register -H "Content-Type: application/json" -X POST -d '{"username": "testuser123", "password": "thisisthesystemuserpasswordnoob", "email": "testuser123@ecosaver.com", "address": "Nanyang Polytechnic 180 Ang Mo Kio Avenue 8 Singapore 569830", "phone": "12345678"}'
|
||||||
curl localhost/api/v0/user/new -H "Content-Type: application/json" -X POST -d '{"username": "system", "password": "thisisthesystemuserpasswordnoob", "email": "system@ecosaver.com", "address": "Nanyang Polytechnic 180 Ang Mo Kio Avenue 8 Singapore 569830", "phone": "12345678"}'
|
|
||||||
|
|
||||||
curl localhost/api/v0/user/new -H "Content-Type: application/json" -X POST -d '{"username": "testuser", "password": "thisisthesystemuserpasswordnoob", "email": "testuser@ecosaver.com", "address": "Nanyang Polytechnic 180 Ang Mo Kio Avenue 8 Singapore 569830", "phone": "12345678"}'
|
|
||||||
|
|
||||||
|
|
||||||
curl localhost/api/v0/apikey/new -H "Content-Type: application/json" -X POST -d '{"userid": "2", "permission": "canRead"}'
|
curl localhost/api/v0/apikey/new -H "Content-Type: application/json" -X POST -d '{"userid": "2", "permission": "canRead"}'
|
||||||
|
@ -4,15 +4,31 @@ const { userModel } = require("../database/model/userModel.js");
|
|||||||
const { Op, Sequelize } = require("sequelize");
|
const { Op, Sequelize } = require("sequelize");
|
||||||
const { hashAPIKey } = require("../functions/bcrypt.js");
|
const { hashAPIKey } = require("../functions/bcrypt.js");
|
||||||
const { generateUUID } = require("../functions/generateUUID.js");
|
const { generateUUID } = require("../functions/generateUUID.js");
|
||||||
|
const { hashPassword } = require("../functions/bcrypt.js");
|
||||||
|
|
||||||
async function getUser() {
|
async function getUser() {
|
||||||
const user = await userModel.findAll();
|
const user = await userModel.findAll();
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
//api/v0/user/register
|
||||||
|
|
||||||
|
/* Registering new user
|
||||||
|
1) req.body is taken from html form or wtv
|
||||||
|
2) bcrpyt and hash the password on the server side
|
||||||
|
3) pass to db
|
||||||
|
*/
|
||||||
async function addUser(user) {
|
async function addUser(user) {
|
||||||
//console.log(user);
|
console.log(user);
|
||||||
await userModel.create(user);
|
//hash password
|
||||||
|
let hash = await hashPassword(user.password);
|
||||||
|
|
||||||
|
await userModel.create({
|
||||||
|
username: user.username,
|
||||||
|
password: hash,
|
||||||
|
email: user.email,
|
||||||
|
address: user.address,
|
||||||
|
phone: user.phone,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAPIKey() {
|
async function getAPIKey() {
|
||||||
@ -46,7 +62,6 @@ async function addAPIKey(userId, permission) {
|
|||||||
|
|
||||||
|
|
||||||
//user token with -
|
//user token with -
|
||||||
//"apikey": "1-9beba05f-1bf1-4d8a-9ee8-9f61e1428e20"
|
|
||||||
return usertoken;
|
return usertoken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
const { getUser, addUser } = require("../functions/apiDatabase.js");
|
const { getUser, addUser } = require("../functions/apiDatabase.js");
|
||||||
const { hashPassword } = require("../functions/bcrypt.js");
|
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
//get all users
|
||||||
router.get("/", async (req, res, next) => {
|
router.get("/", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const location = await getUser();
|
const location = await getUser();
|
||||||
@ -14,17 +14,9 @@ router.get("/", async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
|
||||||
1) req.body is taken from html form or wtv
|
router.post("/register", async (req, res, next) => {
|
||||||
2) bcrpyt and hash the password on the server side
|
|
||||||
3) pass to db
|
|
||||||
*/
|
|
||||||
router.post("/new", async (req, res, next) => {
|
|
||||||
try {
|
try {
|
||||||
//pass pass to hashPassword
|
|
||||||
let hash = await hashPassword(req.body.password);
|
|
||||||
//add hash back to req.body
|
|
||||||
req.body.password = hash;
|
|
||||||
await addUser(req.body);
|
await addUser(req.body);
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -32,7 +32,7 @@ app.set("json spaces", 2);
|
|||||||
middleware logic ( called by next() )
|
middleware logic ( called by next() )
|
||||||
*/
|
*/
|
||||||
app.use("/api/seed/v0", [ apikeyCheck , APIlogger] ,require("../routes/seed_route.js"));
|
app.use("/api/seed/v0", [ apikeyCheck , APIlogger] ,require("../routes/seed_route.js"));
|
||||||
app.use('/api/v0', [apikeyCheck, APIlogger] ,require("../routes/api_route.js")); //webserver\routes\api_route.js
|
app.use('/api/v0', [apikeyCheck, APIlogger] ,require("../routes/api_route.js"));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user