fix: guest and admin logic
This commit is contained in:
+83
-9
@@ -7,6 +7,7 @@ import { MyPhotosPage } from './pages/MyPhotosPage';
|
||||
import { MyNotePage } from './pages/MyNotePage';
|
||||
import { JoinTourPage } from './pages/JoinTourPage';
|
||||
import { MemberDashboard } from './pages/MemberDashboard';
|
||||
import { AdminDashboard } from './pages/AdminDashboard';
|
||||
import { ShareJourneyPage } from './pages/ShareJourneyPage';
|
||||
import { ConfirmProvider } from './hooks/useConfirm';
|
||||
import { NotificationProvider } from './hooks/useNotification';
|
||||
@@ -20,7 +21,7 @@ function App() {
|
||||
|
||||
const [user, setUser] = useState<any>(null);
|
||||
const [shareJourneyToken, setShareJourneyToken] = useState<string | null>(journeyTokenVal);
|
||||
const [currentPage, setCurrentPage] = useState<'landing' | 'dashboard' | 'explore' | 'tourDetail' | 'signup' | 'myPhotos' | 'notes' | 'joinTour' | 'shareJourney'>(
|
||||
const [currentPage, setCurrentPage] = useState<'landing' | 'dashboard' | 'admin' | 'explore' | 'tourDetail' | 'signup' | 'myPhotos' | 'notes' | 'joinTour' | 'shareJourney'>(
|
||||
journeyTokenVal ? 'shareJourney' : (viewTourId ? 'tourDetail' : (window.location.pathname === '/join-tour' || params.has('token') ? 'joinTour' : 'landing'))
|
||||
);
|
||||
const [currentTourId, setCurrentTourId] = useState<string | null>(viewTourId);
|
||||
@@ -34,9 +35,20 @@ function App() {
|
||||
const pathParts = window.location.pathname.split('/');
|
||||
const journeyToken = pathParts[1] === 'journey' && pathParts[2] ? pathParts[2] : null;
|
||||
|
||||
// Check if user just finished uploading from a public tour
|
||||
const fromPublicUpload = localStorage.getItem('fromPublicUpload');
|
||||
if (fromPublicUpload) {
|
||||
localStorage.removeItem('fromPublicUpload');
|
||||
setCurrentPage('landing');
|
||||
return;
|
||||
}
|
||||
|
||||
// Khôi phục thông tin đăng nhập nếu có
|
||||
const token = localStorage.getItem('token');
|
||||
const guestToken = localStorage.getItem('guest_token');
|
||||
const storedUser = localStorage.getItem('user');
|
||||
const storedGuestUser = localStorage.getItem('guest_user');
|
||||
|
||||
let loggedInUser = null;
|
||||
if (token && storedUser) {
|
||||
try {
|
||||
@@ -56,8 +68,18 @@ function App() {
|
||||
} else if (viewTourId) {
|
||||
setCurrentPage('tourDetail');
|
||||
} else {
|
||||
if (loggedInUser) {
|
||||
setCurrentPage('dashboard');
|
||||
// Check if only guest token exists (no real login)
|
||||
const isGuestOnly = guestToken && !token;
|
||||
if (isGuestOnly) {
|
||||
// Guest user trying to access dashboard - redirect to landing
|
||||
setCurrentPage('landing');
|
||||
} else if (loggedInUser) {
|
||||
// Real authenticated user
|
||||
if (loggedInUser.isAdmin) {
|
||||
setCurrentPage('admin');
|
||||
} else {
|
||||
setCurrentPage('dashboard');
|
||||
}
|
||||
} else {
|
||||
setCurrentPage('landing');
|
||||
}
|
||||
@@ -71,8 +93,18 @@ function App() {
|
||||
const pendingInviteToken = localStorage.getItem('pendingInviteToken');
|
||||
if (pendingInviteToken) {
|
||||
setCurrentPage('joinTour');
|
||||
} else if (loggedInUser.isAdmin) {
|
||||
// Nếu là admin, chuyển đến admin dashboard
|
||||
setCurrentPage('admin');
|
||||
} else {
|
||||
setCurrentPage('dashboard');
|
||||
// Only set to dashboard if this is a real user (has token), not a guest
|
||||
const token = localStorage.getItem('token');
|
||||
const guestToken = localStorage.getItem('guest_token');
|
||||
if (token && !guestToken) {
|
||||
setCurrentPage('dashboard');
|
||||
} else {
|
||||
setCurrentPage('landing');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -91,12 +123,22 @@ function App() {
|
||||
};
|
||||
|
||||
const handleBackFromTourDetail = () => {
|
||||
// Check if this was a public view BEFORE clearing the flag
|
||||
const wasPublicView = isPublicTourView;
|
||||
|
||||
setCurrentTourId(null);
|
||||
setIsPublicTourView(false);
|
||||
// Quay về trang cũ nếu đã đăng nhập, ngược lại quay về Landing
|
||||
if (user) {
|
||||
|
||||
// If user was viewing a public tour, redirect to index/landing page
|
||||
// Otherwise redirect based on authentication and previous page
|
||||
if (wasPublicView) {
|
||||
// Public tour view - always redirect to index/landing
|
||||
setCurrentPage('landing');
|
||||
} else if (user) {
|
||||
// Authenticated user viewing their own tour - go back to previous page
|
||||
setCurrentPage(previousPage);
|
||||
} else {
|
||||
// Not authenticated and not public view - go to landing
|
||||
setCurrentPage('landing');
|
||||
}
|
||||
};
|
||||
@@ -137,7 +179,26 @@ function App() {
|
||||
};
|
||||
|
||||
const handleBackFromExplore = () => {
|
||||
if (user) {
|
||||
// Only allow real users (with token, not guest_token)
|
||||
const token = localStorage.getItem('token');
|
||||
const guestToken = localStorage.getItem('guest_token');
|
||||
const isRealUser = token && !guestToken;
|
||||
|
||||
if (user && isRealUser) {
|
||||
setCurrentPage('dashboard');
|
||||
} else {
|
||||
setCurrentPage('landing');
|
||||
}
|
||||
};
|
||||
|
||||
const handleGoToDashboard = () => {
|
||||
// Only allow real users (with token, not guest_token)
|
||||
const token = localStorage.getItem('token');
|
||||
const guestToken = localStorage.getItem('guest_token');
|
||||
const isRealUser = token && !guestToken;
|
||||
|
||||
if (user && isRealUser) {
|
||||
setPreviousPage('explore');
|
||||
setCurrentPage('dashboard');
|
||||
} else {
|
||||
setCurrentPage('landing');
|
||||
@@ -147,14 +208,26 @@ function App() {
|
||||
const handleGoToHome = () => {
|
||||
window.history.pushState({}, '', '/');
|
||||
setShareJourneyToken(null);
|
||||
const loggedIn = !!localStorage.getItem('token');
|
||||
setCurrentPage(loggedIn ? 'dashboard' : 'landing');
|
||||
const token = localStorage.getItem('token');
|
||||
const guestToken = localStorage.getItem('guest_token');
|
||||
// Only real authenticated users can access dashboard, not guests
|
||||
const isRealUser = token && !guestToken;
|
||||
setCurrentPage(isRealUser ? 'dashboard' : 'landing');
|
||||
};
|
||||
|
||||
return (
|
||||
<ConfirmProvider>
|
||||
<NotificationProvider>
|
||||
{(() => {
|
||||
if (currentPage === 'admin') {
|
||||
return (
|
||||
<AdminDashboard
|
||||
user={user}
|
||||
onNavigate={setCurrentPage}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (currentPage === 'dashboard') {
|
||||
return (
|
||||
<MemberDashboard
|
||||
@@ -191,6 +264,7 @@ function App() {
|
||||
onViewTour={handleViewTour}
|
||||
onOpenMyPhotos={() => setCurrentPage('myPhotos')}
|
||||
onLoginSuccess={handleLoginSuccess}
|
||||
onGoToDashboard={handleGoToDashboard}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user