forked from akai/readest
auth: avoid infinite loop of redirect and auth after token is expired (#1758)
This commit is contained in:
@@ -24,6 +24,7 @@ import { start, cancel, onUrl, onInvalidUrl } from '@fabianlars/tauri-plugin-oau
|
||||
import { openUrl } from '@tauri-apps/plugin-opener';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { handleAuthCallback } from '@/helpers/auth';
|
||||
import { getUserPlan } from '@/utils/access';
|
||||
import { getAppleIdAuth, Scope } from './utils/appleIdAuth';
|
||||
import { authWithCustomTab, authWithSafari } from './utils/nativeAuth';
|
||||
import WindowButtons from '@/components/WindowButtons';
|
||||
@@ -170,8 +171,11 @@ export default function AuthPage() {
|
||||
const accessToken = params.get('access_token');
|
||||
const refreshToken = params.get('refresh_token');
|
||||
const type = params.get('type');
|
||||
const next = params.get('next') ?? '/';
|
||||
if (accessToken) {
|
||||
let next = params.get('next') ?? '/';
|
||||
if (getUserPlan(accessToken) === 'free') {
|
||||
next = '/user';
|
||||
}
|
||||
handleAuthCallback({ accessToken, refreshToken, type, next, login, navigate: router.push });
|
||||
}
|
||||
}
|
||||
@@ -310,7 +314,13 @@ export default function AuthPage() {
|
||||
if (session?.access_token && session.user) {
|
||||
login(session.access_token, session.user);
|
||||
const redirectTo = new URLSearchParams(window.location.search).get('redirect');
|
||||
router.push(redirectTo ?? '/library');
|
||||
const lastRedirectAtKey = 'lastRedirectAt';
|
||||
const lastRedirectAt = parseInt(localStorage.getItem(lastRedirectAtKey) || '0', 10);
|
||||
const now = Date.now();
|
||||
localStorage.setItem(lastRedirectAtKey, now.toString());
|
||||
if (now - lastRedirectAt > 3000) {
|
||||
router.push(redirectTo ?? '/library');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user