moved proc and type defs to own files

This commit is contained in:
array-in-a-matrix 2024-06-28 11:32:20 -04:00
parent 6dfb591cb8
commit 218216735f
2 changed files with 49 additions and 0 deletions

12
src/server/files.nim Normal file
View file

@ -0,0 +1,12 @@
import norm/model
import users
# file objects are owned by a user
type File* = ref object of Model
owner*: User
path*: string
tags*: string #? This is a temporary hack should be of type `tags: seq[string]` instead
# creates a new file object and sets default values, recommended by the norm documentation
func newFile*(user: User = newUser(), path: string = "", tags: string = ""): File =
File(owner: user, path: path, tags: tags)

37
src/server/users.nim Normal file
View file

@ -0,0 +1,37 @@
import std/[random, with, times]
import norm/[model, sqlite]
import checksums/sha3
# define user object
type User* = ref object of Model
username*: string # should be unique
email*: string # should be unique
password*: string
token*: string # should be unique
# generate secure login token
proc generateToken*(username: string = ""): string =
randomize()
with result:
add $getTime().nanosecond()
add $rand(1_000_000_000)
add username
add $(getTime().toUnix())
$Sha3_512.secureHash(result)
# checks if the provided token exists in the database
proc validToken*(db: DbConn, user: var User, token: string): bool =
try:
db.select(user, "token = ?", token)
return true
except NotFoundError:
return false
# update user's token using a newly generated token
proc genNewToken*(db: DbConn, user: var User) =
user.token = generateToken(user.username)
db.update(user)
# creates a new user object and sets default values, recommended by the norm documentation
proc newUser*(username: string = "", email: string = "", password: string = ""): User =
User(username: username, email: email, password: password, token: generateToken(username))