Init -- well, everything.

This commit is contained in:
oitsjustjose 2020-07-03 21:55:53 -05:00
commit 4645561d33
9 changed files with 352 additions and 0 deletions

View file

@ -0,0 +1,41 @@
import bcrypt from 'bcrypt'
import { Document, model, Schema, Types } from 'mongoose'
const UserSchema = new Schema({
username: String,
password: String,
configs: [{
name: String,
conf: Object,
}]
})
UserSchema.methods.generateHash = function (password: string) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8))
}
UserSchema.methods.validPassword = function (password: string) {
return bcrypt.compareSync(password, String(this.password))
}
UserSchema.methods.changePassword = function (password: string, newPassword: string) {
if (bcrypt.compareSync(password, this.password)) {
this.password = newPassword
}
}
interface INamedConfig {
name: string,
conf: object
}
export type UserModel = Document & {
username: string,
password: string,
configs: Array<INamedConfig>,
generateHash: Function,
validPassword: Function,
changePassword: Function
}
export default model<UserModel>('users', UserSchema, 'users')

45
src/app/routes/auth.ts Normal file
View file

@ -0,0 +1,45 @@
import { Application } from "express"
import { PassportStatic } from "passport"
import UserDB from '../models/user.model'
export const init = (app: Application, passport: PassportStatic) => {
app.get('/login', (req, res) => {
if (req.isAuthenticated()) {
res.redirect('/')
} else {
if (req.query.origin) {
req.session!.returnTo = req.query.origin
}
res.render('login')
}
})
app.get('/logout', (req, res) => {
req.logout()
res.redirect('/login')
})
app.post('/login', passport.authenticate('local', {
failureRedirect: '/login?error'
}), async (req, res) => {
/* Don't keep the pass reset id if the user can log in... */
if (req.session?.returnTo) {
res.redirect(req.session.returnTo)
} else {
res.redirect('/')
}
})
app.post('/register', async (req, res) => {
const exUser = await UserDB.findOne({ 'email': req.body.email })
if (exUser) {
res.redirect('/register?exists')
} else {
let user = new UserDB()
user.username = req.body.username
user.password = user.generateHash(req.body.password)
user.configs = Array()
await user.save()
}
})
}

28
src/app/routes/routes.ts Normal file
View file

@ -0,0 +1,28 @@
import { Application } from "express"
import { PassportStatic } from "passport"
import * as Auth from './auth'
export const init = (app: Application, passport: PassportStatic) => {
app.get('/', async (req, res) => {
if (req.isAuthenticated() && req.user) {
res.render('home')
} else {
res.redirect('/login?origin=/')
}
})
app.put('/', async (req, res) => {
if (req.isAuthenticated() && req.user) {
req.user.configs.push({
name: req.body.name,
conf: req.body.config
})
} else {
res.status(401).send({
error: 'Not logged in!'
})
}
})
Auth.init(app, passport)
}

28
src/app/utils/logs.ts Normal file
View file

@ -0,0 +1,28 @@
import fs from 'fs'
export const init = (baseDir: string) => {
if (!fs.existsSync(`${baseDir}/logs`)) {
fs.mkdirSync(`${baseDir}/logs`)
}
let date = new Date()
let logs: fs.WriteStream | null = null
let errs: fs.WriteStream | null = null
console.log = (item: any) => {
if (!logs) {
logs = fs.createWriteStream(baseDir + `/logs/logs-${date.toISOString()}.log`, { flags: 'a' })
}
logs.write(`[${new Date().toISOString()}]: ${item}\n`)
}
console.error = (item: any) => {
if (!errs) {
errs = fs.createWriteStream(baseDir + `/logs/errors-${date.toISOString()}.log`, { flags: 'a' })
}
errs.write(`[${new Date().toISOString()}]: ${item}\n`)
}
}

0
src/app/views/home.ejs Normal file
View file

60
src/app/views/login.ejs Normal file
View file

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Geolosys Configurator Login</title>
<link rel="stylesheet" href="/Libraries/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/Libraries/bootstrap/css/bootstrap.css">
<script src="/Libraries/bootstrap/js/bootstrap.js"></script>
<script src="/Custom/js/login.js"></script>
</head>
<body>
<div class="container d-flex flex-column justify-content-center"
style="width: 98vw; height: 100vh; max-width: 768px; max-height: 100vh">
<div class="d-block text-center header">
<h2>
Geolosys Configurator Login
</h2>
<h4>
By: oitsjustjose (Jose)
</h4>
</div>
<form action="/login" method="POST">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" name="username" id="username">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Log In</button>
</form>
</div>
<div class="modal" tabindex="-1" id="notification">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>