Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Http;
- using Newtonsoft.Json;
- using System.Text;
- using System.Collections.Generic;
- using Xamarin.Forms;
- namespace Hackatown
- {
- public partial class Login : ContentPage
- {
- public Login()
- {
- InitializeComponent();
- NavigationPage.SetHasNavigationBar(this, false);
- usernameEntry.Completed += (s, e) => passwordEntry.Focus();
- passwordEntry.Completed += BtnLogin_Clicked;
- btnLogin.Clicked += BtnLogin_Clicked;
- }
- async void BtnLogin_Clicked(object sender, EventArgs e)
- {
- btnLogin.IsEnabled = false;
- try
- {
- var client = new HttpClient();
- client.BaseAddress = new Uri("https://lassondehacks.io:8080/");
- string usernameLabel = "\"" + "username" + "\"";
- string passwordLabel = "\"" + "password" + "\"";
- string username = "\"" + usernameEntry.Text + "\"";
- string password = "\"" + passwordEntry.Text + "\"";
- string jsonData = @"{" + usernameLabel + " : " + username + ", " + passwordLabel + " : " + password + "}";
- var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
- HttpResponseMessage response = await client.PostAsync("/auth/login", content);
- var result = await response.Content.ReadAsStringAsync();
- AuthentificationDecrypter auth = JsonConvert.DeserializeObject<AuthentificationDecrypter>(result);
- if (auth.success)
- {
- await Navigation.PushModalAsync(new MasterPage(auth));
- btnLogin.IsEnabled = true;
- usernameEntry.Text = "";
- passwordEntry.Text = "";
- }
- else {
- IsBusy = false;
- XFToast.LongMessage("Erreur de connexion, nom d'utilisateur ou mot de passe incorrect");
- btnLogin.IsEnabled = true;
- passwordEntry.Text = "";
- }
- }
- catch(Exception exep) {
- IsBusy = false;
- XFToast.LongMessage("Erreur de connexion");
- btnLogin.IsEnabled = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement