@@ -125,26 +125,31 @@ function registerQuotaViewCommands(view: GitGuardianQuotaWebviewProvider) {
125125
126126export function activate ( context : ExtensionContext ) {
127127 // Check if ggshield if available
128+ commands . executeCommand ( 'setContext' , 'isAuthenticated' , false ) ;
129+
128130 const outputChannel = window . createOutputChannel ( "GitGuardian" ) ;
129131 let configuration = getConfiguration ( context ) ;
130- let authStatus : boolean = false ;
132+
131133 const ggshieldResolver = new GGShieldResolver (
132134 outputChannel ,
133135 context ,
134136 configuration
135137 ) ;
136138 const ggshieldViewProvider = new GitGuardianWebviewProvider (
137139 configuration ,
138- context . extensionUri
140+ context . extensionUri ,
141+ context
139142 ) ;
140143
141144 const ggshieldRemediationMessageViewProvider = new GitGuardianRemediationMessageWebviewProvider (
142145 configuration ,
143- context . extensionUri
146+ context . extensionUri ,
147+ context
144148 ) ;
145149 const ggshieldQuotaViewProvider = new GitGuardianQuotaWebviewProvider (
146150 configuration ,
147- context . extensionUri
151+ context . extensionUri ,
152+ context
148153 ) ;
149154 window . registerWebviewViewProvider ( "gitguardianView" , ggshieldViewProvider ) ;
150155 window . registerWebviewViewProvider (
@@ -173,15 +178,16 @@ export function activate(context: ExtensionContext) {
173178 . checkGGShieldConfiguration ( )
174179 . then ( ( ) => {
175180 // Check if ggshield is authenticated
176- authStatus = ggshieldAuthStatus ( configuration ) ;
177- const ggshieldApi = ggshieldApiKey ( configuration ) ;
178- if ( authStatus && ggshieldApi ) {
179- commands . executeCommand ( 'setContext' , 'isAuthenticated' , true ) ;
181+ ggshieldAuthStatus ( configuration , context ) ;
182+ if ( context . globalState . get ( "isAuthenticated" , false ) ) {
180183 updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
181- setApiKey ( configuration , ggshieldApi ) ;
184+ setApiKey ( configuration , ggshieldApiKey ( configuration ) ) ;
185+ ggshieldViewProvider . refresh ( ) ;
186+ ggshieldRemediationMessageViewProvider . refresh ( ) ;
187+ ggshieldQuotaViewProvider . refresh ( ) ;
188+
182189 } else {
183190 updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
184- authStatus = false ;
185191 }
186192 } )
187193 . then ( async ( ) => {
@@ -204,7 +210,9 @@ export function activate(context: ExtensionContext) {
204210 workspace . onDidSaveTextDocument ( ( textDocument ) => {
205211 // Check if the document is inside the workspace
206212 const workspaceFolder = workspace . getWorkspaceFolder ( textDocument . uri ) ;
207- if ( authStatus && workspaceFolder ) {
213+ console . log ( context . globalState . get ( "isAuthenticated" , false ) , workspaceFolder ) ;
214+ console . log ( '»»»»»»»»»»»»»»' , context . globalState . get ( "isAuthenticated" , false ) && workspaceFolder ) ;
215+ if ( context . globalState . get ( "isAuthenticated" , false ) && workspaceFolder ) {
208216 scanFile (
209217 textDocument . fileName ,
210218 textDocument . uri ,
@@ -247,28 +255,28 @@ export function activate(context: ExtensionContext) {
247255 ) ,
248256 commands . registerCommand ( "gitguardian.authenticate" , async ( ) => {
249257 commands . executeCommand ( "gitguardian.openSidebar" ) ;
250- const isAuthenticated = await loginGGShield (
258+ await loginGGShield (
251259 ggshieldResolver . configuration ,
252260 outputChannel ,
253- ggshieldViewProvider . getView ( ) as WebviewView
254- ) ;
255- if ( isAuthenticated ) {
256- authStatus = true ;
257- updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
258- commands . executeCommand ( 'setContext' , 'isAuthenticated' , true ) ;
259- const ggshieldApi = ggshieldApiKey ( configuration ) ;
260- setApiKey ( configuration , ggshieldApi ) ;
261+ ggshieldViewProvider . getView ( ) as WebviewView ,
262+ context
263+ ) . then ( ( ) => {
264+ if ( context . globalState . get ( "isAuthenticated" , false ) ) {
265+ updateStatusBarItem ( StatusBarStatus . ready , statusBar ) ;
266+ setApiKey ( configuration , ggshieldApiKey ( configuration ) ) ;
267+ } else {
268+ updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
269+ }
261270 ggshieldViewProvider . refresh ( ) ;
262271 ggshieldRemediationMessageViewProvider . refresh ( ) ;
263272 ggshieldQuotaViewProvider . refresh ( ) ;
264- } else {
265- updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
266- }
273+ } ) . catch ( ( err ) => {
274+ outputChannel . appendLine ( `Authentication failed: ${ err . message } ` ) ;
275+ } ) ;
267276 } ) ,
268277 commands . registerCommand ( "gitguardian.logout" , async ( ) => {
269- logoutGGShield ( ggshieldResolver . configuration ) ;
278+ logoutGGShield ( ggshieldResolver . configuration , context ) ;
270279 updateStatusBarItem ( StatusBarStatus . unauthenticated , statusBar ) ;
271- commands . executeCommand ( 'setContext' , 'isAuthenticated' , false ) ;
272280 setApiKey ( configuration , undefined ) ;
273281 ggshieldViewProvider . refresh ( ) ;
274282 ggshieldRemediationMessageViewProvider . refresh ( ) ;
0 commit comments