Example #1
0
        public IActionResult Register([FromBody] EmployeeRessource employeeDto)
        {
            // map dto to entity
            var user = _mapper.Map <Employee>(employeeDto);

            try
            {
                // save
                _employeeService.Create(user, employeeDto.Password);
                return(Ok());
            }
            catch (Exception ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Example #2
0
        public IActionResult Update(int id, [FromBody] EmployeeRessource employeeDto)
        {
            // map dto to entity and set id
            var employee = _mapper.Map <Employee>(employeeDto);

            employee.Id = id;

            try
            {
                // save
                _employeeService.Update(employee, employeeDto.Password);
                return(Ok());
            }
            catch (Exception ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Example #3
0
        public IActionResult Authenticate([FromBody] EmployeeRessource employeeDto)
        {
            var employee = _employeeService.Authenticate(employeeDto.Email, employeeDto.Password);

            if (employee == null)
            {
                return(BadRequest(new { message = "L'identifiant ou le mot de passe est incorrect" }));
            }

            if (employee.Status == "Quitter")
            {
                return(BadRequest(new { message = "Vous arrĂȘtez de travailler dans notre entreprise" }));
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes("aszo&123azertyuiopqsd");
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, employee.Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            // return basic user info (without password) and token to store client side
            return(Ok(new
            {
                Id = employee.Id,
                Email = employee.Email,
                NomPrenom = employee.NomPrenom,
                Photo = employee.Photo,
                Tel = employee.Tel,
                Fonction = employee.Fonction,
                Status = employee.Status,
                Token = tokenString
            }));
        }