Moved v3 code from NginxProxyManager/nginx-proxy-manager-3 to NginxProxyManager/nginx-proxy-manager
This commit is contained in:
79
frontend/src/context/AuthContext.tsx
Normal file
79
frontend/src/context/AuthContext.tsx
Normal file
@@ -0,0 +1,79 @@
|
||||
import { ReactNode, createContext, useContext, useState } from "react";
|
||||
|
||||
import { getToken, refreshToken, TokenResponse } from "api/npm";
|
||||
import AuthStore from "modules/AuthStore";
|
||||
import { useQueryClient } from "react-query";
|
||||
import { useIntervalWhen } from "rooks";
|
||||
|
||||
// Context
|
||||
export interface AuthContextType {
|
||||
authenticated: boolean;
|
||||
login: (username: string, password: string) => Promise<void>;
|
||||
logout: () => void;
|
||||
token?: string;
|
||||
}
|
||||
|
||||
const initalValue = null;
|
||||
const AuthContext = createContext<AuthContextType | null>(initalValue);
|
||||
|
||||
// Provider
|
||||
interface Props {
|
||||
children?: ReactNode;
|
||||
tokenRefreshInterval?: number;
|
||||
}
|
||||
function AuthProvider({
|
||||
children,
|
||||
tokenRefreshInterval = 5 * 60 * 1000,
|
||||
}: Props) {
|
||||
const queryClient = useQueryClient();
|
||||
const [authenticated, setAuthenticated] = useState(
|
||||
AuthStore.hasActiveToken(),
|
||||
);
|
||||
|
||||
const handleTokenUpdate = (response: TokenResponse) => {
|
||||
AuthStore.set(response);
|
||||
setAuthenticated(true);
|
||||
};
|
||||
|
||||
const login = async (identity: string, secret: string) => {
|
||||
const type = "password";
|
||||
const response = await getToken({ payload: { type, identity, secret } });
|
||||
handleTokenUpdate(response);
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
AuthStore.clear();
|
||||
setAuthenticated(false);
|
||||
queryClient.invalidateQueries("user");
|
||||
};
|
||||
|
||||
const refresh = async () => {
|
||||
const response = await refreshToken();
|
||||
handleTokenUpdate(response);
|
||||
};
|
||||
|
||||
useIntervalWhen(
|
||||
() => {
|
||||
if (authenticated) {
|
||||
refresh();
|
||||
}
|
||||
},
|
||||
tokenRefreshInterval,
|
||||
true,
|
||||
);
|
||||
|
||||
const value = { authenticated, login, logout };
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
function useAuthState() {
|
||||
const context = useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error(`useAuthState must be used within a AuthProvider`);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
export { AuthProvider, useAuthState };
|
||||
export default AuthContext;
|
41
frontend/src/context/LocaleContext.tsx
Normal file
41
frontend/src/context/LocaleContext.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import { createContext, ReactNode, useContext, useState } from "react";
|
||||
|
||||
import { getLocale } from "locale";
|
||||
|
||||
// Context
|
||||
export interface LocaleContextType {
|
||||
setLocale: (locale: string) => void;
|
||||
locale?: string;
|
||||
}
|
||||
|
||||
const initalValue = null;
|
||||
const LocaleContext = createContext<LocaleContextType | null>(initalValue);
|
||||
|
||||
// Provider
|
||||
interface Props {
|
||||
children?: ReactNode;
|
||||
}
|
||||
function LocaleProvider({ children }: Props) {
|
||||
const [locale, setLocaleValue] = useState(getLocale());
|
||||
|
||||
const setLocale = async (locale: string) => {
|
||||
setLocaleValue(locale);
|
||||
};
|
||||
|
||||
const value = { locale, setLocale };
|
||||
|
||||
return (
|
||||
<LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function useLocaleState() {
|
||||
const context = useContext(LocaleContext);
|
||||
if (!context) {
|
||||
throw new Error(`useLocaleState must be used within a LocaleProvider`);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
export { LocaleProvider, useLocaleState };
|
||||
export default LocaleContext;
|
2
frontend/src/context/index.ts
Normal file
2
frontend/src/context/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./AuthContext";
|
||||
export * from "./LocaleContext";
|
Reference in New Issue
Block a user