'use client'; import React, { useCallback, useEffect, useState } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Flow, HandleError, kratos, LogoutLink } from '@/ory'; import Link from 'next/link'; import { LoginFlow, UpdateLoginFlowBody } from '@ory/client'; import { Button } from '@/components/ui/button'; import { useRouter, useSearchParams } from 'next/navigation'; import Image from 'next/image'; import { Skeleton } from '@/components/ui/skeleton'; import { AxiosError } from 'axios'; export default function Login() { const [flow, setFlow] = useState(); const router = useRouter(); const params = useSearchParams(); const flowId = params.get('flow') ?? undefined; const aal = params.get('aal') ?? undefined; const refresh = Boolean(params.get('refresh')) ? true : undefined; const returnTo = params.get('return_to') ?? undefined; const loginChallenge = params.get('login_challenge') ?? undefined; const onLogout = LogoutLink([aal, refresh]); const getFlow = useCallback((flowId: string) => { return kratos .getLoginFlow({ id: String(flowId) }) .then(({ data }) => setFlow(data)) .catch(handleError); }, []); const handleError = useCallback((error: AxiosError) => { const handle = HandleError(getFlow, setFlow, '/flow/login', true, router); return handle(error); }, [getFlow]); const createFlow = useCallback((aal: string | undefined, refresh: boolean | undefined, returnTo: string | undefined, loginChallenge: string | undefined) => { kratos .createBrowserLoginFlow({ aal, refresh, returnTo, loginChallenge }) .then(({ data }) => { setFlow(data); router.push(`?flow=${data.id}`); }) .catch(handleError); }, [handleError]); const updateFlow = async (body: UpdateLoginFlowBody) => { kratos .updateLoginFlow({ flow: String(flow?.id), updateLoginFlowBody: body, }) .then(() => { if (flow?.return_to) { window.location.href = flow?.return_to; return; } router.push('/'); }) .catch(handleError); }; useEffect(() => { if (flow) { return; } if (flowId) { getFlow(flowId).then(); return; } createFlow(aal, refresh, returnTo, loginChallenge); }, [flowId, router, aal, refresh, returnTo, createFlow, loginChallenge, getFlow]); return ( Markus Thielker Intranet { flow ?
{ (() => { if (flow?.refresh) { return 'Confirm Action'; } else if (flow?.requested_aal === 'aal2') { return 'Two-Factor Authentication'; } return 'Welcome'; })()} Log in to the Intranet to access all locally hosted applications.
:
}
{ flow ? : (
) }
{ flow?.requested_aal === 'aal2' || flow?.requested_aal === 'aal3' || flow?.refresh ? ( ) : (
{ flow ? : } { flow ? : }
) }
); }