TheShopCritics2/TSC2/Components/Pages/GoogleSignIn.razor

98 lines
3.7 KiB
Plaintext
Raw Normal View History

2024-08-05 17:13:29 +00:00
@using System.Net.Http;
@using System.Text.Json;
@using TSC2.Components.Layout;
@page "/signin-google"
<h1>This is Google Page</h1>
@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";
dct["redirect_uri"] = TSC2.Components.Layout.MainLayout.Redirect;
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);
Console.WriteLine("AUTH: " + 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<GoogleProfileResponse>(profile_json);
Console.WriteLine("PROFILE: " + profile_json);
if (MainLayout.Session.Count == 0) // Fpr security, only add to the session if nothing exists in the session.
{
MainLayout.Session.Add("id", profileResponse.id);
MainLayout.Session.Add("email", profileResponse.email);
MainLayout.Session.Add("name", profileResponse.name);
MainLayout.UpdateGreeting();
Console.WriteLine("Signed in successfully.");
}
}
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; }
}
}