File tree 3 files changed +16
-8
lines changed
3 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -75,10 +75,13 @@ export namespace AuthManager {
75
75
export async function acquireToken ( ) {
76
76
const session = await ensureSession ( { createIfNone : false } )
77
77
78
- if ( session === undefined ) throw Error ( '未授权' )
79
- if ( isAuthSessionExpired ( session ) ) throw Error ( '授权已过期' )
78
+ if ( session === undefined ) Alert . throwWithWarn ( '未授权' )
79
+ if ( isAuthSessionExpired ( session ) ) {
80
+ void Alert . warn ( '授权已过期,请重新登录' )
81
+ await logout ( )
82
+ }
80
83
81
- return session . accessToken
84
+ return session ? .accessToken
82
85
}
83
86
84
87
export async function updateAuthStatus ( ) {
Original file line number Diff line number Diff line change @@ -31,4 +31,9 @@ export namespace Alert {
31
31
file = trimExt ? path . basename ( file , path . extname ( file ) ) : file
32
32
void Alert . warn ( `本地文件 ${ file } 未关联博客园博文` )
33
33
}
34
+
35
+ export function throwWithWarn ( message : string ) : never {
36
+ void warn ( message )
37
+ throw Error ( message )
38
+ }
34
39
}
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import { UserInfo } from '@/model/user-info'
4
4
import { ExtConst } from '@/ctx/ext-const'
5
5
6
6
export namespace UserService {
7
- export async function getUserInfo ( ) : Promise < UserInfo > {
7
+ export async function getUserInfo ( ) : Promise < UserInfo | null > {
8
8
const token = await AuthManager . acquireToken ( )
9
9
// TODO: need better solution
10
10
const isPatToken = token . length === 64
@@ -16,7 +16,7 @@ export namespace UserService {
16
16
return userInfo ?. blogApp != null
17
17
}
18
18
19
- export async function getUserInfoWithToken ( token : string , isPat : boolean ) : Promise < UserInfo > {
19
+ export async function getUserInfoWithToken ( token : string , isPat : boolean ) : Promise < UserInfo | null > {
20
20
const url = `${ ExtConst . ApiBase . OPENAPI } /users/v2`
21
21
22
22
const headers = new Headers ( )
@@ -35,9 +35,9 @@ export namespace UserService {
35
35
if ( ! req . ok ) {
36
36
const message = `${ req . status } ${ req . statusText } `
37
37
if ( req . status === 401 ) {
38
- void Alert . err (
39
- '获取用户信息失败,可能是个人访问令牌(PAT)失效或者不存在,重新[创建PAT](https://account.cnblogs.com/settings/tokens)'
40
- )
38
+ void Alert . warn ( '获取用户信息失败,请重新登录' )
39
+ await AuthManager . logout ( )
40
+ return null
41
41
} else {
42
42
void Alert . err ( `获取用户信息失败,错误详情: ${ message } ` )
43
43
}
You can’t perform that action at this time.
0 commit comments