Exemple #1
0
        public IActionResult Register(RegisterAccountViewModel model)
        {
            IActionResult result = null;

            // Does user already exist
            try
            {
                // Generate a password hash
                var pwdMgr  = new PasswordManager(model.Password);
                var roleMgr = new RoleManager(_db);

                // Create a user object
                var user = new UserItem
                {
                    FirstName       = model.FirstName,
                    LastName        = model.LastName,
                    Hash            = pwdMgr.Hash,
                    Salt            = pwdMgr.Salt,
                    Email           = model.Email,
                    RoleId          = roleMgr.UserRoleId,
                    WorkoutGoals    = model.Goals,
                    Username        = model.UserName,
                    WeeklyExercise  = model.WeeklyExercise,
                    ExperienceLevel = model.ExperienceLevel
                };

                var userAccount = new UserAccountModel
                {
                    User = user
                };

                // Save the user account
                _db.AddUserAccount(userAccount);

                // Generate a token
                var token = _tokenGenerator.GenerateToken(user.Username, roleMgr.RoleName);

                result = Ok(token);
            }
            catch (Exception)
            {
                result = BadRequest(new { Message = "Username has already been taken." });
                //result = Ok(new { error = true });
            }

            // Return the token
            return(result);
        }