public HttpResponseMessage Register(RegisterViewModel model)
        {
            HttpResponseMessage response;
            if (ModelState.IsValid)
            {
                var existingUser = db.Users.FirstOrDefault(u => u.Email == model.Email);
                if (existingUser != null)
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "User already exist.");
                }

                //Create user and save to database
                var user = CreateUser(model);

                object dbUser;

                //Create token
                var token = CreateToken(user, out dbUser);

                response = Request.CreateResponse(new {dbUser, token});
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, new {success = false});
            }

            return response;
        }
        /// <summary>
        /// Create a new user and saves it to the database
        /// </summary>
        /// <param name="registerDetails"></param>
        /// <returns></returns>
        private User CreateUser(RegisterViewModel registerDetails)
        {
            var passwordSalt = CreateSalt();
            var user = new User
            {
                Salt = passwordSalt,
                Email = registerDetails.Email,
                PasswordHash = EncryptPassword(registerDetails.Password, passwordSalt)
            };

            var adminRole = db.Roles.FirstOrDefault(d => d.Name == "Admin");

            user.Roles.Add(new UserRole
            {
                User = user,
                Role = adminRole
            });

            db.Users.Add(user);
            db.SaveChanges();

            return user;
        }