mirror of
https://github.com/khoaliber/LetterFeed.git
synced 2026-03-04 13:20:24 +00:00
fix: auth disabled routing and UI
This commit is contained in:
@@ -6,6 +6,7 @@ import { useRouter } from "next/navigation"
|
||||
|
||||
interface AuthContextType {
|
||||
isAuthenticated: boolean
|
||||
isAuthEnabled: boolean
|
||||
login: (username: string, password: string) => Promise<void>
|
||||
logout: () => void
|
||||
isLoading: boolean
|
||||
@@ -15,6 +16,7 @@ export const AuthContext = createContext<AuthContextType | undefined>(undefined)
|
||||
|
||||
export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false)
|
||||
const [isAuthEnabled, setIsAuthEnabled] = useState(false)
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const router = useRouter()
|
||||
|
||||
@@ -23,6 +25,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const { auth_enabled } = await getAuthStatus();
|
||||
setIsAuthEnabled(auth_enabled);
|
||||
if (!auth_enabled) {
|
||||
setIsAuthenticated(true);
|
||||
} else {
|
||||
@@ -67,7 +70,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isAuthenticated, login, logout, isLoading }}>
|
||||
<AuthContext.Provider value={{ isAuthenticated, isAuthEnabled, login, logout, isLoading }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
|
||||
@@ -28,21 +28,27 @@ describe("AuthContext", () => {
|
||||
consoleError.mockRestore()
|
||||
})
|
||||
|
||||
it("authenticates if auth is not enabled", async () => {
|
||||
it("authenticates and sets auth enabled to false if auth is not enabled on server", async () => {
|
||||
mockedApi.getAuthStatus.mockResolvedValue({ auth_enabled: false })
|
||||
render(
|
||||
<AuthProvider>
|
||||
<AuthContext.Consumer>
|
||||
{(value) => (
|
||||
<span>
|
||||
Is Authenticated: {value?.isAuthenticated.toString()}
|
||||
</span>
|
||||
<div>
|
||||
<span>
|
||||
Is Authenticated: {value?.isAuthenticated.toString()}
|
||||
</span>
|
||||
<span>
|
||||
Is Auth Enabled: {value?.isAuthEnabled.toString()}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</AuthContext.Consumer>
|
||||
</AuthProvider>
|
||||
)
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("Is Authenticated: true")).toBeInTheDocument()
|
||||
expect(screen.getByText("Is Auth Enabled: false")).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -54,15 +60,21 @@ describe("AuthContext", () => {
|
||||
<AuthProvider>
|
||||
<AuthContext.Consumer>
|
||||
{(value) => (
|
||||
<span>
|
||||
Is Authenticated: {value?.isAuthenticated.toString()}
|
||||
</span>
|
||||
<div>
|
||||
<span>
|
||||
Is Authenticated: {value?.isAuthenticated.toString()}
|
||||
</span>
|
||||
<span>
|
||||
Is Auth Enabled: {value?.isAuthEnabled.toString()}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</AuthContext.Consumer>
|
||||
</AuthProvider>
|
||||
)
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("Is Authenticated: true")).toBeInTheDocument()
|
||||
expect(screen.getByText("Is Auth Enabled: true")).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user