public ActionResult ClientLogin(MainModel model)
        {
            //tries a client regular login
            Tuple <object, HttpStatusCode> returnTuple = httpClient.PostRequest(ApiConfigs.UserLoginRoute, model.UserLogin);

            if (returnTuple.Item2 == HttpStatusCode.OK)
            {
                JObject jobj = new JObject();
                jobj = (JObject)returnTuple.Item1;
                LoginRegisterResponse obj = jobj.ToObject <LoginRegisterResponse>();
                model.LoggedInUser = obj.user;
                SaveDetailsToSession(obj.user);
                SaveTokenToSession(obj.token);
                return(RedirectToAction("Index", mainModel));
            }
            else if (returnTuple.Item2 == HttpStatusCode.Conflict)
            {
                ViewBag.ErrorMessage = "Username or password are unvalid.";
                return(View("Index", mainModel));
            }
            else
            {
                ViewBag.ErrorMessage = "An error has occurred.";
                return(View("Index", mainModel));
            }
        }
 public ActionResult SendRegister(MainModel model)
 {
     //tries to send a user registration
     if (ModelState.IsValid)
     {
         if (model.UserRegister.Password != model.UserRegister.PasswordConfirm)
         {
             ViewBag.ErrorMessage = "Passwords dont match";
             return(View("Register", model));
         }
         Tuple <object, HttpStatusCode> returnTuple1 = httpClient.PostRequest(ApiConfigs.UsernameExistsRoute, model.UserRegister.Username);
         if (returnTuple1.Item2 == HttpStatusCode.OK)
         {
             bool userNameExists = Convert.ToBoolean(returnTuple1.Item1);
             if (userNameExists)
             {
                 ViewBag.ErrorMessage = "Username already exists";
                 return(View("Register", model));
             }
             else
             {
                 Tuple <object, HttpStatusCode> returnTuple2 = httpClient.PostRequest(ApiConfigs.UserRegisterRoute, model.UserRegister);
                 if (returnTuple2.Item2 == HttpStatusCode.OK)
                 {
                     JObject jobj = new JObject();
                     jobj = (JObject)returnTuple2.Item1;
                     LoginRegisterResponse response = jobj.ToObject <LoginRegisterResponse>();
                     SaveTokenToSession(response.token);
                     SaveDetailsToSession(response.user);
                     ViewBag.SuccessMessage = "Registration successful";
                     return(RedirectToAction("Index", model));
                 }
                 else
                 {
                     ViewBag.ErrorMessage = "An Error has acquired";
                     return(View("Register", model));
                 }
             }
         }
         else
         {
             ViewBag.ErrorMessage = "an Error has acquired";
         }
     }
     return(View("Register", model));
 }
        public async void RegisterReturnsAValidJWTTokenOnValidInput()
        {
            DictionaryDBContext dbcontextMock = DBContextMock.GetContextMock();
            var               context         = new DefaultHttpContext();
            User              user            = dbcontextMock.Users.Single();
            FakeUserManager   userManager     = new FakeUserManager(user);
            FakeSignInManager signInManager   = new FakeSignInManager(user, "testpwd");

            var controller = new JSONDataController(userManager, signInManager, dbcontextMock);

            controller.ControllerContext = new ControllerContext()
            {
                HttpContext = context
            };
            var result = await controller.Register(new RegisterModel { Email = "*****@*****.**", Password = "******" });

            LoginRegisterResponse response = (LoginRegisterResponse)result.Value;

            Assert.NotNull(response.Token);
        }
        public ActionResult FacebookCallBack(string code)
        {
            var     fb             = new FacebookClient();
            dynamic facebookResult = fb.Post("oauth/access_token", new
            {
                client_id     = MainConfigs.FacebookAppId,
                client_secret = MainConfigs.FacebookSecretKey,
                redirect_uri  = RedirectUri.AbsoluteUri,
                code          = code
            });
            var accessToken = facebookResult.access_token;

            Session["AccessToken"] = accessToken;
            fb.AccessToken         = accessToken;
            dynamic me        = fb.Get("me?fields=link,first_name,last_name,email,id");
            string  email     = me.email;
            string  firstName = me.first_name;
            string  lastName  = me.last_name;
            string  userId    = me.id;

            FacebookUser facebookUser = new FacebookUser(userId, firstName, lastName);
            Tuple <object, HttpStatusCode> returnTuple = httpClient.PostRequest(ApiConfigs.FacebookLoginRoute, facebookUser);

            if (returnTuple.Item2 == HttpStatusCode.OK)
            {
                JObject jobj = new JObject();
                jobj = (JObject)returnTuple.Item1;
                LoginRegisterResponse obj = jobj.ToObject <LoginRegisterResponse>();
                mainModel.LoggedInUser = obj.user;
                SaveDetailsToSession(obj.user);
                SaveTokenToSession(obj.token);
                return(RedirectToAction("Index", mainModel));
            }
            ViewBag.ErrorMessage = "An error has occurred.";

            return(View("Index", mainModel));
        }