From c02a157771fa7f77cba53908c27af8002a1fdf5a Mon Sep 17 00:00:00 2001 From: Dara Keon Date: Sun, 18 Feb 2024 23:38:54 +0000 Subject: [PATCH] auth: add signup to API --- api/API/Controllers/UsersController.cs | 30 +++++++----- api/API/Models/BaseModel.cs | 17 ------- api/API/Models/MovesCreateModel.cs | 3 -- api/API/Models/UsersLoginModel.cs | 13 ++++- api/API/Models/UsersSignUpModel.cs | 39 +++++++++++++++ api/postman/DFM.postman_collection.json | 65 ++++++++++++++++++++++++- site/MVC/Models/BaseModel.cs | 15 ------ site/MVC/Models/UsersLogOnModel.cs | 12 ++++- 8 files changed, 144 insertions(+), 50 deletions(-) create mode 100644 api/API/Models/UsersSignUpModel.cs diff --git a/api/API/Controllers/UsersController.cs b/api/API/Controllers/UsersController.cs index ac44671a4..8150c0275 100644 --- a/api/API/Controllers/UsersController.cs +++ b/api/API/Controllers/UsersController.cs @@ -8,18 +8,24 @@ namespace DFM.API.Controllers { public class UsersController : BaseApiController { - [HttpPost] - public IActionResult Login(String email, String password) - { - var model = - new UsersLoginModel - { - Email = email, - Password = password, - }; + [HttpPost] + public IActionResult Login(String email, String password) + { + var model = + new UsersLoginModel + { + Email = email, + Password = password, + }; - return json(() => new { ticket = model.LogOn() }); - } + return json(() => new { ticket = model.LogOn() }); + } + + [HttpPost] + public IActionResult SignUp([FromBody] UsersSignUpModel model) + { + return json(model.SignUp); + } [HttpPost] public IActionResult Logout() @@ -44,7 +50,7 @@ public IActionResult SaveSettings() } [HttpPost, Auth(AuthParams.IgnoreTFA)] - public IActionResult TFA(string code) + public IActionResult TFA(String code) { var model = new UserTFAModel(code); return json(model.Validate); diff --git a/api/API/Models/BaseModel.cs b/api/API/Models/BaseModel.cs index c7645a269..e3988cb1c 100644 --- a/api/API/Models/BaseModel.cs +++ b/api/API/Models/BaseModel.cs @@ -3,11 +3,9 @@ using DFM.API.Helpers.Global; using DFM.API.Starters; using DFM.BusinessLogic; -using DFM.BusinessLogic.Exceptions; using DFM.BusinessLogic.Services; using DFM.Generic; using Microsoft.AspNetCore.Http; -using Error = DFM.BusinessLogic.Exceptions.Error; namespace DFM.API.Models { @@ -40,21 +38,6 @@ public abstract class BaseModel protected Boolean isUsingAccountsSigns => current.UseAccountsSigns; protected Boolean moveCheckingEnabled => current.MoveCheck; - protected String login(String email, String password, Boolean rememberMe) - { - try - { - return current.Set(email, password, rememberMe); - } - catch (CoreError e) - { - if (e.Type == Error.DisabledUser) - outside.SendUserVerify(email); - - throw; - } - } - protected void logout() { current.Clear(); diff --git a/api/API/Models/MovesCreateModel.cs b/api/API/Models/MovesCreateModel.cs index e03a9205f..0096c67df 100644 --- a/api/API/Models/MovesCreateModel.cs +++ b/api/API/Models/MovesCreateModel.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; -using System.Linq; using DFM.BusinessLogic.Response; -using DFM.Entities.Enums; namespace DFM.API.Models { diff --git a/api/API/Models/UsersLoginModel.cs b/api/API/Models/UsersLoginModel.cs index 667ec546e..99299c8c8 100644 --- a/api/API/Models/UsersLoginModel.cs +++ b/api/API/Models/UsersLoginModel.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using DFM.BusinessLogic.Exceptions; namespace DFM.API.Models { @@ -15,7 +16,17 @@ internal class UsersLoginModel : BaseApiModel internal string LogOn() { - return login(Email, Password, RememberMe); + try + { + return current.Set(Email, Password, RememberMe); + } + catch (CoreError e) + { + if (e.Type == Error.DisabledUser) + outside.SendUserVerify(Email); + + throw; + } } } } diff --git a/api/API/Models/UsersSignUpModel.cs b/api/API/Models/UsersSignUpModel.cs new file mode 100644 index 000000000..0a5dcdea2 --- /dev/null +++ b/api/API/Models/UsersSignUpModel.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel.DataAnnotations; +using DFM.BusinessLogic.Response; + +namespace DFM.API.Models +{ + public class UsersSignUpModel : BaseApiModel + { + [Required] + public String Email { get; set; } + + [Required] + public String Password { get; set; } + + [Required] + public Boolean AcceptedContract { get; set; } + + [Required] + public String Language { get; set; } + + [Required] + public String Timezone { get; set; } + + internal void SignUp() + { + auth.SaveUser( + new SignUpInfo + { + Email = Email, + Password = Password, + AcceptedContract = AcceptedContract, + Language = Language, + TimeZone = Timezone, + RetypePassword = Password, + } + ); + } + } +} diff --git a/api/postman/DFM.postman_collection.json b/api/postman/DFM.postman_collection.json index 452c5bc92..6265e13cc 100644 --- a/api/postman/DFM.postman_collection.json +++ b/api/postman/DFM.postman_collection.json @@ -119,6 +119,64 @@ }, "response": [] }, + { + "name": "Users Signup", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "const json = pm.response.json()\r", + "\r", + "if (json.data) {\r", + " pm.collectionVariables.set(\"ticket\", json.data.ticket)\r", + "}\r", + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "prerequest", + "script": { + "exec": [ + "var email_number = pm.collectionVariables.get(\"email_number\");\r", + "email_number = (email_number * 1) + 1;\r", + "pm.collectionVariables.set(\"email_number\", email_number);\r", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "auth": { + "type": "noauth" + }, + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"Email\": \"{{test_user_email}}\",\r\n \"Password\": \"{{test_user_password}}\",\r\n \"AcceptedContract\": true,\r\n \"Language\": \"pt-BR\",\r\n \"Timezone\": \"UTC-03:00\"\r\n}\r\n", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/users/signup", + "host": [ + "{{url}}" + ], + "path": [ + "users", + "signup" + ] + } + }, + "response": [] + }, { "name": "Users Login", "event": [ @@ -785,7 +843,7 @@ }, { "key": "test_user_email", - "value": "", + "value": "test{{email_number}}@dontflymoney.com", "type": "string" }, { @@ -812,6 +870,11 @@ { "key": "move_id", "value": "" + }, + { + "key": "email_number", + "value": "1", + "type": "string" } ] } \ No newline at end of file diff --git a/site/MVC/Models/BaseModel.cs b/site/MVC/Models/BaseModel.cs index 19da3bb61..1bffe7cd4 100644 --- a/site/MVC/Models/BaseModel.cs +++ b/site/MVC/Models/BaseModel.cs @@ -44,21 +44,6 @@ public abstract class BaseModel protected Boolean isUsingAccountsSigns => current.UseAccountsSigns; protected Boolean moveCheckingEnabled => current.MoveCheck; - protected String login(String email, String password, Boolean rememberMe) - { - try - { - return current.Set(email, password, rememberMe); - } - catch (CoreError e) - { - if (e.Type == Error.DisabledUser) - outside.SendUserVerify(email); - - throw; - } - } - protected void logout() { current.Clear(); diff --git a/site/MVC/Models/UsersLogOnModel.cs b/site/MVC/Models/UsersLogOnModel.cs index 182310330..7313e4497 100644 --- a/site/MVC/Models/UsersLogOnModel.cs +++ b/site/MVC/Models/UsersLogOnModel.cs @@ -29,7 +29,17 @@ internal CoreError TryLogOn() internal String LogOn() { - return login(Email, Password, RememberMe); + try + { + return current.Set(Email, Password, RememberMe); + } + catch (CoreError e) + { + if (e.Type == Error.DisabledUser) + outside.SendUserVerify(Email); + + throw; + } } } }