Skip to content

Commit

Permalink
Merge pull request #275 from cnblogs/fix-command-not-found
Browse files Browse the repository at this point in the history
fix: command not found
  • Loading branch information
cnblogs-dudu authored Dec 10, 2023
2 parents 4f7d9ea + 6cd2297 commit f5b0e5e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 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)
}
}
10 changes: 5 additions & 5 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UserInfo } from '@/model/user-info'
import { ExtConst } from '@/ctx/ext-const'

export namespace UserService {
export async function getUserInfo(): Promise<UserInfo> {
export async function getUserInfo(): Promise<UserInfo | null> {
const token = await AuthManager.acquireToken()
// TODO: need better solution
const isPatToken = token.length === 64
Expand All @@ -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 f5b0e5e

Please sign in to comment.