2024-08-05 17:13:29 +00:00
|
|
|
@using System.Net.Http;
|
|
|
|
@using System.Text.Json;
|
|
|
|
@using TSC2.Components.Layout;
|
2024-08-13 16:42:32 +00:00
|
|
|
@using TSC2.Components.CSharp;
|
2024-08-05 17:13:29 +00:00
|
|
|
|
|
|
|
@page "/signin-google"
|
2024-09-12 14:13:00 +00:00
|
|
|
<title>The Shop Critics</title>
|
2024-08-08 17:17:41 +00:00
|
|
|
@inject NavigationManager _navigationManager
|
|
|
|
|
2024-08-05 17:13:29 +00:00
|
|
|
@code {
|
|
|
|
[Parameter][SupplyParameterFromQuery] public string? code { get; set; }
|
|
|
|
[Parameter][SupplyParameterFromQuery] public string? scope { get; set; }
|
|
|
|
[Parameter][SupplyParameterFromQuery] public string? authuser { get; set; }
|
|
|
|
[Parameter][SupplyParameterFromQuery] public string? prompt { get; set; }
|
|
|
|
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender) // Executes every page load; run inexpensive rendering code here
|
|
|
|
{
|
|
|
|
if (!firstRender)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var dct = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
dct["client_id"] = TSC2.Components.CSharp.Credentials.GoogleClientId;
|
|
|
|
dct["client_secret"] = TSC2.Components.CSharp.Credentials.GoogleClientSecret;
|
|
|
|
dct["code"] = code;
|
|
|
|
dct["grant_type"] = "authorization_code";
|
2024-08-08 17:17:41 +00:00
|
|
|
dct["redirect_uri"] = TSC2.Components.Layout.MainLayout.GoogleRedirect;
|
2024-08-05 17:13:29 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
string auth_json = "";
|
|
|
|
string profile_json = "";
|
|
|
|
|
|
|
|
// Get access token for the auth session
|
|
|
|
using(var client = new HttpClient())
|
|
|
|
using(var content = new FormUrlEncodedContent(dct))
|
|
|
|
{
|
|
|
|
HttpResponseMessage response = await client.PostAsync("https://oauth2.googleapis.com/token", content);
|
|
|
|
auth_json = await response.Content.ReadAsStringAsync();
|
|
|
|
}
|
|
|
|
OAuthTokenResponse tokenResponse = JsonSerializer.Deserialize<OAuthTokenResponse>(auth_json);
|
2024-08-13 16:42:32 +00:00
|
|
|
|
2024-08-05 17:13:29 +00:00
|
|
|
// Use the access token to access their information; name, email, & token
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
{
|
|
|
|
profile_json = await client.GetStringAsync("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + tokenResponse.access_token);
|
|
|
|
// I think this is the json response?? who knows!
|
|
|
|
}
|
|
|
|
GoogleProfileResponse profileResponse = JsonSerializer.Deserialize<GoogleProfileResponse>(profile_json);
|
|
|
|
|
|
|
|
if (MainLayout.Session.Count == 0) // Fpr security, only add to the session if nothing exists in the session.
|
|
|
|
{
|
2024-08-13 16:42:32 +00:00
|
|
|
// Add variables to the session
|
2024-08-05 17:13:29 +00:00
|
|
|
MainLayout.Session.Add("id", profileResponse.id);
|
|
|
|
MainLayout.Session.Add("email", profileResponse.email);
|
|
|
|
MainLayout.Session.Add("name", profileResponse.name);
|
2024-09-12 14:13:00 +00:00
|
|
|
MainLayout.isSignedIn = true;
|
2024-08-05 17:13:29 +00:00
|
|
|
MainLayout.UpdateGreeting();
|
2024-08-13 16:42:32 +00:00
|
|
|
|
|
|
|
// Contact database
|
|
|
|
DatabaseManager.SignInGoogle();
|
2024-08-05 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:17:41 +00:00
|
|
|
_navigationManager.NavigateTo("/");
|
2024-08-05 17:13:29 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
// Handler
|
|
|
|
Console.WriteLine("ERORR" + ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Data classes to handle the Http responses
|
|
|
|
public class OAuthTokenResponse
|
|
|
|
{
|
|
|
|
public string access_token { get; set; }
|
|
|
|
public int expires_in { get; set; }
|
|
|
|
public string refresh_token { get; set; }
|
|
|
|
public string scope { get; set; }
|
|
|
|
public string token_type { get; set; }
|
|
|
|
public string id_token { get; set; }
|
|
|
|
public string error { get; set; }
|
|
|
|
public string error_description { get; set; }
|
|
|
|
|
|
|
|
public bool IsSuccess => string.IsNullOrEmpty(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class GoogleProfileResponse
|
|
|
|
{
|
|
|
|
public string id { get; set; }
|
|
|
|
public string email { get; set; }
|
|
|
|
public string email_verified { get; set; }
|
|
|
|
public string family_name { get; set; }
|
|
|
|
public string given_name { get; set; }
|
|
|
|
public string name { get; set; }
|
|
|
|
public string picture { get; set; }
|
|
|
|
public string profile { get; set; }
|
|
|
|
}
|
|
|
|
}
|