Abstract reading the login data from the request.

main
absc 2024-08-15 21:45:36 +00:00
parent f9d3c5fa4b
commit b3cdc8ee45
2 changed files with 26 additions and 3 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]).
set_auth_cookies/4, read_login_data/1]).
-doc """
Verify a session with an existing cookie.
@ -323,3 +323,27 @@ set_auth_cookies(Req, User, Cookie, Validity) ->
#{max_age => Validity}),
Req1.
-doc """
Spec:
```
-spec read_login_data(Req) -> {User, Pass, Req0} when
Req :: cowboy_req:req(),
User :: binary(),
Pass :: binary(),
Req0 :: cowboy_req:req().
```
Read the login details from the `Req` body and return `User` and `Password`.
""".
-spec read_login_data(Req) -> {User, Pass, Req0} when
Req :: cowboy_req:req(),
User :: binary(),
Pass :: binary(),
Req0 :: cowboy_req:req().
read_login_data(Req) ->
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"user">> := User, <<"password">> := Pass} = json:decode(Data),
{User, Pass, Req0}.

View File

@ -151,8 +151,7 @@ delete_completed(Req, State) -> {false, Req, State}.
%
login(Req, State) ->
{ok, Data, Req0} = cowboy_req:read_body(Req),
#{<<"user">> := User, <<"password">> := Pass} = json:decode(Data),
{User, Pass, Req0} = dudeswave_auth:read_login_data(Req),
case dudeswave_auth:authenticate(User, {password, Pass}) of
{true, Cookie, Validity} ->