Add basic user creation

This commit is contained in:
Quad 2022-04-13 23:42:12 +02:00
parent 361d3ebbcb
commit d7f7f547e4
5 changed files with 81 additions and 9 deletions

View File

@ -1,7 +1,7 @@
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
displayname TEXT NOT NULL,
username TEXT UNIQUE NOT NULL,
displayname TEXT NOT NULL,
password TEXT NOT NULL
);

45
src/modules/auth.nim Normal file
View File

@ -0,0 +1,45 @@
import db
import formatter
proc addUser(username: string, displayname: string, password: string): bool =
# Hopefully hashing and stuff will be implemented here at some point
return createUser(username=username, displayname=displayname, password=password)
# Extremely rudimentary, must be fixed later.
proc addUserInteractive(): bool =
var userConsent: bool = false
while userConsent != true:
stdout.write "Did not find a user. Create one now? (y/n) "
stdout.flushFile()
var userInput = readChar(stdin)
stdin.flushFile()
case userInput
of 'y', 'Y':
userConsent = true
of 'n', 'N':
return false
else:
discard
stdout.write "Enter a username: "
stdout.flushFile()
var username = readLine(stdin)
stdin.flushFile()
stdout.write "Enter a display name: "
stdout.flushFile()
var displayname = readLine(stdin)
stdin.flushFile()
stdout.write "Enter a password: "
stdout.flushFile()
var password = readLine(stdin)
stdin.flushFile()
if addUser(username=username, displayname=displayname, password=password):
logPrint("User " & username & " with displayname " & displayname & " has been created", "Main")
return true
else:
return false
export addUserInteractive

View File

@ -3,7 +3,7 @@ import prologue/middlewares/staticfile
import views
import formatter
proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string]) =
proc runWebsite(settings: Table[string, bool]) =
let prologueSettings = newSettings(
debug = settings["debug"]
)

View File

@ -2,8 +2,30 @@ import tables
import std/[os, db_sqlite, strutils, streams]
import formatter
proc connectDb(dbPath: string = "data/blog.db"): DbConn =
var db = open(dbPath, "", "", "")
return db
proc createUser(username: string, displayname: string, password: string): bool =
try:
var db = connectDb()
db.exec(sql"INSERT INTO users (username,displayname,password) VALUES (?, ?, ?)", username, displayname, password)
db.close()
return true
except:
return false
proc checkUser(): bool =
var db = connectDb()
var response = db.getRow(sql"SELECT * FROM users")
db.close()
if response[0] != "":
return true
else:
return false
proc createDb(dbSettings: Table): bool =
if not fileExists(dbSettings["path"]):
if not fileExists("data/blog.db"):
var userConsent: bool = false
while userConsent != true:
stdout.write "Did not find database. Create one now? (y/n) "
@ -20,7 +42,7 @@ proc createDb(dbSettings: Table): bool =
let
schema = readFile(dbSettings["schema_path"])
db = open(dbSettings["path"], "", "", "")
db = connectDb()
for command in schema.split(";"):
# Skip "command" if it's just a blank line
if command == "\c\n" or command == "\n":
@ -30,4 +52,4 @@ proc createDb(dbSettings: Table): bool =
db.close()
return true
export createDb
export createDb, checkUser, createUser

View File

@ -1,7 +1,7 @@
when isMainModule:
import tables
import os
import modules/[blog, env, db, formatter]
import modules/[blog, env, db, formatter, auth]
let dataDir: string = "data"
@ -14,7 +14,6 @@ when isMainModule:
logPrint("Debug mode is enabled", "Warning")
let dbSettings = {
"path": dataDir & "/" & "blog.db",
"schema_path": "schema.sql"
}.toTable()
@ -25,10 +24,16 @@ when isMainModule:
createDir(dataDir)
if not createDb(dbSettings):
logPrint("Failed to prepare database", "Error")
logPrint("A database must be created before use", "Error")
quit(QuitFailure)
if not checkUser():
logPrint("No user in database", "Warning")
if not addUserInteractive():
logPrint("An initial user must be created before use", "Error")
quit(QuitFailure)
try:
runWebsite(settings=settings, dbSettings=dbSettings)
runWebsite(settings=settings)
except:
logPrint("Could not run website", "Error")