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/Controllers/UserController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs index a5153d9..c2e4632 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs @@ -40,13 +40,15 @@ public class UserController : Controller { #region API Methods [HttpPost, AllowAnonymous] public ActionResult Post(SignUpUserModel sign) { - 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 +56,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, @@ -77,7 +80,6 @@ public class UserController : Controller { t1.Wait(); var t2 = _userMan.AddToRoleAsync(usr, "Client"); t2.Wait(); - } catch (Exception e) { return BadRequest(e.Message); } 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(); diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js index 4268bc8..e9de48d 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 && @@ -95,7 +96,7 @@ const Topbar = () => { - Gestion + Gestion @@ -122,7 +123,6 @@ const Topbar = () => { } - 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)} /> +
+ +
diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js index 5fc7d0a..0496160 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js @@ -1,39 +1,27 @@ 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 [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 Register = () => { + 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 +31,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 +112,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