Skip to content
Permalink
Browse files
Implemented fb login and get user currently lgged in
  • Loading branch information
elgezirn committed Nov 12, 2020
1 parent 9414c2b commit f9676cd86ab780aa1f760cf373cbdd10f5075491
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 10 deletions.
@@ -53,14 +53,17 @@
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1560" />
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1687" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="sqlite-net-pcl">
<Version>1.7.335</Version>
</PackageReference>
<PackageReference Include="SQLiteNetExtensions">
<Version>2.1.0</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
@@ -125,14 +125,17 @@
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.6.0.1141" />
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1687" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="sqlite-net-pcl">
<Version>1.7.335</Version>
</PackageReference>
<PackageReference Include="SQLiteNetExtensions">
<Version>2.1.0</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
<ItemGroup>
@@ -3,7 +3,8 @@ using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Trady.Views;
using System.IO;

using SQLite;
using Trady.Models;

namespace Trady
{
@@ -42,6 +43,14 @@ namespace Trady

protected override void OnStart()
{
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{

//var activeUsers = conn.Table<User>().Where(c => c.IsActive == "true").First();
//activeUsers.IsActive = "false";


}
}

protected override void OnSleep()
@@ -0,0 +1,22 @@
using System;
namespace Trady.Models
{
public class FacebookProfile
{
public string Name { get; set; }
public Picture Picture { get; set; }
public string Id { get; set; }

}

public class Picture
{
public Data Data { get; set; }
}

public class Data
{
public bool IsSilhouette { get; set; }
public string Url { get; set; }
}
}
@@ -11,13 +11,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.6.0.1141" />
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1687" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="sqlite-net-pcl" Version="1.7.335" />
<PackageReference Include="SQLiteNetExtensions" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<Reference Include="Mono.Android">
<HintPath>..\..\..\..\..\..\Library\Frameworks\Xamarin.Android.framework\Versions\11.0.2.0\lib\xamarin.android\xbuild-frameworks\MonoAndroid\v9.0\Mono.Android.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Trady.Views
{
public partial class MainPage : ContentPage

{

public MainPage()
{
InitializeComponent();
}

async void Fbbtn_Clicked(System.Object sender, System.EventArgs e)
{

await Navigation.PushAsync(new facebookLoginPage());
}


}
}
@@ -26,15 +26,13 @@
</Grid.RowDefinitions>
<!--Profile pic-->

<Image Source="pp1.jpeg" Aspect="AspectFill"/>
<Image x:Name="userImage" Aspect="AspectFill" HeightRequest="300" WidthRequest="300" />

<!--Profile details-->

<StackLayout Grid.Row="1" HorizontalOptions="CenterAndExpand">
<Label Text="Name: Andreas" FontSize="20" FontAttributes="Bold"/>
<Label Text="Email: ando@gmail.com" FontSize="20" FontAttributes="Bold"/>
<Label Text="# of trades: 4 " FontSize="20" FontAttributes="Bold"/>
<Label Text="Submitted Offers Response " FontSize="20" FontAttributes="Bold"/>
<Label x:Name="userName" Text="{Binding Name}" FontSize="20" FontAttributes="Bold"/>
<Label Text="# of trades: 4 " FontSize="20" FontAttributes="Bold"/>
</StackLayout>

</Grid>
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;

using SQLite;
using Trady.Models;
using Xamarin.Forms;

namespace Trady.Views
@@ -12,6 +13,22 @@ namespace Trady.Views
InitializeComponent();
}

protected override void OnAppearing()
{
base.OnAppearing();
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<User>();
var user = conn.Table<User>().FirstOrDefault(r => r.CurrentUser == true);

userName.Text = user.Name;
userImage.Source = user.Picture;

}

}


void tradesPagebtn_Clicked(System.Object sender, System.EventArgs e)
{

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Trady.Views
{
public partial class RecievedOfferPage : ContentPage
{
public RecievedOfferPage()
{
InitializeComponent();
}

async void acceptbtn_Clicked(System.Object sender, System.EventArgs e)
{
await Navigation.PushAsync(new MyTradesPage());
}

async void declinebtn_Clicked(System.Object sender, System.EventArgs e)
{
await Navigation.PushAsync(new MyTradesPage());
}
}
}
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Trady.Views
{
public partial class SubmitOfferPage : ContentPage
{
public SubmitOfferPage()
{
InitializeComponent();
}

async void submitOfferbtn_Clicked(System.Object sender, System.EventArgs e)
{
await Navigation.PushAsync(new MyTradesPage());
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Trady.Views.facebookLoginPage">
<ContentPage.Content>


<WebView x:Name="WebView" HeightRequest="10" WidthRequest="10" VerticalOptions="FillAndExpand"/>

</ContentPage.Content>
</ContentPage>
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using Newtonsoft.Json;
using Xamarin.Forms;
using Trady.Models;
using SQLite;

namespace Trady.Views
{
public partial class facebookLoginPage : ContentPage
{

//private string ClientId = "1272538809769102";

public facebookLoginPage()


{
InitializeComponent();

WebView.Source = "https://www.facebook.com/v9.0/dialog/oauth?client_id=1272538809769102&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token";
WebView.Navigated += WebView_Navigated;


}
private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
{
var AccessURL = e.Url;
if (AccessURL.Contains("access_token"))
{
AccessURL = AccessURL.Replace("https://www.facebook.com/connect/login_success.html#access_token=", string.Empty);
var accessToken = AccessURL.Split('&')[0];
HttpClient client = new HttpClient();
var response = client.GetStringAsync("https://graph.facebook.com/me?fields=name,picture,email,id&access_token=" + accessToken).Result;


var Data = JsonConvert.DeserializeObject<FacebookProfile>(response);


//connecto to database to check if user is already registered
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{


conn.Delete<User>(13);
var beforCurrentActiveUsers = conn.Table<User>().Where(r => r.CurrentUser == false);
var loggedInUser = conn.Table<User>().FirstOrDefault(c => c.FacebookId == Data.Id);

if(loggedInUser == null)
{

// Adding user to database
User user = new User()
{
Name = Data.Name,
//email = Data.email,
Picture = Data.Picture.Data.Url,
FacebookId = Data.Id,
CurrentUser= false

};

// establishin connection to database
conn.CreateTable<User>();
int rowsAdded = conn.Insert(user);

}

var activeUser = conn.Table<User>().FirstOrDefault(c => c.FacebookId == Data.Id);
conn.Update(new User {
UserId = activeUser.UserId,
CurrentUser = true,
FacebookId = activeUser.FacebookId,
Name = activeUser.Name,
Offers = activeUser.Offers,
Picture = activeUser.Picture,
Trades = activeUser.Trades

});

var beforeCurrentActiveUsers = conn.Table<User>().First(r => r.CurrentUser == true);

}




Navigation.PushAsync(new MyTradesPage());

}



}

}
}

0 comments on commit f9676cd

Please sign in to comment.