'use client'; import React, { useCallback, useEffect, useState } from 'react'; import { SettingsFlow, UpdateSettingsFlowBody } from '@ory/client'; import { kratos } from '@/ory/sdk/kratos'; import { useRouter, useSearchParams } from 'next/navigation'; import { toast } from 'sonner'; import { AxiosError } from 'axios'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Flow, HandleError, LogoutLink } from '@/ory'; import { ThemeToggle } from '@/components/themeToggle'; import { Button } from '@/components/ui/button'; import { LogOut } from 'lucide-react'; export default function Home() { const [flow, setFlow] = useState(); const router = useRouter(); const params = useSearchParams(); const returnTo = params.get('return_to') ?? undefined; const flowId = params.get('flow') ?? undefined; const onLogout = LogoutLink(); const getFlow = useCallback((flowId: string) => { return kratos .getSettingsFlow({ id: String(flowId) }) .then(({ data }) => setFlow(data)) .catch(handleError); }, []); const handleError = useCallback((error: AxiosError) => { const handle = HandleError(getFlow, setFlow, '/flow/settings', true, router); return handle(error); }, [getFlow]); const createFlow = useCallback((returnTo: string | undefined) => { kratos .createBrowserSettingsFlow({ returnTo }) .then(({ data }) => { setFlow(data); router.push(`?flow=${data.id}`); }) .catch(handleError); }, [handleError]); const updateFlow = async (body: UpdateSettingsFlowBody) => { kratos .updateSettingsFlow({ flow: String(flow?.id), updateSettingsFlowBody: body, }) .then(({ data }) => { // update flow object setFlow(data); // show toast for user feedback const message = data.ui.messages?.pop(); if (message) { toast.success(message.text); } // check if verification is needed if (data.continue_with) { for (const item of data.continue_with) { switch (item.action) { case 'show_verification_ui': router.push('/verification?flow=' + item.flow.id); return; } } } // check if custom return page was specified if (data.return_to) { window.location.href = data.return_to; return; } }) .catch(handleError); }; useEffect(() => { if (flow) { return; } if (flowId) { getFlow(flowId).then(); return; } createFlow(returnTo); }, [flowId, router, returnTo, createFlow, getFlow]); return (

Settings

{ flow?.ui.nodes.some(({ group }) => group === 'profile') && ( Password ) } { flow?.ui.nodes.some(({ group }) => group === 'password') && ( Password ) } { flow?.ui.nodes.some(({ group }) => group === 'totp') && ( MFA ) } { flow?.ui.nodes.some(({ group }) => group === 'oidc') && ( Connect Socials ) } { flow?.ui.nodes.some(({ group }) => group === 'link') && ( Connect Socials ) } { flow?.ui.nodes.some(({ group }) => group === 'webauthn') && ( Connect Socials ) } { flow?.ui.nodes.some(({ group }) => group === 'lookup_secret') && ( Recovery Codes ) }
); }