예제 #1
0
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model)
        {
            try
            {
                //should be later on changed to a transaction
                var profileViewModel = new ProfileViewModel
                {
                    Firstname = model.FirstName,
                    Lastname  = model.LastName,
                    Email     = model.Email
                };
                var resultModel = await profileService.AddAsync(profileViewModel);

                if (resultModel.Item == null)
                {
                    throw new Exception();
                }

                var user = new ApplicationUser
                {
                    UserName  = model.Email,
                    Email     = model.Email,
                    ProfileId = resultModel.Item.Id
                };
                var result = await userManager.CreateAsync(user, model.Password);

                if (!result.Succeeded)
                {
                    await profileService.DeleteAsync(resultModel.Item);

                    return(BadRequest());
                }
                await userManager.AddClaimsAsync(user, new List <Claim>
                {
                    new Claim("pid", resultModel.Item.Id.ToString()),
                    new Claim("email", resultModel.Item.Email),
                    new Claim("firstname", resultModel.Item.Firstname),
                    new Claim("lastname", resultModel.Item.Lastname)
                });

                //await mailService.SendAsync(model.Email, "successfully refistered", "registration notice");
                return(Ok(resultModel.Item));
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }