fix db error on bad user name login

This commit is contained in:
Swrup 2022-02-22 00:15:30 +01:00
parent c91623050e
commit 54e5511ca1

View file

@ -40,7 +40,7 @@ module Q = struct
"SELECT nick FROM user;"
let get_user =
Caqti_request.find_opt Caqti_type.string
Caqti_request.find Caqti_type.string
Caqti_type.(tup4 string string string Caqti_type.(tup2 string string))
"SELECT * FROM user WHERE nick=?;"
@ -70,7 +70,10 @@ let () =
(List.map (fun query -> Db.exec query ()) tables)
then Dream.error (fun log -> log "can't create table")
let exists nick = Result.is_ok (Db.find Q.get_user nick)
let login ~nick ~password request =
if exists nick then
let^? good_password = Db.find_opt Q.get_password nick in
if Bcrypt.verify password (Bcrypt.hash_of_string good_password) then
let _ =
@ -79,6 +82,7 @@ let login ~nick ~password request =
in
Ok ()
else Error "wrong password"
else Error "wrong user name"
let register ~email ~nick ~password =
(* TODO: remove bad characters (e.g. delthas) *)
@ -168,5 +172,3 @@ let upload_avatar files nick =
let^ () = Db.exec Q.upload_avatar (content, nick) in
Ok ()
| _files -> Error "More than one file provided"
let exists nick = Result.is_ok (Db.find_opt Q.get_user nick)