コード例 #1
0
        public async Task <IActionResult> ActiveEmail([FromBody] VerifyEmailDTO email)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new
                    {
                        Error = "OTP không được để trống"
                    }));
                }
                var accountFromDb = await _repo.GetAccount(email.Email);

                if (accountFromDb == null)
                {
                    return(BadRequest(new
                    {
                        error = "Tài khoản không tồn tại"
                    }));
                }
                var accountOTP = await _cRUDRepo.GetOneWithConditionTracking <AccountOTP>(ao => ao.AccountId == accountFromDb.Id && ao.ValidUntil > DateTime.Now && ao.OTP == email.OTP);

                if (accountOTP == null)
                {
                    return(BadRequest(new
                    {
                        Error = "OTP đã hết hạn hoặc không tồn tại"
                    }));
                }
                var activeEmailResult = await _userManager.ConfirmEmailAsync(accountFromDb, accountOTP.Token);

                if (activeEmailResult.Succeeded)
                {
                    _cRUDRepo.Delete(accountOTP);
                    if (await _repo.SaveAll())
                    {
                        return(Ok());
                    }
                    return(NoContent());
                }
                return(NoContent());
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }