import React from 'react'
import { useAuth } from "react-oidc-context"

function PrivateRoute({ children }) {
    const auth = useAuth()


    if (auth.isLoading) {
        return (
            <div>
                <p>Keycloak is loading</p>
                <p>or running authorization code flow with PKCE</p>
            </div>
        )
    }

    if (auth.error) {
        return (
            <div>
                <p>Oops ...</p>
                <p>{auth.error.message}</p>
            </div>
        )
    }

    if (!auth.isAuthenticated) {
        auth.signinRedirect()
        return null
    }

    return children
}

export default PrivateRoute
