Exemple #1
0
        public async Task <IActionResult> Register([FromBody] RegisterModel model)
        {
            if (_usersService.UserWithSameLoginIsExist(model.Login))
            {
                return(BadRequest("User with same login already exist"));
            }

            model.Role = Role.User;

            User user = model.Adapt <User>();

            await _usersService.Create(user);

            var identity = _usersService.GetIdentity(user);

            string encodedJwt = _usersService.GetEncodedJwt(identity);

            TokenResponse response = new TokenResponse
            {
                Token = encodedJwt,
                Login = identity.Name
            };

            return(Ok(response));
        }
Exemple #2
0
        public void Register(RegisterModel registerModel)
        {
            User user = registerModel.Adapt <User>();

            //Set role for customer
            user.RoleId = 2;

            userRepository.Add(user);
        }
Exemple #3
0
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (_usersService.UserWithSameLoginIsExist(model.Login))
            {
                return(BadRequest("User with same login already exist"));
            }

            model.Role = Role.User;

            User user = model.Adapt <User>();

            await _usersService.Create(user);

            await _usersService.Authenticate(user, HttpContext);

            return(RedirectToAction("Me"));
        }
Exemple #4
0
 public IActionResult CreateUser([FromBody] RegisterModel user)
 {
     try
     {
         var userData    = user.Adapt <User>();
         var userCreated = _userService.CreateUser(userData, user.Password);
         return(Ok(new
         {
             message = "Create User Done!",
             userInfo = userCreated,
             status = 200
         }));
     }
     catch
     {
         return(BadRequest(new
         {
             message = "User Doesn't Create!"
         }));
     }
 }
Exemple #5
0
        public IActionResult Register([FromBody] RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(model));
            }

            var user = model.Adapt <User>();

            User createdUser;

            try
            {
                createdUser = _authenticationService.Register(user, model.Password);
            }
            catch (Exception e) when(e is ArgumentNullException || e is ArgumentException ||
                                     e is InvalidOperationException)
            {
                return(BadRequest(new BadRequestResponse(nameof(e), e.Message)));
            }

            return(Ok(new RegisterResponse(createdUser.Id, createdUser.Username)));
        }