Skip to content

Commit

Permalink
fix: command not found
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Dec 10, 2023
1 parent cd9663a commit 06d20fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/auth/auth-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ export namespace AuthManager {
export async function acquireToken() {
const session = await ensureSession({ createIfNone: false })

if (session === undefined) throw Error('未授权')
if (isAuthSessionExpired(session)) throw Error('授权已过期')
if (session === undefined) Alert.throwWithWarn('未授权')
if (isAuthSessionExpired(session)) {
void Alert.warn('授权已过期,请重新登录')
await logout()
}

return session.accessToken
return session?.accessToken
}

export async function updateAuthStatus() {
Expand Down
5 changes: 5 additions & 0 deletions src/infra/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ export namespace Alert {
file = trimExt ? path.basename(file, path.extname(file)) : file
void Alert.warn(`本地文件 ${file} 未关联博客园博文`)
}

export function throwWithWarn(message: string): never {
void warn(message)
throw Error(message)
}
}
8 changes: 4 additions & 4 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export namespace UserService {
return userInfo?.blogApp != null
}

export async function getUserInfoWithToken(token: string, isPat: boolean): Promise<UserInfo> {
export async function getUserInfoWithToken(token: string, isPat: boolean): Promise<UserInfo | null> {
const url = `${ExtConst.ApiBase.OPENAPI}/users/v2`

const headers = new Headers()
Expand All @@ -35,9 +35,9 @@ export namespace UserService {
if (!req.ok) {
const message = `${req.status} ${req.statusText}`
if (req.status === 401) {
void Alert.err(
'获取用户信息失败,可能是个人访问令牌(PAT)失效或者不存在,重新[创建PAT](https://account.cnblogs.com/settings/tokens)'
)
void Alert.warn('获取用户信息失败,请重新登录')
await AuthManager.logout()
return null
} else {
void Alert.err(`获取用户信息失败,错误详情: ${message}`)
}
Expand Down

0 comments on commit 06d20fc

Please sign in to comment.