big commit
This commit is contained in:
parent
e96f85efdc
commit
dcae12453c
26 changed files with 3183 additions and 8 deletions
47
src/lib/Area.svelte
Normal file
47
src/lib/Area.svelte
Normal file
|
@ -0,0 +1,47 @@
|
|||
<style>
|
||||
#content {
|
||||
background: var(--dark-1);
|
||||
|
||||
width: min(700px, 90vw);
|
||||
padding: 20px;
|
||||
padding-top: 8px;
|
||||
margin: 25px;
|
||||
|
||||
border-radius: 25px;
|
||||
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#header {
|
||||
font-weight: bold;
|
||||
|
||||
font-size: 2rem;
|
||||
|
||||
width: 100%;
|
||||
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#main {
|
||||
min-height: 250px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div id='content'>
|
||||
<div id='header'>
|
||||
<slot name="header">
|
||||
Header
|
||||
</slot>
|
||||
</div>
|
||||
<div id='main'>
|
||||
<slot name="main">
|
||||
<p>Content</p>
|
||||
</slot>
|
||||
</div>
|
||||
<div id='footer'>
|
||||
<slot name="footer">
|
||||
<p>Footer</p>
|
||||
</slot>
|
||||
</div>
|
||||
</div>
|
15
src/lib/Form.svelte
Normal file
15
src/lib/Form.svelte
Normal file
|
@ -0,0 +1,15 @@
|
|||
<script>
|
||||
import Area from '$lib/Area.svelte';
|
||||
</script>
|
||||
|
||||
<Area>
|
||||
<p slot="header">
|
||||
Welcome to SvelteKit
|
||||
</p>
|
||||
<p slot="main">
|
||||
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation
|
||||
</p>
|
||||
<p slot="footer">
|
||||
|
||||
</p>
|
||||
</Area>
|
15
src/lib/Post.svelte
Normal file
15
src/lib/Post.svelte
Normal file
|
@ -0,0 +1,15 @@
|
|||
<script>
|
||||
import Area from '$lib/Area.svelte';
|
||||
</script>
|
||||
|
||||
<Area>
|
||||
<p slot="header">
|
||||
Welcome to SvelteKit
|
||||
</p>
|
||||
<p slot="main">
|
||||
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation
|
||||
</p>
|
||||
<p slot="footer">
|
||||
|
||||
</p>
|
||||
</Area>
|
108
src/lib/db.js
Normal file
108
src/lib/db.js
Normal file
|
@ -0,0 +1,108 @@
|
|||
import sqlite3 from 'sqlite3'
|
||||
import { open } from 'sqlite'
|
||||
import { hash, compare } from 'bcrypt'
|
||||
|
||||
const {
|
||||
randomBytes
|
||||
} = await import('node:crypto');
|
||||
|
||||
var db;
|
||||
async function initDb() {
|
||||
db = await open({
|
||||
filename: `${process.cwd()}/db/main.db`,
|
||||
driver: sqlite3.Database
|
||||
});
|
||||
|
||||
await db.run('CREATE TABLE IF NOT EXISTS auth (username CHAR(64), password CHAR(1024))');
|
||||
await db.run('CREATE TABLE IF NOT EXISTS token (username CHAR(64), token CHAR(1024))');
|
||||
await db.run('CREATE TABLE IF NOT EXISTS post (username CHAR(64), id CHAR(64), content CHAR(10240), upvotes INTEGER, downvotes INTEGER)');
|
||||
await db.run('CREATE TABLE IF NOT EXISTS vote (id CHAR(64), username CHAR(64), type INTEGER)');
|
||||
}
|
||||
|
||||
async function registerBackend({user, pass}) {
|
||||
if (!db) await initDb();
|
||||
|
||||
var existingAccounts = await db.all('SELECT username FROM auth WHERE username = ?',[
|
||||
user
|
||||
]);
|
||||
|
||||
if (existingAccounts && existingAccounts.length > 0)
|
||||
return { success: 'Account already exists.' };
|
||||
|
||||
var passHash = await hash(pass,10);
|
||||
|
||||
await db.run('INSERT INTO auth (username, password) VALUES (?, ?)', [
|
||||
user,
|
||||
passHash
|
||||
])
|
||||
|
||||
return { success: 'Successfully created account.' };
|
||||
}
|
||||
|
||||
async function loginBackend({user, pass}) {
|
||||
if (!db) await initDb();
|
||||
|
||||
var existingAccounts = await db.all('SELECT username, password FROM auth WHERE username = ?',[
|
||||
user
|
||||
]);
|
||||
|
||||
if (!existingAccounts || existingAccounts.length < 1)
|
||||
return { success: 'Account does not exist.' };
|
||||
|
||||
var passHash = await compare(pass,existingAccounts[0].password);
|
||||
|
||||
if (!passHash)
|
||||
return { success: 'Incorrect password.' };
|
||||
|
||||
var token = randomBytes(256).toString('hex');
|
||||
|
||||
await db.run('INSERT INTO token (username, token) VALUES (?, ?)', [
|
||||
user,
|
||||
token
|
||||
])
|
||||
|
||||
return { success: 'Successfully logged into account.', token };
|
||||
}
|
||||
|
||||
async function postCreateBackend({user, content}) {
|
||||
var id = randomBytes(10).toString('hex');
|
||||
|
||||
await db.run('INSERT INTO post (username, id, content) VALUES (?, ?, ?)', [
|
||||
user,
|
||||
id,
|
||||
content
|
||||
])
|
||||
}
|
||||
|
||||
async function postGetBackend({id}) {
|
||||
var posts = await db.all('SELECT * from post WHERE id = ?', [
|
||||
id
|
||||
])
|
||||
|
||||
if (!posts || posts.length < 1) {
|
||||
return {'success': 'Post does not exist.'}
|
||||
}
|
||||
|
||||
return posts[0];
|
||||
}
|
||||
|
||||
async function tokenBackend({token}) {
|
||||
if (!db) await initDb();
|
||||
|
||||
var existingAccounts = await db.all('SELECT username from token WHERE token = ?',[
|
||||
token
|
||||
]);
|
||||
|
||||
if (!existingAccounts || existingAccounts.length < 1)
|
||||
return false;
|
||||
|
||||
return existingAccounts[0].username;
|
||||
}
|
||||
|
||||
export {
|
||||
registerBackend,
|
||||
loginBackend,
|
||||
tokenBackend,
|
||||
postCreateBackend,
|
||||
postGetBackend
|
||||
}
|
26
src/lib/util.js
Normal file
26
src/lib/util.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
let checkLength = function(string, field, lowerBound, upperBound) {
|
||||
if (string.length < lowerBound) {
|
||||
if (string.length == 0) {
|
||||
return { success: `${field} cannot be blank.` }
|
||||
}
|
||||
return { success: `${field} is too short (minimum length: ${lowerBound} characters).` }
|
||||
}
|
||||
|
||||
if (string.length > upperBound) {
|
||||
return { success: `${field} is too long (maximum length: ${upperBound} characters).` }
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
let checkRegex = function(string, field, regex) {
|
||||
if (string.search(regex) != -1) {
|
||||
return { success: `${field} contains illegal characters.` }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export {
|
||||
checkLength,
|
||||
checkRegex
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue