Compare commits

...

2 Commits

Author SHA1 Message Date
absc 640ffe4d75 Corrected stuffs found through the dialyzer. 2024-08-11 20:56:11 +00:00
absc 2b530fbe7e Added some more detailed responses to the JSON API calls. 2024-08-11 20:49:48 +00:00
2 changed files with 21 additions and 16 deletions

View File

@ -146,7 +146,7 @@ resource_exists(Req, State) ->
#{dudename := User} = cowboy_req:match_cookies([dudename], Req),
{ok, Bucket} = maps:find(bucket, State),
case dudeswave_auth:user_details(User, Bucket) of
case dudeswave_auth:details(User, Bucket) of
[] ->
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}),
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
@ -193,8 +193,8 @@ delete_completed(Req, State) -> {false, Req, State}.
%
login(Req, State) ->
{ok, Cookies} = maps:find(State, cookies),
{ok, Bucket} = maps:find(State, bucket),
{ok, Cookies} = maps:find(cookies, State),
{ok, Bucket} = maps:find(bucket, State),
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"user">> := User, <<"password">> := Pass} = json:decode(Data),

View File

@ -181,8 +181,11 @@ forbidden(Req, State) ->
dudename], Req),
{ok, Bucket} = maps:find(cookies, State),
case dudeswave_auth:authenticate({cookie, User, Auth}, Bucket) of
{error, service_unavailable} -> exit(service_unavailable);
case dudeswave_auth:authenticate(User, Auth, Bucket) of
{error, service_unavailable} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{true, Req0, State};
true ->
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
@ -215,7 +218,7 @@ resource_exists(Req, State) ->
#{dudename := User} = cowboy_req:match_cookies([dudename], Req),
{ok, Bucket} = maps:find(bucket, State),
case dudeswave_auth:user_details(User, Bucket) of
case dudeswave_auth:details(User, Bucket) of
[] ->
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}),
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
@ -268,35 +271,37 @@ create_user(Req, State) ->
{ok, Bucket} = maps:find(bucket, State),
#{dudename := User} = cowboy_req:match_cookies([dudename], Req),
#{<<"password">> := Pass, <<"email">> := Email} = json:decode(cowboy_req:body(req)),
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"password">> := Pass, <<"email">> := Email} = json:decode(Data),
case dudeswave_auth:new_user(User, Pass, Email, Bucket) of
case dudeswave_auth:new(User, Pass, Email, Bucket) of
ok ->
Resp = json:encode(#{<<"result">> => <<"created">>}),
Req0 = cowboy_req:reply(201, #{}, Resp, Req),
{true, Req0, []};
{error, Reason} ->
Req1 = cowboy_req:reply(201, #{}, Resp, Req0),
{true, Req1, []};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, Reason}
Req1 = cowboy_req:reply(500, #{}, Resp, Req0),
{false, Req1, State}
end.
modify_user(Req, State) ->
{ok, Bucket} = maps:find(bucket, State),
#{dudename := User} = cowboy_req:match_cookies([dudename], Req),
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"email">> := Email, <<"description">> := Desc,
<<"name">> := Name} = json:decode(cowboy_req:body(req)),
<<"name">> := Name} = json:decode(Data),
case dudeswave_auth:update(User, Name, Email, Desc, Bucket) of
ok ->
Resp = json:encode(#{<<"result">> => <<"details updated">>}),
Req0 = cowboy_req:reply(200, #{}, Resp, Req),
{true, Req0, []};
{error, Reason} ->
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, Reason}
{false, Req0, State}
end.
user_details(Req, State) ->