check if username and email are unique

This commit is contained in:
array-in-a-matrix 2024-08-07 10:58:46 -04:00
parent c9073ed8c6
commit fb74d955ac
2 changed files with 17 additions and 5 deletions

View file

@ -15,11 +15,23 @@ proc createAuthenticationRoutes*() =
returns: JSON returns: JSON
]# ]#
post "/api/v1/newUser": post "/api/v1/newUser":
# TODO: sanitization + check if username and email are unique
if @"username".isEmptyOrWhitespace() or @"email".isEmptyOrWhitespace() or if @"username".isEmptyOrWhitespace() or @"email".isEmptyOrWhitespace() or
@"password".isEmptyOrWhitespace(): @"password".isEmptyOrWhitespace():
resp Http403, "Not all required parameters are provided.\n" resp Http403, "Not all required parameters are provided.\n"
block UniqueParametersCheck:
try:
var user = newUser()
db.select(user, """"User".username = $1""", @"username")
except NotFoundError:
try:
var user = newUser()
db.select(user, """"User".email = $1""", @"email")
except NotFoundError:
break UniqueParametersCheck
resp Http403, "A user with that email already exists.\n"
resp Http403, "A user with that username already exists.\n"
var user = newUser(@"username", @"email", @"password") var user = newUser(@"username", @"email", @"password")
db.insert(user) db.insert(user)

View file

@ -1,13 +1,13 @@
import std/oids import std/oids
import norm/[model, postgres] import norm/[model, postgres, pragmas]
import checksums/sha3 import checksums/sha3
# define user object # define user object
type User* = ref object of Model type User* = ref object of Model
username*: string # should be unique username* {.unique.}: string
email*: string # should be unique email* {.unique.}: string
password*: string # sha3-512 hash password*: string # sha3-512 hash
token*: string # should be unique token* {.unique.}: string
fileCount*: int = 0 fileCount*: int = 0
# checks if the provided token exists in the database # checks if the provided token exists in the database