Exemple #1
0
        public IActionResult Register([FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var exist = _context.User.FirstOrDefault(u => u.User_Name == user.User_Name);

            if (exist != null)
            {
                return(BadRequest("El nombre de usuario ya existe!"));
            }
            user.User_Role = "User";
            // Hashing the password and creating the tokens
            user.User_Password = _passowrdHasher.GenerateIdentityV3Hash(user.User_Password);
            string tokenAccess    = _tokensService.CreateAccessToken(user);
            var    newUserSession = new UserRefreshToken {
                Password     = user.User_Password,
                UserName     = user.User_Name,
                RefreshToken = _tokensService.CreateRefreshToken()
            };

            // Storing the data
            using ( _context )
            {
                _context.User.Add(user);
                _context.UserRefreshToken.Add(newUserSession);

                _context.SaveChanges();
            }

            return(Created("api/auth/login", new
            {
                access_token = tokenAccess,
                refresh_token = newUserSession.RefreshToken
            }));
        }