diff --git a/BodyshopUploader/App.config b/BodyshopUploader/App.config index f5e0f5a..5235ea7 100644 --- a/BodyshopUploader/App.config +++ b/BodyshopUploader/App.config @@ -1,7 +1,7 @@  - +
diff --git a/BodyshopUploader/BodyshopUploader.csproj b/BodyshopUploader/BodyshopUploader.csproj index 7c0cb35..356321b 100644 --- a/BodyshopUploader/BodyshopUploader.csproj +++ b/BodyshopUploader/BodyshopUploader.csproj @@ -58,6 +58,12 @@ ..\packages\FirebaseAuthentication.net.3.4.0\lib\netstandard1.1\Firebase.Auth.dll + + ..\packages\GraphQL.Client.1.0.3\lib\netstandard2.0\GraphQL.Client.dll + + + ..\packages\GraphQL.Common.1.0.3\lib\netstandard2.0\GraphQL.Common.dll + ..\packages\Hardcodet.NotifyIcon.Wpf.1.0.8\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll diff --git a/BodyshopUploader/Utils/Auth.cs b/BodyshopUploader/Utils/Auth.cs index 22a132c..8e6dbda 100644 --- a/BodyshopUploader/Utils/Auth.cs +++ b/BodyshopUploader/Utils/Auth.cs @@ -12,9 +12,6 @@ namespace BodyshopUploader.Utils { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); public static FirebaseAuthLink authlink; - public static string authToken; - static string refreshToken; - static int tokenExpiration; static FirebaseAuthProvider ap = new FirebaseAuthProvider(new FirebaseConfig(Utils.AppMetaData.FirebaseAPIKey_DEV)); //TODO: Update this to be a dynamic key. Perhaps a function that fetches? private static Timer tokenTimer = new Timer(); @@ -23,15 +20,17 @@ namespace BodyshopUploader.Utils try { authlink = await ap.SignInWithEmailAndPasswordAsync(Username, Password); - authToken = authlink.FirebaseToken; - refreshToken = authlink.RefreshToken; - tokenExpiration = authlink.ExpiresIn; - logger.Trace("Firebase Auth Token {0}.", authToken); - logger.Trace("Firebase Refresh Token {0}.", refreshToken); - logger.Trace("Firebase Auth Token expires in {0} seconds.", tokenExpiration); + authlink.FirebaseAuthRefreshed += Authlink_FirebaseAuthRefreshed; + + logger.Trace("Firebase Auth Token {0}.", authlink.FirebaseToken); + logger.Trace("Firebase Refresh Token {0}.", authlink.RefreshToken); + logger.Trace("Firebase Auth Token expires in {0} seconds.", authlink.ExpiresIn); + //tokenTimer.Interval = (authlink.ExpiresIn - 600) * 1000; //Set the token to refresh 10 minutes before it has to. + tokenTimer.Interval = 10000; //Set the token to refresh 10 minutes before it has to. + logger.Trace("Refresh timer interval set to {0}ms", (authlink.ExpiresIn - 600) * 1000); + tokenTimer.Elapsed += TokenTimer_Tick; + tokenTimer.Start(); - tokenTimer.Interval = (tokenExpiration - 600) * 1000; //Set the token to refresh 10 minutes before it has to. - tokenTimer.Elapsed += TokenTimer_Elapsed; return (true, null); } catch (FirebaseAuthException Ex) @@ -60,10 +59,31 @@ namespace BodyshopUploader.Utils } } - private static void TokenTimer_Elapsed(object sender, ElapsedEventArgs e) + public static async Task Refresh() + { + logger.Info("Old Token {0}", authlink.RefreshToken); + await authlink.GetFreshAuthAsync(); + authlink = await ap.RefreshAuthAsync(authlink); + logger.Info("new Token {0}", authlink.FirebaseToken); + } + + private static void Authlink_FirebaseAuthRefreshed(object sender, FirebaseAuthEventArgs e) + { + logger.Debug("Auth token refreshed!"); + logger.Debug("New token: {0}", e.FirebaseAuth.FirebaseToken); + } + + private static void TokenTimer_Tick(object sender, ElapsedEventArgs e) { //Gotta do some stuff now that i got a new token! //Maybe the token auto refreshes? + logger.Info("Timer Old Token {0}", authlink.FirebaseToken); + //await authlink.RefreshUserDetails(); + //logger.Info("new Token {0}", authlink.FirebaseToken); + //tokenTimer.Stop(); + //tokenTimer.Interval = (authlink.ExpiresIn - 600) * 1000; //Set the token to refresh 10 minutes before it has to. + //tokenTimer.Interval = 10000; + //tokenTimer.Start(); } } } diff --git a/BodyshopUploader/ViewModels/MainViewModel.commands.cs b/BodyshopUploader/ViewModels/MainViewModel.commands.cs index 92496db..85eaf73 100644 --- a/BodyshopUploader/ViewModels/MainViewModel.commands.cs +++ b/BodyshopUploader/ViewModels/MainViewModel.commands.cs @@ -14,9 +14,9 @@ namespace BodyshopUploader.ViewModels { _testCommand = new RelayCommand( p => true, - p => + async p => { - logger.Info("test command clicked"); + await Utils.Auth.Refresh(); }); } return _testCommand; diff --git a/BodyshopUploader/packages.config b/BodyshopUploader/packages.config index 4c5ee6e..b618089 100644 --- a/BodyshopUploader/packages.config +++ b/BodyshopUploader/packages.config @@ -1,9 +1,12 @@  + + +