feat: refresh token
Some checks failed
Dev Deploy / Explore-Gitea-Actions (push) Failing after 1m16s
Some checks failed
Dev Deploy / Explore-Gitea-Actions (push) Failing after 1m16s
This commit is contained in:
parent
ddf37c26e9
commit
291df75086
@ -16,7 +16,7 @@ export default function HomeScreen() {
|
||||
setIsLoading(true);
|
||||
checkAuthStatus(router, () => {
|
||||
router.replace('/ask')
|
||||
}).then(() => {
|
||||
}, false).then(() => {
|
||||
setIsLoading(false);
|
||||
});
|
||||
}, []);
|
||||
|
||||
@ -109,8 +109,6 @@ export default function OwnerPage() {
|
||||
|
||||
// 当用户选择发生变化时,重新获取排名
|
||||
useEffect(() => {
|
||||
console.log('selectedLocation', selectedLocation);
|
||||
console.log('selectedClassify', selectedClassify);
|
||||
if (selectedLocation?.length > 0 && selectedClassify?.length > 0) {
|
||||
getRanking();
|
||||
}
|
||||
@ -151,8 +149,6 @@ export default function OwnerPage() {
|
||||
return item.name === JSON.parse(location || "").city
|
||||
}) || [], JSON.parse(location || "").district);
|
||||
if (location) {
|
||||
console.log("xuhuiElement", xuhuiElement);
|
||||
|
||||
setSelectedLocation([xuhuiElement]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ const Ranking = ({ data }: { data: TitleRankings[] }) => {
|
||||
data={data}
|
||||
showsVerticalScrollIndicator={false}
|
||||
contentContainerStyle={{ width: "100%" }}
|
||||
keyExtractor={(item) => item.display_name}
|
||||
keyExtractor={(item, index) => `${item.display_name}-${index}`}
|
||||
renderItem={({ item }) => (
|
||||
<View style={styles.item}>
|
||||
<ThemedText style={styles.rank}>No.{item.ranking}</ThemedText>
|
||||
|
||||
12
lib/auth.ts
12
lib/auth.ts
@ -1,7 +1,7 @@
|
||||
import { useRouter } from 'expo-router';
|
||||
import * as SecureStore from 'expo-secure-store';
|
||||
import { Platform } from 'react-native';
|
||||
import { API_ENDPOINT } from './server-api-util';
|
||||
import { API_ENDPOINT, refreshAuthToken } from './server-api-util';
|
||||
|
||||
|
||||
export async function identityCheck(token: string) {
|
||||
@ -12,14 +12,18 @@ export async function identityCheck(token: string) {
|
||||
},
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.code != 0) {
|
||||
await refreshAuthToken("Token expired");
|
||||
}
|
||||
return data.code == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查登录态,未登录自动跳转到 /login,已登录可执行回调。
|
||||
* @param onAuthed 已登录时的回调(可选)
|
||||
* @param login 是否跳转登录页(可选)
|
||||
*/
|
||||
export async function checkAuthStatus(router: ReturnType<typeof useRouter>, onAuthed?: () => Promise<void> | void) {
|
||||
export async function checkAuthStatus(router: ReturnType<typeof useRouter>, onAuthed?: () => Promise<void> | void, login: boolean = true) {
|
||||
let token: string | null = '';
|
||||
if (Platform.OS === 'web') {
|
||||
token = localStorage.getItem('token') || '';
|
||||
@ -28,11 +32,11 @@ export async function checkAuthStatus(router: ReturnType<typeof useRouter>, onAu
|
||||
}
|
||||
|
||||
const loggedIn = !!token && await identityCheck(token);
|
||||
if (!loggedIn) {
|
||||
if (!loggedIn && login) {
|
||||
router.replace('/login');
|
||||
return false;
|
||||
}
|
||||
if (onAuthed) {
|
||||
if (onAuthed && loggedIn) {
|
||||
await onAuthed();
|
||||
}
|
||||
return true;
|
||||
|
||||
@ -64,15 +64,14 @@ export const refreshAuthToken = async<T>(message: string | null): Promise<User>
|
||||
cookie = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') || "")?.refresh_token || "" : "";
|
||||
userId = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') || "")?.user_id || "" : "";
|
||||
} else {
|
||||
await SecureStore.getItemAsync('user').then((user: User) => {
|
||||
cookie = user?.refresh_token || "";
|
||||
userId = user?.user_id || "";
|
||||
await SecureStore.getItemAsync('user').then((user: string | null) => {
|
||||
cookie = JSON.parse(user || "")?.refresh_token || "";
|
||||
userId = JSON.parse(user || "")?.user_id || "";
|
||||
})
|
||||
}
|
||||
|
||||
// 退出刷新会重新填充数据
|
||||
let response;
|
||||
|
||||
response = await fetch(`${API_ENDPOINT}/v1/iam/access-token-refresh`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
@ -97,6 +96,13 @@ export const refreshAuthToken = async<T>(message: string | null): Promise<User>
|
||||
user: userData,
|
||||
token: userData.access_token
|
||||
}));
|
||||
if (Platform.OS === 'web') {
|
||||
localStorage.setItem('user', JSON.stringify(userData));
|
||||
localStorage.setItem('token', userData.access_token);
|
||||
} else {
|
||||
SecureStore.setItemAsync('user', JSON.stringify(userData));
|
||||
SecureStore.setItemAsync('token', userData.access_token);
|
||||
}
|
||||
}
|
||||
|
||||
return userData;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user