Skip to content

Commit

Permalink
update google login with retrieving cookis from android webview.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwecho committed Jan 5, 2024
1 parent f3aa0ab commit eb357f7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/V2ex.Api/UrlUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public static class UrlUtilities
{
public const string BASE_URL = "https://www.v2ex.com";

public const string BASE_DOMAIN = "www.v2ex.com";

public static string CompleteUrl(string url)
{
if(url.StartsWith("https://") || url.StartsWith("http://"))
Expand Down
15 changes: 11 additions & 4 deletions src/V2ex.Blazor.Shared/Components/Shared/SideBar.razor
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,17 @@
{
<div class="flex justify-end">
<div class="flex flex-row w-auto p-2 items-center space-x-1 bg-gray-200 rounded-lg">
<img class="w-4 h-4" src="https://v2ex.com/static/img/[email protected]" />
<span class="text-gray-400">@currentUser.MoneyGold</span>
<img class="w-4 h-4" src="https://v2ex.com/static/img/[email protected]" />
<span class="text-gray-400">@currentUser.MoneySilver</span>
@if (currentUser.MoneyGold>0)
{
<img class="w-4 h-4" src="https://v2ex.com/static/img/[email protected]" />
<span class="text-gray-400">@currentUser.MoneyGold</span>
}

@if (!(currentUser.MoneyGold == 0 && currentUser.MoneySilver == 0))
{
<img class="w-4 h-4" src="https://v2ex.com/static/img/[email protected]" />
<span class="text-gray-400">@currentUser.MoneySilver</span>
}
<img class="w-4 h-4" src="https://v2ex.com/static/img/[email protected]" />
<span class="text-gray-400">@currentUser.MoneyBronze</span>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/V2ex.Blazor/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Reflection;
using V2ex.Api;
using V2ex.Blazor.Pages;
using V2ex.Blazor.Services;
using IPreferences = V2ex.Blazor.Services.IPreferences;
Expand Down
54 changes: 35 additions & 19 deletions src/V2ex.Blazor/Pages/LoginWithGooglePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using System.Net;
using V2ex.Api;
using V2ex.Blazor.Services;
Expand All @@ -12,21 +13,22 @@ public LoginWithGooglePage(string once, Action<bool> loginCallback)
this.BindingContext = this.ViewModel =
InstanceCreator.Create<LoginWithGooglePageViewModel>();
this.ViewModel.Once = once;
this.Logger = InstanceCreator.Create<ILogger<LoginWithGooglePage>>();

this.CookieContainerService = InstanceCreator.Create<CookieContainerService>();
this.LoginCallback = loginCallback;

this.WebView.UserAgent = this.ViewModel.UserAgent;

#if !WINDOWS
// The app crashes when the WebView tries to access the cookie container on Windows.
#if IOS || MACCATALYST
this.WebView.Cookies = this.CookieContainerService.Container;
#endif
}

private LoginWithGooglePageViewModel ViewModel { get; }
private CookieContainerService CookieContainerService { get; }
private Action<bool> LoginCallback { get; }
private ILogger<LoginWithGooglePage> Logger { get; }

private bool isAuthenticated;

Expand All @@ -40,24 +42,25 @@ private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
{
this.Dispatcher.DispatchAsync(async () =>
{
await ExchangeCookies();
await this.Navigation.PopAsync();
this.LoginCallback(true);
try
{
#if ANDROID || WINDOWS
await ExchangeCookies();
#endif
await this.Navigation.PopAsync();
this.LoginCallback(true);
}
catch (Exception exception)
{
this.Logger.LogError(exception, "Exchange cookies failed.");
}
});
}

if(e.Url.StartsWith($"{UrlUtilities.BASE_URL}/auth/google?once"))
{
if(this.WebView.Cookies!=null)
{
var cookies = this.WebView.Cookies.GetAllCookies();
}
}
}

#if WINDOWS
private async Task ExchangeCookies()
{
#if WINDOWS
var webView = this.WebView.Handler!.PlatformView as Microsoft.UI.Xaml.Controls.WebView2;
var cookieManager = webView!.CoreWebView2.CookieManager;

Expand All @@ -67,15 +70,29 @@ private async Task ExchangeCookies()

this.CookieContainerService.Container.Add(cookie);
}
#else
await Task.CompletedTask;
#endif
#endif

#if ANDROID
private Task ExchangeCookies()
{
var cookieManager = Android.Webkit.CookieManager.Instance;

var cookies = (cookieManager?.GetCookie(UrlUtilities.BASE_URL))
?? throw new InvalidOperationException("Can not retrieve cookies from WebView.");
foreach (var item in cookies.Split(";"))
{
var key = item.Split("=")[0].Trim();
var value = item.Split("=")[1].Trim();
var cookie = new Cookie(key, value, "/", UrlUtilities.BASE_DOMAIN);
this.CookieContainerService.Container.Add(cookie);
}
return Task.CompletedTask;
}
#endif

#if WINDOWS
protected override async void OnHandlerChanged()
{

if(this.WebView.Handler == null)
{
throw new Exception("WebView.Handler is null");
Expand All @@ -96,7 +113,6 @@ protected override async void OnHandlerChanged()
var cookie2 = cookieManager.CreateCookie(cookie.Name, cookie.Value, cookie.Domain, cookie.Path);
cookieManager.AddOrUpdateCookie(cookie2);
}

}
#endif

Expand Down

0 comments on commit eb357f7

Please sign in to comment.