@using System.Net.Http; @using System.Text.Json; @using TSC2.Components.Layout; @using TSC2.Components.CSharp; @page "/signin-google" @inject NavigationManager _navigationManager @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(); dct["client_id"] = TSC2.Components.CSharp.Credentials.GoogleClientId; dct["client_secret"] = TSC2.Components.CSharp.Credentials.GoogleClientSecret; dct["code"] = code; dct["grant_type"] = "authorization_code"; dct["redirect_uri"] = TSC2.Components.Layout.MainLayout.GoogleRedirect; 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(auth_json); // 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(profile_json); if (MainLayout.Session.Count == 0) // Fpr security, only add to the session if nothing exists in the session. { // Add variables to the session MainLayout.Session.Add("id", profileResponse.id); MainLayout.Session.Add("email", profileResponse.email); MainLayout.Session.Add("name", profileResponse.name); MainLayout.UpdateGreeting(); // Contact database DatabaseManager.SignInGoogle(); } _navigationManager.NavigateTo("/"); } 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; } } }