31 lines
821 B
Perl
31 lines
821 B
Perl
|
:- http_handler(
|
||
|
root(login),
|
||
|
login_route,
|
||
|
[]
|
||
|
).
|
||
|
|
||
|
% Handles logging in, returning the correct Json in Reply.
|
||
|
handle_login(Username, Password, Reply) :-
|
||
|
user(Username, Hash),
|
||
|
crypto_password_hash(Password, Hash), !,
|
||
|
crypto_n_random_bytes(16, Bytes),
|
||
|
hex_bytes(Token, Bytes),
|
||
|
assert_token(Username, Token),
|
||
|
prolog_to_json(token(Username, Token), Reply).
|
||
|
|
||
|
handle_login(Username, _, Reply) :-
|
||
|
user(Username, _), !,
|
||
|
prolog_to_json(error(incorrect_password), Reply).
|
||
|
|
||
|
handle_login(_, _, Reply) :-
|
||
|
prolog_to_json(error(unknown_user), Reply).
|
||
|
|
||
|
|
||
|
% POST /login
|
||
|
login_route(Request) :-
|
||
|
member(method(post), Request), !,
|
||
|
http_read_data(Request, Json, []),
|
||
|
json_to_prolog(Json, login(Username, Password)),
|
||
|
handle_login(Username, Password, Reply),
|
||
|
reply_json(Reply).
|