From 5c9ad9633572de67480afc0a8e99a3120e653c2a Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 8 Nov 2022 07:44:41 -0800 Subject: [PATCH 1/6] RIP pAPI --- .../Controllers/AddressController.cs | 2 +- .../Controllers/InvoiceController.cs | 55 ++++++++++--------- GrossesMitaines/GrossesMitainesAPI/Program.cs | 3 + 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs index c138575..429767f 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs @@ -14,7 +14,7 @@ using Microsoft.EntityFrameworkCore; #endregion [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), - Authorize(AuthenticationSchemes = "Identity.Application")] + Authorize(AuthenticationSchemes = "Identity.Application", Roles ="Client, Administrateur")] public class AddressController : Controller { #region DI Fields private readonly ILogger _logger; diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs index 931a949..39152b0 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs @@ -16,7 +16,7 @@ using System.Linq; #endregion [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), - Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")] + Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Client, Administrateur")] public class InvoiceController : Controller { #region DI Fields private readonly ILogger _logger; @@ -43,15 +43,14 @@ public class InvoiceController : Controller { #endregion #region API Methods - [HttpGet, Authorize(Roles = "Client, Administrateur")] + [HttpGet] public async Task>> Get(bool? all = false) { IList roles; string id; try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver. var user = await _userMan.GetUserAsync(_signInMan.Context.User); roles = await _userMan.GetRolesAsync(user); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(10, e.Message); roles = new List(); } @@ -71,30 +70,27 @@ public class InvoiceController : Controller { .Include(x => x.Products) .ThenInclude(y => y.Product) .Where(x => x.LinkedAccount != null && x.LinkedAccount.Id == id).ToList()); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(10, e.Message); return BadRequest(); } } - [HttpGet("{id}"), Authorize(Roles = "Client, Administrateur")] + [HttpGet("{id}")] public async Task> Get(int id) { IList roles; InvoiceModel inv; try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver. roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User)); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(10, e.Message); roles = new List(); } try { inv = _context.Invoices.Where(x => x.Id == id).Include("ShippingAddress").First(); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(10, e.Message); return BadRequest(); } @@ -119,7 +115,11 @@ public class InvoiceController : Controller { PhoneNumber = sinv.PhoneNumber, PurchaseDate = DateTime.Now }; - AddressModel ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber && + AddressModel ad; + + if (user is not null) { + inv.LinkedAccount = user; + ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber && x.Appartment == sinv.Appartment && x.Street == sinv.Street && x.City == sinv.City && @@ -134,14 +134,22 @@ public class InvoiceController : Controller { Country = sinv.Country, PostalCode = sinv.PostalCode }; + } + else ad = new() { + CivicNumber = sinv.CivicNumber, + Appartment = sinv.Appartment, + Street = sinv.Street, + City = sinv.City, + Province = sinv.Province, + Country = sinv.Country, + PostalCode = sinv.PostalCode + }; + inv.ShippingAddress = ad; - if (user is not null) - inv.LinkedAccount = user; try { prods = _context.Products.Where(x => sinv.ProdQuant.Select(x => x.Key).Contains(x.Id)).ToList(); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(8, e.Message); return BadRequest(); } @@ -158,8 +166,7 @@ public class InvoiceController : Controller { inventProd.Status = inventProd.Status == ProductModel.States.Clearance ? ProductModel.States.Discontinued : ProductModel.States.BackOrder; - } - else inventProd.Quantity -= prod.Value; + } else inventProd.Quantity -= prod.Value; inventProd.LastSale = DateTime.Now; inventProd.Sales += prod.Value; } @@ -172,8 +179,7 @@ public class InvoiceController : Controller { _context.Invoices.Add(inv); _context.Products.UpdateRange(prods); _context.SaveChanges(); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(8, e.Message); return BadRequest(e.InnerException.Message); } @@ -190,16 +196,14 @@ public class InvoiceController : Controller { try { // Trouver la commande. inv = _context.Invoices.Where(x => x.Id == id) .Include("Product").First(); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(8, e.Message); return BadRequest(); } try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver. roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User)); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(10, e.Message); roles = new List(); } @@ -231,8 +235,7 @@ public class InvoiceController : Controller { try { _context.Update(inv); _context.SaveChanges(); - } - catch (Exception e) { + } catch (Exception e) { _logger.LogError(8, e.Message); return BadRequest(); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Program.cs b/GrossesMitaines/GrossesMitainesAPI/Program.cs index 91bf90d..ad50110 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Program.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Program.cs @@ -28,6 +28,9 @@ builder.Services.AddIdentityCore() .AddRoles() .AddEntityFrameworkStores() .AddSignInManager(); +builder.Services.Configure(o => + o.User.RequireUniqueEmail = true); + builder.Services.AddAuthorization(); builder.Services.AddAuthentication().AddIdentityCookies(); From 360326bd7153c23cfc1bd0375b022f99f0b96702 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 8 Nov 2022 08:18:33 -0800 Subject: [PATCH 2/6] KATAMARI DAMACY --- .../Controllers/UserController.cs | 8 +- .../grosses-mitaines-ui/src/pages/Register.js | 97 ++++++++++++++----- 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs index a5153d9..c5f90f6 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs @@ -43,10 +43,13 @@ public class UserController : Controller { int x = 0; InventoryUser usr; try { - usr = new() { + usr = new() { FirstName = sign.FirstName, LastName = sign.LastName, + UserName = sign.FirstName + sign.LastName, + NormalizedUserName = (sign.FirstName + sign.LastName).ToUpper(), Email = sign.Email, + NormalizedEmail = sign.Email.ToUpper(), PhoneNumber = sign.Phone }; } catch (Exception e){ @@ -54,9 +57,10 @@ public class UserController : Controller { } try { + usr.Adresses = new(); usr.Adresses.Add(new AddressModel() { CivicNumber = sign.CivicNumber, - Appartment = sign.Appartment, + Appartment = sign.Appartment is not null && sign.Appartment != ""? sign.Appartment: null, Street = sign.Street, City = sign.City, Province = sign.Province, diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js index 5fc7d0a..e549414 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js @@ -1,8 +1,10 @@ import { useState } from "react"; import { Button } from "react-bootstrap"; import { useForm } from "react-hook-form"; +import Swal from "sweetalert2"; +import withReactContent from "sweetalert2-react-content"; -// const Register = () => { + const Register = () => { // //const [username, setUsername] = useState(""); // //const [password, setPassword] = useState(""); @@ -23,17 +25,24 @@ import { useForm } from "react-hook-form"; // } -export default function App() { +//export default function App() { + const mySwal = withReactContent(Swal); const { register, handleSubmit, watch, formState: { errors } } = useForm(); const onSubmit = data => { - const response = fetch(`https://localhost:7292/api/User`, { + fetch(`https://localhost:7292/api/User`, { method: 'POST', headers: { 'Accept': 'text/json', 'Content-Type': 'text/json' }, body: JSON.stringify(data) - }) + }).then((response) => { + if (response.ok) + mySwal.fire({ + text: 'Vous vous êtes incrit avec succès!', + confirmButtonText: 'Ok' + }).then((response) => window.location.href = "/login"); + }) } return ( @@ -43,34 +52,76 @@ return (
- +
- {errors.Prénom && errors.Prénom.type === 'required' && Vous devez entrer votre prénom!} - {errors.Prénom && errors.Prénom.type === 'minLength' && Votre prénom doit avoir au moins 2 lettres!} + {errors.FirstName && errors.FirstName.type === 'required' && Vous devez entrer votre prénom!} + {errors.FirstName && errors.FirstName.type === 'minLength' && Votre prénom doit avoir au moins 2 lettres!}
- +
- {errors.Nom && errors.Nom.type === 'required' && Vous devez entrer votre nom!} - {errors.Nom && errors.Nom.type === 'minLength' && Votre nom doit avoir au moins 2 lettres!} + {errors.LastName && errors.LastName.type === 'required' && Vous devez entrer votre nom!} + {errors.LastName && errors.LastName.type === 'minLength' && Votre nom doit avoir au moins 2 lettres!}
- +
- {errors.Téléphone && errors.Téléphone.type === 'required' && Vous devez entrer un numéro de téléphone!} - {errors.Téléphone && errors.Téléphone.type === 'pattern' && Téléphone non valide!} + {errors.Phone && errors.Phone.type === 'required' && Vous devez entrer un numéro de téléphone!} + {errors.Phone && errors.Phone.type === 'pattern' && Téléphone non valide!}
- - + +
- {errors.Adresse && errors.Adresse.type === 'required' && Vous devez entrer une adresse!} + {errors.CivicNumber && errors.CivicNumber.type === 'required' && Vous devez entrer un numéro civique!} +
+
+ + +
+
+ +
+
+ + +
+
+ {errors.Street && errors.Street.type === 'required' && Vous devez entrer une rue!} +
+
+ + +
+
+ {errors.City && errors.City.type === 'required' && Vous devez entrer une vile!} +
+
+ + +
+
+ {errors.Province && errors.Province.type === 'required' && Vous devez entrer une province!} +
+
+ + +
+
+ {errors.Country && errors.Country.type === 'required' && Vous devez entrer un pays!} +
+
+ + +
+
+ {errors.PostalCode && errors.PostalCode.type === 'required' && Vous devez entrer un code postal!}
@@ -82,19 +133,17 @@ return (
- +
- {errors.MotPasse && errors.MotPasse.type === 'required' && Vous devez entrer un mot de passe!} - {errors.MotPasse && errors.MotPasse.type === 'minLength' && Votre mot de passe doit avoir au moins 5 caractères!} -
-
- - + {errors.Password && errors.Password.type === 'required' && Vous devez entrer un mot de passe!} + {errors.Password && errors.Password.type === 'minLength' && Votre mot de passe doit avoir au moins 5 caractères!}
) -} \ No newline at end of file +} + +export default Register; \ No newline at end of file From 9c508f67bea0dedf793ef25ba80aa02fdec382af Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 8 Nov 2022 08:26:35 -0800 Subject: [PATCH 3/6] salkffasfaflkn --- GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js index 4268bc8..18610f5 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js @@ -65,6 +65,7 @@ const Topbar = () => { S'inscrire } + {user !== null && user.LoggedIn && @@ -122,7 +123,6 @@ const Topbar = () => { } - From df620ecec3cd0864ba3c8f0c44c2e978f66c3338 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 8 Nov 2022 08:29:54 -0800 Subject: [PATCH 4/6] Update UserController.cs --- .../GrossesMitainesAPI/Controllers/UserController.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs index c5f90f6..c2e4632 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs @@ -40,7 +40,6 @@ public class UserController : Controller { #region API Methods [HttpPost, AllowAnonymous] public ActionResult Post(SignUpUserModel sign) { - int x = 0; InventoryUser usr; try { usr = new() { @@ -81,7 +80,6 @@ public class UserController : Controller { t1.Wait(); var t2 = _userMan.AddToRoleAsync(usr, "Client"); t2.Wait(); - } catch (Exception e) { return BadRequest(e.Message); } From 3053d0cb0c9aaad65483dc71a34e8eb87410963c Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 8 Nov 2022 10:04:21 -0800 Subject: [PATCH 5/6] Update Register.js --- .../grosses-mitaines-ui/src/pages/Register.js | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js index e549414..0496160 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js @@ -5,27 +5,6 @@ import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; const Register = () => { - -// //const [username, setUsername] = useState(""); -// //const [password, setPassword] = useState(""); - -// // const handleLogin = async (e) => { -// // e.preventDefault(); - -// // const response = await fetch(`https://localhost:7292/api/Login?rememberme=true`, { -// // method: 'POST', -// // headers: { -// // 'Accept': 'application/json', -// // 'Content-Type': 'application/json' -// // }, -// // //body: JSON.stringify({ username, password }) -// // }) - -// // Partie de display d'erreur ou de redirection (faudrait checker pour se faire un state de connexion). - -// } - -//export default function App() { const mySwal = withReactContent(Swal); const { register, handleSubmit, watch, formState: { errors } } = useForm(); const onSubmit = data => { From db79087632894805438b4e488f2f7e31e5b2d15c Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 8 Nov 2022 10:21:53 -0800 Subject: [PATCH 6/6] Je me souviens. --- .../grosses-mitaines-ui/src/components/Topbar.js | 2 +- GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js index 18610f5..e9de48d 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js @@ -96,7 +96,7 @@ const Topbar = () => { - Gestion + Gestion diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js index c48dd24..9aea143 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js @@ -5,7 +5,7 @@ import Cookies from "universal-cookie"; const Login = () => { - const [rememberme, setPersistence] = useState(true); + const [rememberme, setPersistence] = useState(false); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [returnmess, returnMessage] = useState(""); @@ -81,6 +81,13 @@ const Login = () => { value={password} onChange={(e) => setPassword(e.target.value)} /> +
+ +