parent
ee5402ab44
commit
32913d582a
@ -1 +1,32 @@ |
||||
LogIn.jsx |
||||
import { useForm } from "react-hook-form"; |
||||
import { useNavigate } from 'react-router-dom'; |
||||
import { logIn } from '../apiTools.jsx'; |
||||
|
||||
function LogIn() { |
||||
const { register, handleSubmit } = useForm(); |
||||
const navigate = useNavigate(); |
||||
|
||||
const onSubmit = (d) => { |
||||
logIn(d) |
||||
.then(() => navigate('/')) |
||||
.catch((error) => console.log('error logging in: ', error)); |
||||
}; |
||||
|
||||
return ( |
||||
<form onSubmit={handleSubmit(onSubmit)}> |
||||
<label> |
||||
User name: |
||||
<input {...register("name")} /> |
||||
<br/> |
||||
</label> |
||||
<label> |
||||
Password: |
||||
<input {...register("password")} /> |
||||
<br/> |
||||
</label> |
||||
<button type="submit">Log right in, buckaroo!</button> |
||||
</form> |
||||
); |
||||
} |
||||
|
||||
export default LogIn; |
@ -0,0 +1,37 @@ |
||||
import { useForm } from "react-hook-form"; |
||||
import { useNavigate } from "react-router-dom"; |
||||
import { createAccount } from "../apiTools.jsx"; |
||||
|
||||
function Register() { |
||||
const { register, handleSubmit } = useForm(); |
||||
const navigate = useNavigate(); |
||||
|
||||
const onSubmit = (d) => { |
||||
createAccount(d) |
||||
.then(() => navigate("/login")) |
||||
.catch((error) => console.log("error: ", error)); |
||||
}; |
||||
|
||||
return ( |
||||
<form onSubmit={handleSubmit(onSubmit)}> |
||||
<label> |
||||
User name: |
||||
<input {...register("name")} /> |
||||
<br/> |
||||
</label> |
||||
<label> |
||||
Password: |
||||
<input {...register("password")} /> |
||||
<br/> |
||||
</label> |
||||
<label> |
||||
Nonce password from shoofle: |
||||
<input {...register("nonce")} /> |
||||
<br/> |
||||
</label> |
||||
<button type="submit">Request Account!</button> |
||||
</form> |
||||
); |
||||
} |
||||
|
||||
export default Register; |
@ -1,13 +0,0 @@ |
||||
const reportWebVitals = onPerfEntry => { |
||||
if (onPerfEntry && onPerfEntry instanceof Function) { |
||||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { |
||||
getCLS(onPerfEntry); |
||||
getFID(onPerfEntry); |
||||
getFCP(onPerfEntry); |
||||
getLCP(onPerfEntry); |
||||
getTTFB(onPerfEntry); |
||||
}); |
||||
} |
||||
}; |
||||
|
||||
export default reportWebVitals; |
@ -1,72 +1,122 @@ |
||||
const express = require('express'); |
||||
const showdown = require('showdown'); |
||||
const argon2 = require('argon2'); |
||||
|
||||
const { query } = require('./dbHelper.js'); |
||||
const { graphFromList } = require('./graphStuff.js'); |
||||
|
||||
const app = express.Router(); |
||||
const converter = new showdown.Converter(); |
||||
|
||||
const sqlite = require("better-sqlite3"); |
||||
const db = new sqlite('the_big_db.db', { verbose: console.log }); |
||||
console.log(db); |
||||
|
||||
app.get('/pages', (req, res) => { |
||||
query('select id, title from pages', []) |
||||
.then((r) => r.rows) |
||||
.then((row) => row.map(([id, title]) => {return {"id": id, "title": title};})) |
||||
.then((pages) => res.status(200).json(pages)) |
||||
.catch((error) => res.status(500).json({"error": error})); |
||||
try { |
||||
const pages = db.prepare('select id, title from pages').all(); |
||||
res.status(200).json(pages); |
||||
} catch (error) { |
||||
res.status(500).json({"error": error}); |
||||
}
|
||||
}); |
||||
|
||||
app.post('/page/new', (req, res) => { |
||||
query('insert into pages (title, description) values (?, ?) returning id', |
||||
["new page", "this page is new!"]) |
||||
.then((r) => r.rows[0]) |
||||
.then((row) => res.status(200).json({id: row[0]})) |
||||
.catch((error) => res.status(500).json({"error": error})) |
||||
try { |
||||
const newPage = db.prepare('insert into pages (title, description) values (?, ?) returning id') |
||||
.get("new page", "this page is new!"); |
||||
res.status(200).json(newPage); |
||||
} catch (error) { |
||||
res.status(500).json({"error": error}); |
||||
} |
||||
}); |
||||
|
||||
app.get('/page/:id', (req, res) => { |
||||
query('select * from pages where id=?', [req.params.id]) |
||||
.then((r) => { |
||||
if (r.rows.length == 0) res.status(404).json({"error": "page not found in db"}); |
||||
else return r.rows[0]; |
||||
}).then((row) => res.status(200).json(row)) |
||||
.catch((error) => res.status(500).json({"error": error})); |
||||
try { |
||||
const page = db.prepare('select * from pages where id=:id').get(req.params); |
||||
if (page === undefined) res.status(404).json({"error": "page not found"}); |
||||
else res.status(200).json(page); |
||||
} catch (error) { |
||||
res.status(500).json({"error": error}); |
||||
} |
||||
}); |
||||
|
||||
app.post('/page/:id', (req, res) => { |
||||
const html = converter.makeHtml(req.body.description); |
||||
query('replace into pages (id, title, description, html) values (?, ?, ?, ?)', |
||||
[req.params.id, req.body.title, req.body.description, html]) |
||||
.then(() => res.status(200).json({})) |
||||
.catch((error) => res.status(500).json({"error": error})); |
||||
try { |
||||
const html = converter.makeHtml(req.body.description); |
||||
const changes = db.prepare('replace into pages (id, title, descriptioon, html) values (?, ?, ?, ?)') |
||||
.run(req.params.id, req.body.title, req.body.description, html); |
||||
res.status(200).json(changes); |
||||
} catch (error) { |
||||
res.status(500).json({"error": error}); |
||||
} |
||||
}); |
||||
|
||||
app.delete('/page/:id', (req, res) => { |
||||
query('delete from pages where id = ?', [req.params.id]) |
||||
.then(() => res.status(200).json({id: req.params.id})) |
||||
.catch((error) => res.status(500).json({"error": error})) |
||||
try { |
||||
const changes = db.prepare('delete from pages where id = ?').run(req.params.id); |
||||
res.status(200).json({id: req.params.id}); |
||||
} catch (error) { |
||||
res.status(500).json({"error": error}); |
||||
} |
||||
}) |
||||
|
||||
app.get('/graph', (req, res) => { |
||||
query('select id, html from pages', []) |
||||
.then((r) => r.rows) |
||||
.then((stuff) => graphFromList(stuff)) |
||||
.then((graph) => res.json(graph)) |
||||
.catch((error) => res.status(500).json({"error": error})); |
||||
try { |
||||
const rows = db.prepare('select id, html from pages').all(); |
||||
const graph = graphFromList(rows); |
||||
res.status(200).json(graph); |
||||
} catch (error) { |
||||
res.status(500).json({"error": error}); |
||||
} |
||||
}); |
||||
|
||||
// auth stuff
|
||||
app.post('/register', (req, res) => { |
||||
// parse the body, which miight be url-encoded?
|
||||
app.post('/register', async (req, res) => { |
||||
const {name, password, nonce} = req.body; |
||||
|
||||
const oldUser = db.prepare('select name from users where name=?').get(name); |
||||
if (oldUser) return res.status(500).json({"error": "user name already in use"}); |
||||
|
||||
// check if the nonce password is correctt
|
||||
// hash the password
|
||||
// add tthe user to the users database
|
||||
if (nonce != "a softer birdsong") return res.status(500).json({"error": "wrong nonce"}); |
||||
|
||||
try { |
||||
// i'm told argon2 is the good one nowatimes
|
||||
const hash = await argon2.hash(password); |
||||
const inserted = db.prepare('insert into users (name, password) values (?, ?)').run(name, hash); |
||||
res.status(200).json(inserted); |
||||
} catch (error) { |
||||
res.status(500).json({"error": error}); |
||||
} |
||||
}); |
||||
|
||||
app.post('/login', async (req, res) => { |
||||
if (req.session.name) { |
||||
return res.status(200).json({message: "already logged in", name: req.session.name}); |
||||
} |
||||
|
||||
const {name, password} = req.body; |
||||
|
||||
// fetch username and passswords from the db
|
||||
const storedUser = db.prepare('select name, password from users where name = ?').get(name); |
||||
if (!storedUser) { |
||||
return res.status(401).json({"error": "password/username combo not found in database"}); |
||||
} |
||||
|
||||
//check if the passss hashes mattch and log in
|
||||
if (!(await argon2.verify(storedUser.password, password))) { |
||||
return res.status(401).json({"error": "password/username combo not found in database"}); |
||||
} |
||||
|
||||
// set the session cookie and rreturn 200!
|
||||
req.session.name = name; |
||||
return res.status(200).json({message: "successfully logged in!", name: name}); |
||||
}); |
||||
|
||||
app.post('/login', (req, res) => { |
||||
// hash the password
|
||||
// check if the user/hashed pass matches
|
||||
// if not, throw an error
|
||||
// create a valid ttoken?
|
||||
// return token
|
||||
app.post('/logout', (req, res) => { |
||||
req.session.destroy(); |
||||
res.status(200).json({message: "successfully logged out"}); |
||||
}); |
||||
|
||||
module.exports = app; |
@ -1,19 +0,0 @@ |
||||
// helper function(s?) to make a query to the database
|
||||
|
||||
const db_url = 'http://127.0.0.1:8000' |
||||
|
||||
async function query(q, params) { |
||||
return fetch(db_url, { |
||||
method: 'POST', |
||||
headers: {'Content-Type': 'application/json'}, |
||||
body: JSON.stringify({ |
||||
statements: [{ |
||||
q: q, |
||||
params: params |
||||
}] |
||||
}) |
||||
}).then((res) => res.json()) |
||||
.then((data) => data[0].results); |
||||
} |
||||
|
||||
module.exports = { query: query }; |
@ -0,0 +1,31 @@ |
||||
const sqlite = require("better-sqlite3"); |
||||
const sqlite3_db = new sqlite('./the_big_db.db', { verbose: console.log }); |
||||
|
||||
const libsql = require("@libsql/client"); |
||||
const config = {url: "http://127.0.0.1:8000"}; |
||||
const libsql_db = libsql.createClient(config); |
||||
|
||||
async function copyPages() { |
||||
console.log("copying everythting from hte old db"); |
||||
const pages = (await libsql_db.execute(`select id, title, description, html from pages`)).rows; |
||||
|
||||
const insertPage = sqlite3_db.prepare(`replace into pages (id, title, description, html) values (:id, :title, :description, :html)`); |
||||
pages.forEach((page) => { |
||||
console.log(`inserting page ${page.id}`) |
||||
insertPage.run(page); |
||||
}); |
||||
} |
||||
|
||||
async function copyUsers() { |
||||
console.log("copying users now"); |
||||
const users = (await libsql_db.execute(`select name, password from users`)).rows; |
||||
|
||||
const insertUser = sqlite3_db.prepare(`replace into users (name, password) values (:name, :password)`); |
||||
users.forEach((user) => {
|
||||
console.log(`inserting ${user.name}, ${user.password}`);
|
||||
insertUser.run(user);
|
||||
}); |
||||
} |
||||
|
||||
copyPages(); |
||||
copyUsers(); |
@ -0,0 +1,48 @@ |
||||
const sqlite = require("better-sqlite3"); |
||||
const showdown = require("showdown"); |
||||
const db = new sqlite('./the_big_db.db', { verbose: console.log }); |
||||
|
||||
const converter = new showdown.Converter(); |
||||
|
||||
const createPages = db.prepare(` |
||||
create table if not exists pages ( |
||||
id integer primary key, |
||||
title varchar(255), |
||||
description text, |
||||
html text |
||||
) |
||||
`);
|
||||
|
||||
const createUsers = db.prepare(` |
||||
create table if not exists users ( |
||||
name varchar(64) primary key, |
||||
password varchar(128) |
||||
) |
||||
`);
|
||||
|
||||
|
||||
function initDb() { |
||||
console.log("creating page table") |
||||
console.log(createPages.run()); |
||||
|
||||
console.log("finding pages that havent been rendered"); |
||||
rows = db.prepare(`select * from pages where html is null`).all(); |
||||
console.log(rows); |
||||
|
||||
const insertPage = db.prepare(`replace into pages (id, title, description, html) values (:id, :title, :description, :html)`); |
||||
|
||||
rows.forEach((pageData) => { |
||||
const {id, title, description, html} = pageData; |
||||
console.log(`rendering page number ${id}`) |
||||
console.log(`${id}. ${title}: ${description} ( ${html} )`); |
||||
|
||||
const renderedPage = converter.makeHtml(description); |
||||
|
||||
insertPage.run({...pageData, html: renderedPage}); |
||||
}); |
||||
|
||||
console.log("creating user table"); |
||||
console.log(createUsers.run()); |
||||
} |
||||
|
||||
initDb(); |
@ -0,0 +1,4 @@ |
||||
const sqlite = require("better-sqlite3"); |
||||
const db = new sqlite('./the_big_db.db', { verbose: console.log }); |
||||
|
||||
console.log(db.prepare("select name from sqlite_master where type='table'").all()); |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue