feature: localization's first implmentation done. all frontend hardcoded text is now localized.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
'use client';
|
||||
|
||||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useRouter } from '@/i18n/navigation';
|
||||
import { authApi } from '@/lib/api/auth';
|
||||
import { User, Organization } from '@/types/organization';
|
||||
import { isAppLocale } from '@/i18n/routing';
|
||||
import { isAppLocale, getLocaleFromPathname } from '@/i18n/routing';
|
||||
|
||||
interface AuthContextType {
|
||||
user: User | null;
|
||||
@@ -37,6 +38,7 @@ interface AuthContextType {
|
||||
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const t = useTranslations('auth');
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [organizations, setOrganizations] = useState<Organization[]>([]);
|
||||
const [currentOrganization, setCurrentOrganization] = useState<Organization | null>(null);
|
||||
@@ -121,6 +123,23 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
void checkAuth();
|
||||
}, [checkAuth]);
|
||||
|
||||
const applyUrlLocaleToUser = useCallback(async (user: User): Promise<User> => {
|
||||
if (typeof window === 'undefined') return user;
|
||||
|
||||
const urlLocale = getLocaleFromPathname(window.location.pathname);
|
||||
if (!isAppLocale(urlLocale) || user.language === urlLocale) {
|
||||
return user;
|
||||
}
|
||||
|
||||
try {
|
||||
await authApi.updateLanguage(urlLocale);
|
||||
} catch {
|
||||
/* keep URL locale in client state even if persistence fails */
|
||||
}
|
||||
|
||||
return { ...user, language: urlLocale };
|
||||
}, []);
|
||||
|
||||
// ✅ REGISTER
|
||||
const registerTrial = useCallback(async (
|
||||
email: string,
|
||||
@@ -143,7 +162,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
organizationType,
|
||||
});
|
||||
|
||||
setUser(response.data.user);
|
||||
const userData = await applyUrlLocaleToUser(response.data.user);
|
||||
setUser(userData);
|
||||
setOrganizations(response.data.organizations);
|
||||
|
||||
const orgs = response.data.organizations;
|
||||
@@ -160,12 +180,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Registration failed');
|
||||
setError(err.message || t('errorRegistrationFailed'));
|
||||
throw err;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [router]);
|
||||
}, [applyUrlLocaleToUser, router, t]);
|
||||
|
||||
// ✅ LOGIN
|
||||
const login = useCallback(async (email: string, password: string) => {
|
||||
@@ -175,7 +195,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const response = await authApi.login({ email, password });
|
||||
|
||||
setUser(response.data.user);
|
||||
const userData = await applyUrlLocaleToUser(response.data.user);
|
||||
setUser(userData);
|
||||
setOrganizations(response.data.organizations);
|
||||
|
||||
const orgs = response.data.organizations;
|
||||
@@ -191,17 +212,17 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Login failed');
|
||||
setError(err.message || t('errorLoginFailed'));
|
||||
throw err;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [router]);
|
||||
}, [applyUrlLocaleToUser, router, t]);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
try {
|
||||
// Important: clear auth cookies/session on the server first,
|
||||
// otherwise middleware may still treat the user as authenticated.
|
||||
// otherwise proxy may still treat the user as authenticated.
|
||||
await authApi.logout();
|
||||
} catch (err) {
|
||||
console.error('Logout API failed:', err);
|
||||
@@ -272,12 +293,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
return createResponse.data.organization.id as string;
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Failed to create organization');
|
||||
setError(err.message || t('errorCreateOrganization'));
|
||||
throw err;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [normalizeProfilePayload]);
|
||||
}, [normalizeProfilePayload, t]);
|
||||
|
||||
const clearError = useCallback(() => setError(null), []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user