Livebeat is now able to send, store and show beats

This commit is contained in:
2020-10-23 00:39:36 +02:00
parent f722ee9595
commit 13f8437f29
52 changed files with 1948 additions and 442 deletions

30
backend/endpoints/beat.ts Normal file
View File

@@ -0,0 +1,30 @@
import { Response } from "express";
import { logger } from "../app";
import { LivebeatRequest } from "../lib/request";
import { Beat } from "../models/beat/beat.model.";
import { Phone } from "../models/phone/phone.model";
export interface IFilter {
phone: string,
time: {
from: number,
to: number
},
max: number
}
export async function GetBeat(req: LivebeatRequest, res: Response) {
const filter: IFilter = req.body.filter as IFilter;
// If no filters are specified, we return the last 500 points. We take the first phone as default.
if (filter === undefined) {
const phone = await Phone.findOne({ user: req.user?._id });
logger.debug(`No filters were provided! Take ${phone?.displayName} as default.`);
if (phone !== undefined && phone !== null) {
logger.debug("Query for latest beats ...");
const beats = await Beat.find({ phone: phone._id }).limit(800).sort({ _id: -1 });
res.status(200).send(beats);
}
}
}

View File

@@ -0,0 +1,63 @@
import { Response } from "express";
import { logger } from "../app";
import { LivebeatRequest } from "../lib/request";
import { Phone } from "../models/phone/phone.model";
export async function GetPhone(req: LivebeatRequest, res: Response) {
const phoneId: String = req.params['id'];
if (phoneId === undefined) {
res.status(400).send();
return;
}
// Check database for phone
const phone = await Phone.findOne({ androidId: phoneId, user: req.user?._id });
if (phone === undefined) {
res.status(404).send();
return;
}
res.status(200).send(phone);
}
export async function PostPhone(req: LivebeatRequest, res: Response) {
const androidId: String = req.body.androidId;
const modelName: String = req.body.modelName;
const displayName: String = req.body.displayName;
const operatingSystem: String = req.body.operatingSystem;
const architecture: String = req.body.architecture;
if (androidId === undefined ||
modelName === undefined ||
displayName === undefined ||
operatingSystem === undefined ||
architecture === undefined) {
logger.debug("Request to /phone failed because of missing parameters.");
res.status(400).send();
return;
}
// Check if phone already exists
const phone = await Phone.findOne({ androidId, user: req.user?._id });
if (phone !== null) {
logger.debug("Request to /phone failed because phone already exists.");
res.status(409).send();
return;
}
// Create phone
await Phone.create({
androidId,
displayName,
modelName,
operatingSystem,
architecture,
user: req.user?._id
});
logger.info(`New device (${displayName}) registered for ${req.user?.name}.`)
res.status(200).send();
}

View File

@@ -3,7 +3,8 @@ import { verifyPassword } from "../lib/crypto";
import { User } from "../models/user/user.model";
import { sign, decode, verify } from 'jsonwebtoken';
import { JWT_SECRET, logger } from "../app";
import { IUser } from "../models/user/user.interface";
import { LivebeatRequest } from '../lib/request';
import { SchemaTypes } from "mongoose";
export async function GetUser(req: Request, res: Response) {
@@ -48,7 +49,7 @@ export async function LoginUser(req: Request, res: Response) {
}
// We're good. Create JWT token.
const token = sign({ user: user._id }, JWT_SECRET!, { notBefore: Date.now(), expiresIn: '30d' });
const token = sign({ user: user._id }, JWT_SECRET, { expiresIn: '30d' });
logger.info(`User ${user.name} logged in.`)
res.status(200).send({ token });
@@ -58,29 +59,34 @@ export async function LoginUser(req: Request, res: Response) {
* This middleware validates any tokens that are required to access most of the endpoints.
* Note: This validation doesn't contain any permission checking.
*/
export async function MW_User(req: Request, res: Response, next: () => void) {
export async function MW_User(req: LivebeatRequest, res: Response, next: () => void) {
if (req.headers.token === undefined) {
res.status(401).send();
res.status(401).send({ message: "Token not specified" });
return;
}
const token = req.headers.token.toString();
try {
// Verify token
if(await verify(token, JWT_SECRET!, { algorithms: ['HS256'] })) {
if(await verify(token, JWT_SECRET, { algorithms: ['HS256'] })) {
// Token is valid, now look if user is in db (in case he got deleted)
const id: number = Number(decode(token, { json: true })!.id);
const db = await User.findOne({ where: { id } });
if (db !== undefined) {
const id = decode(token, { json: true })!.user;
const db = await User.findById(id);
if (db !== undefined && db !== null) {
req.user = db
next();
return;
} else {
res.status(401).send();
res.status(401).send({ message: "Token is not valid" });
}
} else {
res.status(401).send();
res.status(401).send({ message: "Token is not valid" });
}
} catch (err) {
if (err) res.status(401).send();
if (err) {
res.status(500).send({ message: "We failed validating your token for some reason." });
logger.error(err);
}
}
}