check nick and email are unique

This commit is contained in:
pena 2021-11-07 00:54:35 +01:00 committed by Swrup
parent 82e65cfad4
commit a16748c81f
2 changed files with 17 additions and 7 deletions

View file

@ -5,7 +5,7 @@ let f ?email ?nick ?password request =
% begin match User.register ~email ~nick ~password with % begin match User.register ~email ~nick ~password with
% | Error e -> % | Error e ->
Error: <%s e %> Error: <%s e %>
% | Ok _ -> % | Ok _ (* TODO: do not match on _ *) ->
User created ! User created !
% end; % end;
% | _ -> % | _ ->

View file

@ -48,8 +48,16 @@ let register ~email ~nick ~password =
if not valid then if not valid then
Error "Something is wrong" Error "Something is wrong"
else else
(* TODO: add check uniqueness of id *)
let open Sqlite3_utils in let open Sqlite3_utils in
let unique =
Db.with_db (fun db ->
exec_raw_args db
"SELECT EXISTS(SELECT 1 FROM user WHERE nick=? OR email=?);"
[| Data.TEXT nick; Data.TEXT email |]
~f:Cursor.to_list )
in
match unique with
| Ok [ [| Data.INT 0L |] ] -> (
let res = let res =
Db.with_db (fun db -> Db.with_db (fun db ->
exec_raw_args db "INSERT INTO user VALUES (?, ?, ?);" exec_raw_args db "INSERT INTO user VALUES (?, ?, ?);"
@ -58,4 +66,6 @@ let register ~email ~nick ~password =
in in
match res with match res with
| Ok res -> Ok res | Ok res -> Ok res
| Error e -> Error (Format.sprintf "db error: %s" (Rc.to_string e)) )
| Ok _ -> Error "nick or email already exists"
| Error e -> Error (Format.sprintf "db error: %s" (Rc.to_string e)) | Error e -> Error (Format.sprintf "db error: %s" (Rc.to_string e))