public HttpResponseMessage RegisterUser(UserModel model)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                this.ValidateUser(model);
                this.ValidateEmail(model.Email);

                var context = new TasksManagerDbContext();
                var dbUser = GetUserByUsernameOrEmail(model, context);
                if (dbUser != null)
                {
                    throw new InvalidOperationException("This user already exists in the database");
                }
                dbUser = new User()
                {
                    Username = model.Username,
                    Email = model.Email,
                    AuthenticationCode = model.AuthCode
                };
                context.Users.Add(dbUser);

                context.SaveChanges();

                var responseModel = new RegisterUserResponseModel()
                {
                    Id = dbUser.Id,
                    Username = dbUser.Username,
                };

                var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);
                return response;
            });
        }
 internal static void RegisterUser(string username, string email, string authenticationCode)
 {
     //Validation!!!!!
     //validate username
     //validate email
     //validate authentication code
     //use validation from WebAPI
     var userModel = new UserModel()
     {
         Username = username,
         Email = email,
         AuthCode = authenticationCode
     };
     HttpRequester.Post(BaseServicesUrl + "users/register",
         userModel);
 }
 internal static string LoginUser(string username, string authenticationCode)
 {
     //Validation!!!!!
     //validate username
     //validate email
     //validate authentication code
     //use validation from WebAPI
     var userModel = new UserModel()
     {
         Username = username,
         AuthCode = authenticationCode
     };
     var loginResponse = HttpRequester.Post<LoginResponseModel>(BaseServicesUrl + "auth/token",
         userModel);
     AccessToken = loginResponse.AccessToken;
     return loginResponse.Username;
 }
        public HttpResponseMessage LoginUser(UserModel model)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                this.ValidateUser(model);

                if (model == null)
                {
                    throw new FormatException("invalid username and/or password");
                }
                this.ValidateAuthCode(model.AuthCode);
                try
                {
                    this.ValidateUsername(model.Username);
                }
                catch (Exception ex)
                {
                    this.ValidateEmail(model.Email);
                }

                var context = new TasksManagerDbContext();
                var username = ((string.IsNullOrEmpty(model.Username)) ? model.Email : model.Username).ToLower();
                var user = context.Users.FirstOrDefault(u => u.Username == username || u.Email == username);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username or password");
                }
                if (user.AccessToken == null)
                {
                    user.AccessToken = this.GenerateAccessToken(user.Id);
                    context.SaveChanges();
                }
                var responseModel = new LoginResponseModel()
                {
                    Id = user.Id,
                    Username = user.Username,
                    AccessToken = user.AccessToken
                };
                var response = this.Request.CreateResponse(HttpStatusCode.OK, responseModel);
                return response;
            });
        }
 private void ValidateUser(UserModel userModel)
 {
     if (userModel == null)
     {
         throw new FormatException("Username and/or password are invalid");
     }
     this.ValidateUsername(userModel.Username);
     this.ValidateAuthCode(userModel.AuthCode);
 }
 private User GetUserByUsernameOrEmail(UserModel model, TasksManagerDbContext context)
 {
     var usernameToLower = model.Username.ToLower();
     var emailToLower = model.Email.ToLower();
     var dbUser = context.Users.FirstOrDefault(u => u.Username == usernameToLower || u.Email == emailToLower);
     return dbUser;
 }