From 96781451b03b346eb35230ab5fe81f81ed52f955 Mon Sep 17 00:00:00 2001 From: absc Date: Sat, 3 Aug 2024 01:13:47 +0200 Subject: [PATCH] Improve the registration bit. Now, we return a proper answer when creating a user through a POST request. Maybe in the future we will convert the APIs to use JSON, like all cool kids do. However, it doesn't look like it makes much sense in this case. --- dudeswave/src/dudeswave_newuser_handler.erl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/dudeswave/src/dudeswave_newuser_handler.erl b/dudeswave/src/dudeswave_newuser_handler.erl index 625780a..af5c19d 100644 --- a/dudeswave/src/dudeswave_newuser_handler.erl +++ b/dudeswave/src/dudeswave_newuser_handler.erl @@ -27,7 +27,7 @@ Register a new user. % -export([allowed_methods/2, content_types_accepted/2, known_methods/2, resource_exists/2, is_conflict/2, - create_user/2]). + previously_existed/2, allow_missing_post/2, create_user/2]). -include_lib("storage/include/storage.hrl"). @@ -68,14 +68,26 @@ is_conflict(Req, user_exists) -> {true, Req, []}; is_conflict(Req, State) -> {false, Req, State}. +previously_existed(Req, State) -> + {false, Req, State}. + +allow_missing_post(Req, State) -> + {true, Req, State}. + create_user(Req, {Bucket, [{name, Name}, {username, User}, {password, Pass}]}) -> crypto:rand_seed(), Salt = rand:bytes(32), Hash = crypto:hash(sha256, <>), + URI = uri_string:recompose(#{ + scheme => cowboy_req:scheme(Req), + host => cowboy_req:host(Req), + path => lists:flatten(["/user/", User]) + }), + case storage:write(Bucket, User, Hash, [{salt, Salt}, {name, Name}]) of ok -> - {true, Req, []}; + {{true, list_to_binary(URI)}, Req, []}; {error, Reason} -> {false, Req, Reason} end.