Skip to content

Commit

Permalink
First steps with the auth system, implemented websocket support.
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiklippl committed Oct 1, 2020
1 parent 518a9bd commit 8d10793
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 48 deletions.
53 changes: 51 additions & 2 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import Sentry = require('@sentry/node');
import express = require('express');
import http = require('http');

import {EventEmitter} from "events";

// Services
import {Service} from "./services/SentryService";
import {SentryService} from "./services/SentryService";
import {MongooseService} from "./services/MongooseService";

import * as net from 'net'

//Routes
import {apiRouter} from "./routes/ApiRoute";
import {authRouter} from "./routes/AuthRoute";
import {Server, Socket} from "socket.io";
import {OutgoingHttpHeaders} from "http";

try {
//read .env file
Expand All @@ -16,7 +24,11 @@ try {
const app: express.Application = express();

//initialize sentry logging
const sentry: Service.SentryService = new Service.SentryService();
const sentry: SentryService = new SentryService();

//connect to mongodb database
const database: MongooseService = new MongooseService();
database.connect();

//register routers
app.use('/api/v1', apiRouter);
Expand All @@ -25,6 +37,43 @@ try {
app.listen(process.env.PORT, function () {
console.log('cloud rest-api listening on port ' + process.env.PORT);
});

const WebSocket = require('ws');
const wss = new WebSocket.Server({port: 3000});

let i: number;
wss.on('connection', function connection(ws: any) {
console.log("new connection")
ws.on('message', function incoming(message: any) {
console.log('Client: %s', message);
ws.send('Server: ' + i);
i++;
});

ws.send('hello');
});

// Normal Socket (Works with java)
// var i: number;
//
// const server = net.createServer((socket) => {
// socket.on('data', (data) => {
// console.log("Data: " + data.toString());
// socket.write('Resend ' + i);
// i++;
// });
//
// socket.on('pingServer', function (data) {
// console.log("ping")
// //console.log(data);
// socket.emit('pingServer', data);
// });
// })
//
// server.listen(3000, function () {
// console.log(`Server listening for connection requests on socket localhost:3000`);
// });

} catch (e) {
Sentry.captureException(e);
}
54 changes: 44 additions & 10 deletions controls/AuthController.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
import {Request, Response} from "express";
import UserModel from "../models/UserModel";

export namespace Controls {
export class AuthController {
static auth(req: Request, res: Response, next: void) {
import {v4 as uuid} from 'uuid';

}
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');

static create(req: Request, res: Response) {
export class AuthController {
static auth(req: Request, res: Response, next: void) {

}
}

static create(req: Request, res: Response) {
// const {username, password} = req.headers;
//
// if (username !== undefined && password !== undefined) {
// const user = UserModel.findOne({
// username: `${username}`
// });
//
// if (user === null) {
// console.log("NEW !")
// }
//
// const hashedPassword = bcrypt.hashSync(password, 8);
//
// UserModel.create({
// username: `${username}`,
// password: `${hashedPassword}`,
// }, (err, user) => {
// if (err)
// return res.status(500).send("There was a problem registering the user.");
//
// // create a token
// const token = jwt.sign({uuid: user[0]}, process.env.SECRET, {
// expiresIn: 86400 // expires in 24 hours
// });
//
// res.status(200).send({auth: true, token: token});
// }).then(r => {
// });
// } else {
// res.sendStatus(400);
// }
}

static remove(req: Request, res: Response): void {
static remove(req: Request, res: Response): void {

}
}

static list(req: Request, res: Response): void {
static list(req: Request, res: Response): void {

}
}
}
15 changes: 15 additions & 0 deletions models/UserModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import mongoose, {Schema, Document} from 'mongoose';

export interface IUser extends Document {
username: string;
password: string;
}

const UserSchema: Schema = new Schema({
username: {type: String, required: true},
password: {type: String, required: true}
}, {
versionKey: false
});

export default mongoose.model<IUser>('User', UserSchema, "users");
Loading

0 comments on commit 8d10793

Please sign in to comment.