Exemplo n.º 1
0
        public async Task <bool> Login(string email, string password, bool isNewUser)
        {
            string              postURL  = Central.ServerURL + "/api/_auth/local";
            CentralLogin        login    = new CentralLogin(email, password, isNewUser);
            var                 content  = new StringContent(JsonConvert.SerializeObject(login), Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(postURL, content);

            if (!response.IsSuccessStatusCode)
            {
                return(false);
            }

            string resContent = await response.Content.ReadAsStringAsync();

            CentralUser user = JsonConvert.DeserializeObject <CentralUser>(resContent);

            if (user.Tokens.Count == 0)
            {
                // create token
                user = await CreateAuthToken(user);
            }

            Central.APIKey = user.Tokens[0];

            UpdateRequestHeaders();
            WriteCentralConfig();

            return(true);
        }
Exemplo n.º 2
0
        public async Task <CentralUser> CreateAuthToken(CentralUser user)
        {
            string randomTokenURL        = Central.ServerURL + "/api/randomToken";
            HttpResponseMessage response = await client.GetAsync(randomTokenURL);

            if (!response.IsSuccessStatusCode)
            {
                // TODO: throw an error
                return(null);
            }

            string resContent = await response.Content.ReadAsStringAsync();

            CentralToken t = JsonConvert.DeserializeObject <CentralToken>(resContent);

            user.Tokens.Add(t.Token);

            string tokenObj = "{ \"tokens\": " + JsonConvert.SerializeObject(user.Tokens) + " } ";

            string postURL     = Central.ServerURL + "/api/user/" + user.Id;
            var    postContent = new StringContent(tokenObj, Encoding.UTF8, "application/json");

            response = await client.PostAsync(postURL, postContent);

            if (!response.IsSuccessStatusCode)
            {
                // TODO: thrown an error
                return(null);
            }

            resContent = await response.Content.ReadAsStringAsync();

            user = JsonConvert.DeserializeObject <CentralUser>(resContent);

            return(user);
        }