Junhui Chen d31b587330 feat: 自动上传组件
feat: 自动上传组件
Co-authored-by: Junhui Chen <chenjunhui@fairclip.cn>
Co-committed-by: Junhui Chen <chenjunhui@fairclip.cn>
2025-07-17 15:55:27 +08:00

40 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useRouter } from 'expo-router';
import * as SecureStore from 'expo-secure-store';
import { Platform } from 'react-native';
import { API_ENDPOINT } from './server-api-util';
export async function identityCheck(token: string) {
const res = await fetch(`${API_ENDPOINT}/v1/iam/identity-check`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`
},
});
const data = await res.json();
return data.code == 0;
}
/**
* 检查登录态,未登录自动跳转到 /login已登录可执行回调。
* @param onAuthed 已登录时的回调(可选)
*/
export async function checkAuthStatus(router: ReturnType<typeof useRouter>, onAuthed?: () => Promise<void> | void) {
let token: string | null = '';
if (Platform.OS === 'web') {
token = localStorage.getItem('token') || '';
} else {
token = await SecureStore.getItemAsync('token') || '';
}
const loggedIn = !!token && await identityCheck(token);
if (!loggedIn) {
router.replace('/login');
return false;
}
if (onAuthed) {
await onAuthed();
}
return true;
}