Exemplo n.º 1
0
        public async Task <bool> Inscription(UserRegister user)
        {
            Uri uri = new Uri(Constant.URL_USER_INSCRIPTION);

            try
            {
                string        json    = JsonSerializer.Serialize <UserRegister>(user);
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage response = null;
                response = await client.PostAsync(uri, content);

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


                GetLoginData code = JsonConvert.DeserializeObject <GetLoginData>(contentResponse);

                return(code.is_success);
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(false);
        }
Exemplo n.º 2
0
        public async Task <GetLoginData> RefreshToken(string token)
        {
            RefreshToken refreshToken = new RefreshToken(token);
            Uri          uri          = new Uri(Constant.URL_REFRESH_TOKEN);
            GetLoginData loginData    = null;

            try
            {
                string        json    = JsonSerializer.Serialize <RefreshToken>(refreshToken);
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage response = null;
                response = await client.PostAsync(uri, content);

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


                if (response.IsSuccessStatusCode)
                {
                    loginData = JsonConvert.DeserializeObject <GetLoginData>(contentResponse);


                    Debug.WriteLine(@"\tTodoItem successfully saved.");
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
            }

            return(loginData);
        }
Exemplo n.º 3
0
        public async Task <bool> UpdatePassword(UpdatePassword updatePassword, string token)
        {
            Uri uri = new Uri(Constant.URL_UPDATE_PASSWORD);

            try
            {
                string        json    = JsonSerializer.Serialize <UpdatePassword>(updatePassword);
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage response = null;
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                var method = new HttpMethod("PATCH");

                var request = new HttpRequestMessage(method, uri)
                {
                    Content = new StringContent(
                        JsonConvert.SerializeObject(updatePassword),
                        Encoding.UTF8, "application/json")
                };
                response = await client.SendAsync(request);

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

                GetLoginData code = JsonConvert.DeserializeObject <GetLoginData>(contentResponse);

                return(code.is_success);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        private async void Button_OnClicked_Login(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.login.Text) ||
                string.IsNullOrWhiteSpace(this.login.Text) ||
                string.IsNullOrEmpty(this.mdp.Text) ||
                string.IsNullOrWhiteSpace(this.mdp.Text))
            {
                await DisplayAlert("Alert", "Login ou Mot de passe incorrecte", "OK");
            }
            else
            {
                Barrel.ApplicationId = Constant.KEY_LOGIN;
                Login login = new Login(this.login.Text, mdp.Text);


                GetLoginData loginData = await App.PizzaManager.GetTokenLogin(login);

                if (loginData != null)
                {
                    if (Barrel.Current.Exists(login.login))
                    {
                        App.PizzaManager.SaveLoginInProperties(login);
                        await Navigation.PopAsync();
                    }
                    else
                    {
                        App.PizzaManager.SaveLoginInProperties(login);
                        await App.PizzaManager.GetAuthentificationToken(login);

                        // await Navigation.PopAsync();
                        await Navigation.PopAsync();
                    }
                }
                else
                {
                    await DisplayAlert("Alert", "Login ou Mot de passe incorrecte", "OK");
                }
            }
        }
Exemplo n.º 5
0
        public async Task <GetLoginData> GetTokenLogin(Login login)
        {
            Uri          uri       = new Uri(Constant.URL_LOGIN);
            GetLoginData loginData = null;

            try
            {
                string        json    = JsonSerializer.Serialize <Login>(login, serializerOptions);
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage response = null;
                response = await client.PostAsync(uri, content);

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


                if (response.IsSuccessStatusCode)
                {
                    loginData = JsonConvert.DeserializeObject <GetLoginData>(contentResponse);


                    Debug.WriteLine(@"\tTodoItem successfully saved.");
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"\tERROR {0}", ex.Message);

                Debug.WriteLine("IMPOSSIBLE Connect", ex.Message);
            }

            return(loginData);
        }
Exemplo n.º 6
0
        public async Task <string> GetAuthentificationToken(Login login)
        {
            try
            {
                var key = login.login;
                //Dev handles checking if cache is expired
                if (!Barrel.Current.IsExpired(key))
                {
                    var d = Barrel.Current.Get <GetLoginData>(key: key);
                    return(d.data.access_token);
                }
                else if (Barrel.Current.Exists(key) && Barrel.Current.IsExpired(key: key))
                {
                    GetLoginData loginDataRes = Barrel.Current.Get <GetLoginData>(key: key);
                    loginDataRes = await RefreshToken(loginDataRes.data.refresh_token);

                    if (loginDataRes != null)
                    {
                        Barrel.Current.Add(key: key, data: loginDataRes, expireIn: TimeSpan.FromSeconds(loginDataRes.data.expires_in));
                        return(loginDataRes.data.access_token);
                    }
                }


                //Saves the cache and pass it a timespan for expiration


                GetLoginData dataUser = await GetTokenLogin(login);

                Barrel.Current.Add(key: key, data: dataUser, expireIn: TimeSpan.FromSeconds(5));
                return(dataUser.data.access_token);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Catches the Mediator message and invokes the <see cref="GetLoginData"/> event.
 /// </summary>
 /// <param name="value"></param>
 private void OnGetLoginData(object value)
 {
     GetLoginData?.Invoke(this, null);
 }