コード例 #1
0
        public override async Task <EmailChangeReply> HandleRequest(
            AuthorizedRequestObject <EmailChangeRequest, EmailChangeReply> request,
            CancellationToken cancellationToken = default)
        {
            // TODO: add time change email timelock
            EmailChangeRequest requestValue = request.Value;
            string             email        = requestValue.Email;

            // check if email aldeary linked
            var linkedEmail = Context.Accounts.Any(a => a.Email == email && a.Role > RoleType.Unverified);

            if (linkedEmail)
            {
                request.StatusCode = StatusCode.AlreadyExists;
                request.Detail     = $"Email address '{ email }' is already linked to another account";
                return(new EmailChangeReply());
            }

            ApproveEmailTemplate approveEmailTemplate = new ApproveEmailTemplate();

            try
            {
                await emailTemplateSender.SendMailAsync(email, approveEmailTemplate).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                request.StatusCode = StatusCode.InvalidArgument;
                request.Detail     = $"The mail server could not send the email {email} to the address and threw an error {e.Message}";
                return(new EmailChangeReply());
            }

            // add code
            EmailVerifies emailVerifies = new EmailVerifies()
            {
                Code     = approveEmailTemplate.Code,
                SentTime = DateTime.UtcNow,
                Attempts = 0,
                UserId   = request.UserId,
                Guid     = Guid.Parse(approveEmailTemplate.Link),
                Email    = email
            };

            Context.EmailVerifies.Add(emailVerifies);
            await Context.SaveChangesAsync().ConfigureAwait(false);


            EmailChangeReply confirmReply = new EmailChangeReply
            {
                Email = email
            };

            return(confirmReply);
        }