diff --git a/SeniorAssistant/Controllers/AccountController.cs b/SeniorAssistant/Controllers/AccountController.cs index d33ac8b..9edde56 100644 --- a/SeniorAssistant/Controllers/AccountController.cs +++ b/SeniorAssistant/Controllers/AccountController.cs @@ -1,14 +1,16 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using SeniorAssistant.Models; +using SeniorAssistant.Controllers; +using LinqToDB; +using System.Linq; namespace IdentityDemo.Controllers { [ApiExplorerSettings(IgnoreApi = true)] [Route("[controller]/[action]")] - public class AccountController : Controller + public class AccountController : BaseController { /* private readonly UserManager _userManager; @@ -41,19 +43,24 @@ namespace IdentityDemo.Controllers */ [HttpPost] - public ActionResult _login(string username, string password, bool rememberMe) + public ActionResult _login(string username, string password) { - var result = username != null && password != null && username.Equals("acc1") && password.Equals("123"); //await _signInManager.PasswordSignInAsync(userName, password, rememberMe, lockoutOnFailure: false); JsonResponse response = new JsonResponse(); response.Success = false; response.Message = "Username or password is invalid."; - if (result) + var strunz = Db.GetTable().Where(user => user.Username.Equals(username) && user.Password.Equals(password)).ToListAsync().Result; + + if (strunz.Count == 1) { var loggedUser = HttpContext.Session.GetString("username"); if (loggedUser==null || !loggedUser.Equals(username)) { HttpContext.Session.SetString("username", username); + HttpContext.Session.SetString("email", strunz.First().Email); + HttpContext.Session.SetString("name", strunz.First().Name); + HttpContext.Session.SetString("isdoc", strunz.First().Doctor?"true":"false"); + //HttpContext.Session.SetString("lastname", strunz.First().LastName); response.Success = true; response.Message = ""; } @@ -65,15 +72,31 @@ namespace IdentityDemo.Controllers return Json(response); } + [HttpPost] public ActionResult _logout() { HttpContext.Session.Clear(); return Json(new JsonResponse()); } - public ActionResult _register() + [HttpPost] + public ActionResult _register(Register register) { - return Json(new JsonResponse()); + if(ModelState.IsValid) + { + User user = new User() { Username = register.Username, Email = register.Email, Password = register.Password}; + try + { + Db.Insert(user); + } + catch + { + return Json(new JsonResponse() { Success = false, Message = "Username already exist" }); + } + _login(user.Username, user.Password); + return Json(new JsonResponse() { Success = true }); + } + return Json(new JsonResponse() { Success = false, Message = "Modello non valido" }); } internal class JsonResponse { diff --git a/SeniorAssistant/Models/Heartbeat.cs b/SeniorAssistant/Models/Heartbeat.cs index 38791a1..baf0622 100644 --- a/SeniorAssistant/Models/Heartbeat.cs +++ b/SeniorAssistant/Models/Heartbeat.cs @@ -7,14 +7,12 @@ namespace SeniorAssistant.Models { [PrimaryKey] [NotNull] + [Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)] public string Username { get; set; } [PrimaryKey] [NotNull] public DateTime Time { get; set; } - - [Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)] - public User user { get; set; } public double Value { get; set; } } diff --git a/SeniorAssistant/Models/Register.cs b/SeniorAssistant/Models/Register.cs new file mode 100644 index 0000000..40bdbcf --- /dev/null +++ b/SeniorAssistant/Models/Register.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SeniorAssistant.Models +{ + public class Register + { + public string Username { get; set; } + public string Email { get; set; } + public string Password { get; set; } + public bool Doctor { get; set; } + } +} diff --git a/SeniorAssistant/Models/Sleep.cs b/SeniorAssistant/Models/Sleep.cs index 2ca6db8..12ba7f9 100644 --- a/SeniorAssistant/Models/Sleep.cs +++ b/SeniorAssistant/Models/Sleep.cs @@ -7,14 +7,12 @@ namespace SeniorAssistant.Models { [PrimaryKey] [NotNull] + [Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)] public string Username { get; set; } [PrimaryKey] [NotNull] public DateTime Time { get; set; } - - [Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)] - public User user { get; set; } public long Value { get; set; } } diff --git a/SeniorAssistant/Models/Step.cs b/SeniorAssistant/Models/Step.cs index 73d5ad3..04eeab6 100644 --- a/SeniorAssistant/Models/Step.cs +++ b/SeniorAssistant/Models/Step.cs @@ -7,15 +7,13 @@ namespace SeniorAssistant.Models { [PrimaryKey] [NotNull] + [Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)] public string Username { get; set; } [PrimaryKey] [NotNull] public DateTime Time { get; set; } - [Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)] - public User user { get; set; } - public long Value { get; set; } } } diff --git a/SeniorAssistant/Models/User.cs b/SeniorAssistant/Models/User.cs index 44856fb..dd9e25c 100644 --- a/SeniorAssistant/Models/User.cs +++ b/SeniorAssistant/Models/User.cs @@ -10,6 +10,17 @@ namespace SeniorAssistant.Models public string Username { get; set; } [NotNull] + public string Email { get; set; } + + [NotNull] + public string Password { get; set; } + + [NotNull] + public bool Doctor { get; set; } + public string Name { get; set; } + + public string LastName { get; set; } + } } diff --git a/SeniorAssistant/Startup.cs b/SeniorAssistant/Startup.cs index 204d43e..3d652ee 100644 --- a/SeniorAssistant/Startup.cs +++ b/SeniorAssistant/Startup.cs @@ -132,7 +132,8 @@ namespace SeniorAssistant int count = 0; foreach (string user in users) { - db.InsertOrReplace(new User { Name = user, Username = baseUsername + (count == 0 ? "" : "" + count) }); + var username = baseUsername + count; + db.InsertOrReplace(new User { Name = user, Username = username, Password = username, Email = username + "@email.st" } ); count++; } } diff --git a/SeniorAssistant/Views/Home/Index.cshtml b/SeniorAssistant/Views/Home/Index.cshtml index 8733407..76f15dd 100644 --- a/SeniorAssistant/Views/Home/Index.cshtml +++ b/SeniorAssistant/Views/Home/Index.cshtml @@ -5,6 +5,26 @@ logo sito disattivare l-aside e le opzioni se non loggato deve tornare qua --> -

- ciao noob -

\ No newline at end of file +@inject IHttpContextAccessor HttpContextAccessor + +@{ + string session = HttpContextAccessor.HttpContext.Session.GetString("username"); +} + +
+ @if (session == null) + { + + + + } + else + { + await Html.RenderPartialAsync("Profile"); + } +
diff --git a/SeniorAssistant/Views/Shared/Login.cshtml b/SeniorAssistant/Views/Shared/Login.cshtml index ca1aefd..bc96ed3 100644 --- a/SeniorAssistant/Views/Shared/Login.cshtml +++ b/SeniorAssistant/Views/Shared/Login.cshtml @@ -1,9 +1,8 @@  - -