Example #1
0
        public async Task <IActionResult> Register([FromBody] RegisterForDto userRegisterDto)
        {
            if (!string.IsNullOrEmpty(userRegisterDto.Email))
            {
                userRegisterDto.Email = userRegisterDto.Email.ToLower();
            }
            if (await _repo.UserExists(userRegisterDto.Email))
            {
                ModelState.AddModelError("Email", "Email is already taken");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var userToCreate = _mapper.Map <AppUser>(userRegisterDto);
            var createUser   = await _userManager.CreateAsync(userToCreate, userRegisterDto.Password);

            //var userToReturn = _mapper.Map<UserForDetailedDto>(createUser);
            if (createUser.Succeeded)
            {
                await _userManager.AddToRoleAsync(userToCreate, "User");

                //await _userManager.AddToRolesAsync(userToCreate, userRegisterDto.Roles);
                return(Ok(new { email = userRegisterDto.Email, status = 1, message = "Registration Successful" }));
            }
            return(BadRequest());
        }
Example #2
0
        public async Task <IActionResult> Register([FromBody] RegisterForDto model)
        {
            ApiResponse <RegisterForDto> response = new ApiResponse <RegisterForDto>();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (string.IsNullOrEmpty(model.Password) || string.IsNullOrEmpty(model.RTPassword))
                {
                    return(BadRequest(new { error = "Password & Repeat password is required." }));
                }
                if (model.Password != model.RTPassword)
                {
                    return(BadRequest(new { error = "Password & Repeat password does not match." }));
                }
                if (await _userSrv.UserExists(model.Email))
                {
                    ModelState.AddModelError("Email", "Email already taken");
                }
                var IsPhoneAlreadyRegistered = _userManager.Users.Any(item => item.PhoneNumber == model.PhoneNumber);
                if (IsPhoneAlreadyRegistered == true)
                {
                    return(BadRequest(new { error = $"{model.PhoneNumber} Already exist!" }));
                }

                var user = AppUser.Create(model.FirstName, model.LastName, model.Email, model.Gender, model.PhoneNumber, model.NIN);

                user.UserName       = model.Email;
                user.EmailConfirmed = true;
                user.FullName       = model.FirstName + " " + model.LastName;
                user.Activated      = true;
                user.IsDisabled     = false;
                user.CreatedOnUtc   = DateTime.Now.GetDateUtcNow();
                user.LockoutEnabled = false;

                var createResult = await _userManager.CreateAsync(user, model.Password);

                if (!createResult.Succeeded)
                {
                    return(BadRequest(new { error = $"{createResult.Errors.FirstOrDefault().Description}" }));
                }
                createResult = await _userManager.AddToRoleAsync(user, "USER");

                if (!createResult.Succeeded)
                {
                    return(BadRequest(new { error = $"{createResult.Errors.FirstOrDefault().Description}" }));
                }
                response.Code        = ApiResponseCodes.OK;
                response.Description = $"Registration Successful";
                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Example #3
0
        public async Task <IActionResult> Register([FromBody] RegisterForDto model)
        {
            if (ModelState.IsValid)
            {
                model.Role = "User";
                var result = await _userService.RegisterUserAsync(model);

                if (result.IsSuccess)
                {
                    return(Ok(result));
                }
                return(BadRequest(result));
            }

            return(BadRequest("Some properties are not valid.")); // Status code: 400
        }
Example #4
0
        public async Task <IActionResult> Register(RegisterForDto model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var serRes = await _accountService.Register(model);

            if (serRes.ResultStatus.Status != Enums.ResultStatus.Success)
            {
                ModelState.AddModelError("", serRes.ResultStatus.Explanation);
                return(View(model));
            }

            AddSweetAlert("Teşekkürler", "Kaydınız başarıyla yapıldı. Bilgileriniz ile giriş yapabilirsiniz.", NotificationType.success);
            return(RedirectToAction("Login"));
        }
Example #5
0
        public async Task <IActionResult> Register([FromBody] RegisterForDto model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password) || string.IsNullOrEmpty(model.Email))
                {
                    return(BadRequest("Lütfen tüm alanları doldurunuz."));
                }

                var isExist = await _userManager.FindByNameAsync(model.UserName);

                if (isExist != null)
                {
                    return(BadRequest($"{model.UserName} isminde zaten bir kullanıcı var."));
                }

                var applicationUser = new ApplicationUser
                {
                    UserName = model.UserName,
                    Email    = model.Email
                };

                var identityResult = await _userManager.CreateAsync(applicationUser, model.Password);

                if (!identityResult.Succeeded)
                {
                    return(BadRequest(string.Join(",", identityResult.Errors.Select(s => s.Description).ToArray())));
                }

                // Default user rol'ü ekleniyor.
                await _userManager.AddToRoleAsync(applicationUser, "User");

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
Example #6
0
        public async Task <ServiceResult> Register(RegisterForDto model)
        {
            var serRes = new ServiceResult();

            var json       = JsonConvert.SerializeObject(model, Formatting.Indented);
            var httpClinet = _httpClientFactory.CreateClient("ingamedemo");
            var response   = await httpClinet.PostAsync("account/register", new StringContent(json, Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                var message = await response.Content.ReadAsStringAsync();

                if (string.IsNullOrEmpty(message))
                {
                    message = "Beklenmedik bir hata meydana geldi. Lütfen daha sonra tekrar deneyiniz.";
                }

                serRes.ResultStatus.Explanation = message;
                return(serRes);
            }

            serRes.ResultStatus.Status = Enums.ResultStatus.Success;
            return(serRes);
        }
Example #7
0
        public async Task <UserManagerResponse> RegisterUserAsync(RegisterForDto registerForDto)
        {
            if (registerForDto == null)
            {
                throw new NullReferenceException("Register model is null.");
            }

            if (registerForDto.Password != registerForDto.ConfirmPassword)
            {
                return new UserManagerResponse
                       {
                           Message   = "Şifreler eşleşmiyor.",
                           IsSuccess = false
                       }
            }
            ;

            var user = _context.Users.SingleOrDefaultAsync(x => x.UserName == registerForDto.UserName).Result;

            if (user != null)
            {
                return(new UserManagerResponse
                {
                    Message = "Kullanıcı adı kullanımda.",
                    IsSuccess = false
                });
            }

            var identityUser = new User
            {
                Email     = registerForDto.Email,
                UserName  = registerForDto.UserName,
                FirstName = registerForDto.FirstName,
                LastName  = registerForDto.LastName
            };

            var result = await _userManager.CreateAsync(identityUser, registerForDto.Password);

            if (result.Succeeded)
            {
                var confirmEmailToken = await _userManager.GenerateEmailConfirmationTokenAsync(identityUser);

                var    encodedEmailToken = Encoding.UTF8.GetBytes(confirmEmailToken);
                var    validEmailToken   = WebEncoders.Base64UrlEncode(encodedEmailToken);
                string url =
                    $"{_configuration["AppUrl"]}/api/auth/confirmEmail?userId={identityUser.Id}&token={validEmailToken}";

                await _mailService.SendMailAsync(identityUser.Email, "Confirm your email",
                                                 $"<h1>MagnifiSoccer'e hoş geldin.</h1><p>E-posta aktivasyonu için <a href='{url}'>buraya tıklayınız.</a></p>");

                return(new UserManagerResponse
                {
                    Message = "Kayıt başarılı! Giriş yapabilirsiniz.",
                    IsSuccess = true
                });
            }

            return(new UserManagerResponse
            {
                Message = result.Errors.Select(e => e.Description).Last(),
                IsSuccess = false,
                Errors = result.Errors.Select(e => e.Description)
            });
        }