From bbb44f9337143828fe3c535e57eafdfcf11d2111 Mon Sep 17 00:00:00 2001 From: absc Date: Thu, 15 Aug 2024 21:54:41 +0000 Subject: [PATCH] Make reading the registration data a more abstract operation. --- dudeswave/src/dudeswave_auth.erl | 32 ++++++++++++++++++++++-- dudeswave/src/dudeswave_user_handler.erl | 18 ++++++------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/dudeswave/src/dudeswave_auth.erl b/dudeswave/src/dudeswave_auth.erl index 13fb9bd..ffa1c30 100644 --- a/dudeswave/src/dudeswave_auth.erl +++ b/dudeswave/src/dudeswave_auth.erl @@ -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}. \ No newline at end of file + {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}. diff --git a/dudeswave/src/dudeswave_user_handler.erl b/dudeswave/src/dudeswave_user_handler.erl index e5a9e75..c61a2bc 100644 --- a/dudeswave/src/dudeswave_user_handler.erl +++ b/dudeswave/src/dudeswave_user_handler.erl @@ -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, []};