public async Task <IActionResult> Post(LoginCredentials loginCredentials)
        {
            try
            {
                var user = await _userManager.FindByNameAsync(loginCredentials.Username);

                if (user == null)
                {
                    return(Forbid());
                }

                // Sign user in with the valid credentials
                var result = await _signInManager.CheckPasswordSignInAsync(
                    user,
                    loginCredentials.Password,
                    true);

                // If successful...
                if (!result.Succeeded)
                {
                    return(Forbid());
                }

                var claims = new[]
                {
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(ClaimsConstants.USERID, user.Id.ToString())
                };

                var userInfo = await _context
                               .UserInfo
                               .AsNoTracking()
                               .Where(u => u.FkUserId == user.Id).FirstOrDefaultAsync();

                return(Ok(new
                {
                    User = new
                    {
                        Id = user.Id,
                        Username = user.UserName,
                        Firstname = userInfo?.Firstname,
                        Lastname = userInfo?.Lastname,
                        ProfilePictureData = userInfo?.Profilepicturedata
                    },
                    JwtToken = JWTGenerator.GenerateJWTToken(_configuration, claims)
                }));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] LoginCredentials loginCredentials)
        {
            try
            {
                var result = await _userManager.CreateAsync(new ApplicationUser
                {
                    UserName = loginCredentials.Username,
                    Email    = loginCredentials.Username
                }, loginCredentials.Password);

                if (!result.Succeeded)
                {
                    // TODO: Add types of incorrect password format and handle it in client
                    // For now only the first error will be returned as a header
                    var firstErrorDescription = result.Errors.FirstOrDefault()?.Description;
                    this.HttpContext.Response.Headers.Add("user-creation-first-error", firstErrorDescription);
                    return(BadRequest(firstErrorDescription));
                }

                var justCreatedUser = await _userManager.FindByNameAsync(loginCredentials.Username);

                _applicationContext.UserInfo.Add(new UserInfo()
                {
                    Id          = Guid.NewGuid(),
                    Firstname   = loginCredentials.Firstname,
                    Lastname    = loginCredentials.Lastname,
                    Description = loginCredentials.Description,
                    FkUserId    = justCreatedUser.Id
                });

                _applicationContext.BusinessInfo.Add(new BusinessInfo()
                {
                    Id       = Guid.NewGuid(),
                    FkUserId = justCreatedUser.Id
                });

                await _applicationContext.SaveChangesAsync();

                var claims = new[]
                {
                    new Claim(ClaimTypes.Name, justCreatedUser.UserName),
                    new Claim(ClaimsConstants.USERID, justCreatedUser.Id.ToString())
                };

                return(Ok(new
                {
                    User = new
                    {
                        Id = justCreatedUser.Id,
                        Firstname = loginCredentials.Firstname,
                        Lastname = loginCredentials.Lastname,
                        Username = justCreatedUser.UserName
                    },
                    JwtToken = JWTGenerator.GenerateJWTToken(_configuration, claims)
                }));
            }
            catch (Exception)
            {
                var justCreatedUser = await _userManager.FindByNameAsync(loginCredentials.Username);

                if (justCreatedUser != null)
                {
                    var userInfo = _applicationContext
                                   .UserInfo
                                   .FirstOrDefault(u => u.Id == justCreatedUser.Id);

                    if (userInfo != null)
                    {
                        _applicationContext
                        .UserInfo
                        .Remove(userInfo);
                    }

                    var businessInfo = _applicationContext
                                       .BusinessInfo
                                       .FirstOrDefault(u => u.Id == justCreatedUser.Id);

                    if (businessInfo != null)
                    {
                        _applicationContext
                        .BusinessInfo
                        .Remove(businessInfo);
                    }

                    _applicationContext.SaveChanges();
                    await _userManager.DeleteAsync(justCreatedUser);
                }

                return(BadRequest());
            }
        }