Skip to content

Commit

Permalink
auth sync
Browse files Browse the repository at this point in the history
  • Loading branch information
zeusongit committed Nov 28, 2023
1 parent 30b861d commit 5e9bf4f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/DynamoCore/Core/IDSDKManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Dynamo.Core
/// <summary>
/// The class to provide auth APIs for IDSDK related methods.
/// </summary>
public class IDSDKManager : IOAuth2AuthProvider, IOAuth2AccessTokenProvider
public class IDSDKManager : IOAuth2AuthProvider, IOAuth2AccessTokenProvider, IDisposable
{
/// <summary>
/// Used by the auth provider to request authentication.
Expand Down Expand Up @@ -228,8 +228,10 @@ private bool Initialize()
}

bool ret = GetClientIDAndServer(out idsdk_server server, out string client_id);
if (ret)
if (ret)
{
Client.LogoutCompleteEvent += AuthCompleteEventHandler;
Client.LoginCompleteEvent += AuthCompleteEventHandler;
ret = SetProductConfigs("Dynamo", server, client_id);
Client.SetServer(server);
return ret;
Expand All @@ -253,6 +255,11 @@ private bool Deinitialize()
}
return false;
}
public void Dispose()
{
Client.LoginCompleteEvent -= AuthCompleteEventHandler;
Client.LogoutCompleteEvent -= AuthCompleteEventHandler;
}
private bool GetClientIDAndServer(out idsdk_server server, out string client_id)
{
server = idsdk_server.IDSDK_PRODUCTION_SERVER;
Expand All @@ -273,6 +280,12 @@ private bool GetClientIDAndServer(out idsdk_server server, out string client_id)
}
return !string.IsNullOrEmpty(client_id);
}

// Event handler for LogoutCompleteEvent and LoginCompleteEvent that is thrown whenever the user's auth state changes.
private void AuthCompleteEventHandler(object sender, Client.TypedEventArgs e)
{
OnLoginStateChanged(LoginState);
}
#endregion
}
}
21 changes: 17 additions & 4 deletions src/DynamoCoreWpf/Controls/ShortcutToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Windows;
using System.Collections.Generic;
using System;

namespace Dynamo.UI.Controls
{
Expand Down Expand Up @@ -57,13 +58,14 @@ public ShortcutToolbar(DynamoViewModel dynamoViewModel)
authManager = dynamoViewModel.Model.AuthenticationManager;
if (authManager.IsLoggedInInitial())
{
authManager.LoginStateChanged += SignOutHandler;
authManager.LoginStateChanged += AuthChangeHandler;
}
else {
logoutOption.Visibility = Visibility.Collapsed;
}

this.Loaded += ShortcutToolbar_Loaded;
this.Unloaded += ShortcutToolbar_Unloaded;
}

private void ShortcutToolbar_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -73,14 +75,26 @@ private void ShortcutToolbar_Loaded(object sender, RoutedEventArgs e)
DynamoViewModel.OnRequestShorcutToolbarLoaded(RightMenu.ActualWidth);
}

private void SignOutHandler(LoginState status)
private void ShortcutToolbar_Unloaded(object sender, RoutedEventArgs e)
{
authManager.LoginStateChanged -= AuthChangeHandler;
this.Loaded -= ShortcutToolbar_Loaded;
this.Unloaded -= ShortcutToolbar_Unloaded;
}

private void AuthChangeHandler(LoginState status)
{
if (status == LoginState.LoggedOut)
{
LoginButton.ToolTip = Wpf.Properties.Resources.SignInButtonContentToolTip;
txtSignIn.Text = Wpf.Properties.Resources.SignInButtonText;
logoutOption.Visibility = Visibility.Collapsed;
authManager.LoginStateChanged -= SignOutHandler;
}
else if (status == LoginState.LoggedIn)
{
txtSignIn.Text = authManager.Username;
logoutOption.Visibility = Visibility.Visible;
LoginButton.ToolTip = null;
}
}

Expand Down Expand Up @@ -115,7 +129,6 @@ private void LoginButton_OnClick(object sender, RoutedEventArgs e)
tb.Text = authManager.Username;
logoutOption.Visibility = Visibility.Visible;
LoginButton.ToolTip = null;
authManager.LoginStateChanged += SignOutHandler;
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Dynamo.Utilities;
using Dynamo.ViewModels;
using DynamoUtilities;
using Greg.AuthProviders;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;

Expand Down Expand Up @@ -236,6 +237,11 @@ private void LaunchDynamo(bool isCheckboxChecked)
dynamoView?.Activate();
}

private void OnLoginStateChanged(LoginState state)
{
HandleSignInStatusChange(authManager.IsLoggedIn());
}

/// <summary>
/// Once main window is initialized, Dynamic Splash screen should finish loading
/// </summary>
Expand All @@ -250,6 +256,7 @@ private void OnStaticScreenReady()
// If user is launching Dynamo for the first time or chose to always show splash screen, display it. Otherwise, display Dynamo view directly.
if (viewModel.PreferenceSettings.IsFirstRun || viewModel.PreferenceSettings.EnableStaticSplashScreen)
{
authManager.LoginStateChanged += OnLoginStateChanged;
SetSignInStatus(authManager.IsLoggedInInitial());
SetLoadingDone();
}
Expand Down Expand Up @@ -393,6 +400,18 @@ await webView.CoreWebView2.ExecuteScriptAsync("window.setSignInStatus({" +
}
}

/// <summary>
/// Handle the login status changes on splash screen.
/// </summary>
internal async void HandleSignInStatusChange(bool status)
{
if (webView?.CoreWebView2 != null)
{
await webView.CoreWebView2.ExecuteScriptAsync("window.handleSignInStateChange({" +
$"status: \"" + status + "\"})");
}
}

/// <summary>
/// Setup the values for all labels on splash screen using resources
/// </summary>
Expand Down

0 comments on commit 5e9bf4f

Please sign in to comment.