flow: prep

This commit is contained in:
cha0s 2020-07-12 18:16:13 -05:00
parent 7469ceecb1
commit 7b69d023e6
11 changed files with 65 additions and 33 deletions

View File

@ -6,6 +6,14 @@ services:
ports:
- 31346:6379
reddichat_phpredisadmin:
image: erikdubbelboer/phpredisadmin:latest
environment:
- REDIS_1_HOST=reddichat_redis
- REDIS_1_NAME=reddichat
labels:
- 'traefik.frontend.rule=Host:phpredisadmin.reddichat.localhost'
reddichat_mysql:
image: mysql:8
ports:

View File

@ -73,7 +73,6 @@ code {
}
label {
align-items: left;
background-color: rgba(255, 255, 255, 0.025);
color: #ffffff;
display: flex;

View File

@ -1,24 +1,9 @@
import express from 'express';
import httpSession from 'express-session';
import passport from 'passport';
import {allModels} from './models/registrar';
import userRoutes from './routes/user';
import passport from './passport';
import session from './session';
let insideSession;
passport.serializeUser((user, fn) => fn(null, user.id));
passport.deserializeUser(async (id, fn) => {
const {User} = allModels();
try {
fn(undefined, await User.findByPk(id));
}
catch (error) {
fn(error);
}
});
export default function createApp() {
const app = express();
app.use(express.urlencoded({extended: true}));

View File

@ -8,7 +8,7 @@ import {allModels} from './models/registrar';
let map;
export async function createDatabaseConnection() {
const Models = allModels();
const Models = Object.values(allModels());
const sequelize = new Sequelize(mysqlOrigin);
Models.filter((Model) => Model.attributes).forEach((Model) => {
Model.init(Model.attributes, {

View File

@ -36,7 +36,6 @@ export function createHttpServer() {
res.sendFile(join(__dirname, '..', 'client', 'index.html'));
});
}
return httpServer;
}

View File

@ -5,10 +5,6 @@ import {all} from './models.scwp';
const modelTo = new Map();
const nameTo = new Map();
export function allModels() {
return Object.entries(all()).map(([, M]) => M.default);
}
let hasMapped = false;
function ensureNameMap() {
if (!hasMapped) {
@ -33,6 +29,11 @@ registerHooks({
},
}, module.id);
export function allModels() {
ensureNameMap();
return Array.from(nameTo.entries()).reduce((r, [key, M]) => ({...r, [key]: M}), {});
}
export function lookupModel(name) {
ensureNameMap();
return nameTo.get(name);

View File

@ -1,3 +1,6 @@
/* eslint-disable import/no-extraneous-dependencies */
import {randomBytes} from 'crypto';
import bcrypt from 'bcrypt';
import {registerHooks} from 'scwp';
import {DataTypes as Types} from 'sequelize';
@ -44,12 +47,24 @@ User.saltRounds = 10;
export default User;
registerHooks({
replCommands: () => ({
replCommands: (socket) => ({
createUser: (spec) => {
const [email, maybePassword] = spec.split(' ', 2);
const password = maybePassword || crypto.randomBytes(8).toString('hex');
const password = maybePassword || randomBytes(8).toString('hex');
const user = User.build({email});
user.addHashedPassword(password).then(() => user.save());
},
resetPassword: async (email) => {
const password = randomBytes(8).toString('hex');
const user = await User.findOne({where: {email}});
if (user) {
user.addHashedPassword(password)
.then(() => user.save())
// eslint-disable-next-line no-console
.then(() => socket.write(`New password:\n\n${password}\n`));
}
// eslint-disable-next-line no-console
console.log('User not found.');
},
}),
}, module.id);

16
src/server/passport.js Normal file
View File

@ -0,0 +1,16 @@
import passport from 'passport';
import {allModels} from './models/registrar';
passport.serializeUser((user, fn) => fn(null, user.id));
passport.deserializeUser(async (id, fn) => {
const {User} = allModels();
try {
fn(undefined, await User.findByPk(id));
}
catch (error) {
fn(error);
}
});
export default passport;

View File

@ -16,7 +16,7 @@ export function createReplServer() {
replServer.context[key] = value;
});
Object.entries(
invokeHookFlat('replCommands').reduce((r, commands) => ({...r, ...commands}), {}),
invokeHookFlat('replCommands', socket).reduce((r, commands) => ({...r, ...commands}), {}),
).forEach(([key, value]) => {
replServer.defineCommand(key, value);
});

View File

@ -1,7 +1,11 @@
import redis from 'redis';
import session from 'express-session';
const redisClient = redis.createClient();
import {redisOrigin} from '~/common/environment';
const [host = 'localhost', port = 31346] = redisOrigin.split(':');
const redisClient = redis.createClient(port, host);
// eslint-disable-next-line import/newline-after-import
const RedisStore = require('connect-redis')(session);

View File

@ -6,21 +6,26 @@ import socketSession from 'express-socket.io-session';
import {redisOrigin} from '~/common/environment';
import passport from './passport';
import session from './session';
export function createSocketServer(httpServer) {
const [redisHost, redisPort] = redisOrigin.split(':');
const [host = 'localhost', port = 31346] = redisOrigin.split(':');
const socketServer = new SocketServer(httpServer, {
adapter: redisAdapter({
host: redisHost || 'localhost',
port: redisPort || 31346,
}),
adapter: redisAdapter({host, port}),
});
socketServer.io.use(socketSession(session()));
socketServer.io.use((socket, next) => {
passport.initialize()(socket.handshake, undefined, next);
});
socketServer.io.use((socket, next) => {
passport.session()(socket.handshake, undefined, next);
});
socketServer.on('connect', (socket) => {
const {req} = socket;
socket.on('packet', (packet, fn) => {
});
});
socketServer.io.use(socketSession(session()));
return socketServer;
}