@@ -12,14 +12,25 @@ import { AlertService } from '@/services/alert.service'
12
12
13
13
const isAuthorizedStorageKey = 'isAuthorized'
14
14
15
+ export const ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED = 'unauthenticated'
16
+ export const ACQUIRE_TOKEN_REJECT_EXPIRED = 'expired'
17
+
15
18
class AccountManager extends vscode . Disposable {
16
- // eslint-disable-next-line @typescript-eslint/naming-convention
17
- static readonly ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED = 'unauthenticated'
18
- // eslint-disable-next-line @typescript-eslint/naming-convention
19
- static readonly ACQUIRE_TOKEN_REJECT_EXPIRED = 'expired'
19
+ private readonly _authProvider = AuthProvider . instance
20
+ private readonly _disposable = Disposable . from (
21
+ this . _authProvider . onDidChangeSessions ( async ( { added } ) => {
22
+ this . _session = null
23
+ if ( added != null && added . length > 0 ) await this . ensureSession ( )
24
+
25
+ await this . updateAuthorizationStatus ( )
26
+
27
+ accountViewDataProvider . fireTreeDataChangedEvent ( )
28
+ postsDataProvider . fireTreeDataChangedEvent ( undefined )
29
+ postCategoriesDataProvider . fireTreeDataChangedEvent ( )
20
30
21
- private readonly _authProvider : AuthProvider
22
- private readonly _disposable : vscode . Disposable
31
+ BlogExportProvider . optionalInstance ?. refreshRecords ( { force : false , clearCache : true } ) . catch ( console . warn )
32
+ } )
33
+ )
23
34
24
35
private _oauthClient : OauthApi | null = null
25
36
private _session : AuthSession | null = null
@@ -28,26 +39,6 @@ class AccountManager extends vscode.Disposable {
28
39
super ( ( ) => {
29
40
this . _disposable . dispose ( )
30
41
} )
31
-
32
- this . _authProvider = AuthProvider . instance
33
-
34
- this . _disposable = Disposable . from (
35
- this . _authProvider ,
36
- this . _authProvider . onDidChangeSessions ( async ( { added } ) => {
37
- this . _session = null
38
- if ( added != null && added . length > 0 ) await this . ensureSession ( )
39
-
40
- await this . updateAuthorizationStatus ( )
41
-
42
- accountViewDataProvider . fireTreeDataChangedEvent ( )
43
- postsDataProvider . fireTreeDataChangedEvent ( undefined )
44
- postCategoriesDataProvider . fireTreeDataChangedEvent ( )
45
-
46
- BlogExportProvider . optionalInstance
47
- ?. refreshRecords ( { force : false , clearCache : true } )
48
- . catch ( console . warn )
49
- } )
50
- )
51
42
}
52
43
53
44
get isAuthorized ( ) {
@@ -59,7 +50,8 @@ class AccountManager extends vscode.Disposable {
59
50
}
60
51
61
52
protected get oauthClient ( ) {
62
- return ( this . _oauthClient ??= new OauthApi ( ) )
53
+ this . _oauthClient ??= new OauthApi ( )
54
+ return this . _oauthClient
63
55
}
64
56
65
57
/**
@@ -69,11 +61,12 @@ class AccountManager extends vscode.Disposable {
69
61
*/
70
62
async acquireToken ( ) : Promise < string > {
71
63
const session = await this . ensureSession ( { createIfNone : false } )
72
- return session == null
73
- ? Promise . reject ( AccountManager . ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED )
74
- : session . isExpired
75
- ? Promise . reject ( AccountManager . ACQUIRE_TOKEN_REJECT_EXPIRED )
76
- : session . accessToken
64
+
65
+ if ( session == null ) return Promise . reject ( ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED )
66
+
67
+ if ( session . isExpired ) return Promise . reject ( ACQUIRE_TOKEN_REJECT_EXPIRED )
68
+
69
+ return session . accessToken
77
70
}
78
71
79
72
async login ( ) {
@@ -84,7 +77,7 @@ class AccountManager extends vscode.Disposable {
84
77
if ( ! this . isAuthorized ) return
85
78
86
79
const session = await authentication . getSession ( AuthProvider . providerId , [ ] )
87
- if ( session ) await this . _authProvider . removeSession ( session . id )
80
+ if ( session !== undefined ) await this . _authProvider . removeSession ( session . id )
88
81
89
82
// For old version compatibility, **never** remove this line
90
83
await globalContext . storage . update ( 'user' , undefined )
@@ -118,9 +111,9 @@ class AccountManager extends vscode.Disposable {
118
111
}
119
112
}
120
113
121
- private async ensureSession ( opt ?: AuthenticationGetSessionOptions ) : Promise < AuthSession > {
114
+ private async ensureSession ( opt ?: AuthenticationGetSessionOptions ) : Promise < AuthSession | null > {
122
115
const session = await authentication . getSession ( this . _authProvider . providerId , [ ] , opt ) . then (
123
- session => ( session ? AuthSession . parse ( session ) : null ) ,
116
+ session => ( session ? AuthSession . from ( session ) : null ) ,
124
117
e => AlertService . err ( `创建/获取 session 失败: ${ e } ` )
125
118
)
126
119
@@ -131,7 +124,7 @@ class AccountManager extends vscode.Disposable {
131
124
this . _session = session
132
125
}
133
126
134
- return this . _session ?? AuthSession . parse ( )
127
+ return this . _session
135
128
}
136
129
}
137
130
0 commit comments