Make reading the registration data a more abstract operation.

main
absc 2024-08-15 21:54:41 +00:00
parent b3cdc8ee45
commit bbb44f9337
2 changed files with 38 additions and 12 deletions

View File

@ -26,7 +26,7 @@ from the dudeswave database.
-export([authenticate/2, details/1, new/3,
update/4, delete/1, logout/2, auth_cookies/1, invalidate_cookies/1,
set_auth_cookies/4, read_login_data/1]).
set_auth_cookies/4, read_login_data/1, read_new_user_data/1]).
-doc """
Verify a session with an existing cookie.
@ -346,4 +346,32 @@ read_login_data(Req) ->
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"user">> := User, <<"password">> := Pass} = json:decode(Data),
{User, Pass, Req0}.
{User, Pass, Req0}.
-doc """
Read new registration informations from the request
Spec:
```
-spec read_new_user_data(Req) -> {User, Pass, Email Req0} when
Req :: cowboy_req:req(),
User :: binary(),
Pass :: binary(),
Email :: binary(),
Req0 :: cowboy_req:req().
```
""".
-spec read_new_user_data(Req) -> {User, Pass, Email, Req0} when
Req :: cowboy_req:req(),
User :: binary(),
Pass :: binary(),
Email :: binary(),
Req0 :: cowboy_req:req().
read_new_user_data(Req) ->
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"user">> := User, <<"password">> := Pass,
<<"email">> := Email} = json:decode(Data),
{User, Pass, Email, Req0}.

View File

@ -76,6 +76,7 @@ PUT /api/v1/user
```
{
"username": "foo",
"email": "foo@example.com",
"password": "123456"
}
@ -148,10 +149,8 @@ forbidden(Req, State) ->
{User, Auth} = dudeswave_auth:auth_cookies(Req),
case dudeswave_auth:authenticate(User, {cookie, Auth}) of
{error, service_unavailable} ->
{true, Req, State};
true ->
{false, Req, State};
{error, service_unavailable} -> {true, Req, State};
true -> {false, Req, State};
false -> {true, Req, State}
end
end.
@ -180,8 +179,10 @@ resource_exists(Req, State) ->
{User, _} = dudeswave_auth:auth_cookies(Req),
case dudeswave_auth:details(User) of
[] -> {false, Req, State};
{error, _} -> {false, Req, State};
[] ->
{false, Req, State};
{error, _} ->
{false, Req, State};
Details ->
NewState = State#{
details => Details,
@ -215,10 +216,7 @@ delete_completed(Req, State) -> {true, Req, State}.
%
create_user(Req, State) ->
{User, _} = dudeswave_auth:auth_cookies(Req),
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"password">> := Pass, <<"email">> := Email} = json:decode(Data),
{User, Pass, Email, Req0} = dudeswave_auth:read_new_user_data(Req),
case dudeswave_auth:new(User, Pass, Email) of
ok -> {true, Req0, []};