flat-rose-25983
11/17/2023, 9:46 AMchilly-king-10285
11/17/2023, 9:46 AMchilly-king-10285
11/17/2023, 9:48 AM@ory/client
) to create a login flow and render the user interface. The @ory/integrations
package can be used as a helper to filter the flow data and render only the password
method form fields. Here is an example of how to do this: javascript import { Configuration, FrontendApi, LoginFlow, UiNode, UiNodeInputAttributes, } from "@ory/client" import { filterNodesByGroups, isUiNodeInputAttributes, } from "@ory/integrations/ui" import { useEffect, useState } from "react" import { useSearchParams } from "react-router-dom" const frontend = new FrontendApi( new Configuration({ basePath: "<http://localhost:4000>", // Use your local Ory Tunnel URL baseOptions: { withCredentials: true, // we need to include cookies }, }), ) export const Login = () => { const [flow, setFlow] = useState<LoginFlow>() const [searchParams] = useSearchParams() useEffect(() => { // check if the login flow is for two factor authentication const aal2 = searchParams.get("aal2") // we can redirect the user back to the page they were on before login const returnTo = searchParams.get("return_to") frontend .createBrowserLoginFlow({ returnTo: returnTo || "/", // redirect to the root path after login // if the user has a session, refresh it refresh: true, // if the aal2 query parameter is set, we get the two factor login flow UI nodes aal: aal2 ? "aal2" : "aal1", }) .then(({ data: flow }) => { // set the flow data setFlow(flow) }) .catch((err) => { // Couldn't create login flow // handle the error }) }, []) const mapUINode = (node: UiNode, key: number) => { // other node types are also supported // if (isUiNodeTextAttributes(node.attributes)) { // if (isUiNodeImageAttributes(node.attributes)) { // if (isUiNodeAnchorAttributes(node.attributes)) { if (isUiNodeInputAttributes(node.attributes)) {
3. You can also use Ory Elements, a component library designed to make building login, registration, and account pages for Ory straightforward. The UI created with Ory Elements changes dynamically to adapt to your Ory Network configuration. You can find more information about this in the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/bring-your-own-ui/custom-ui-ory-elements). Remember to replace "<http://localhost:4000>"
with your local Ory Tunnel URL.flat-rose-25983
11/17/2023, 9:50 AMchilly-king-10285
11/17/2023, 9:50 AMchilly-king-10285
11/17/2023, 9:51 AMjavascript if (isUiNodeInputAttributes(node.attributes)) { const attrs = node.attributes as UiNodeInputAttributes const nodeType = attrs.type switch (nodeType) { case "button": case "submit": return ( <button type={attrs.type as "submit" | "reset" | "button" | undefined} name={attrs.name} value={attrs.value} key={key} /> ) default: return ( <input name={attrs.name} type={attrs.type} autoComplete={ attrs.autocomplete || attrs.name === "identifier" ? "username" : "" } /> ) } }
In this example, the isUiNodeInputAttributes
function is used to check if the node attributes are of type UiNodeInputAttributes
. If they are, it creates a new attrs
constant and assigns the node attributes to it. It then checks the type of the node and creates a button or input element accordingly. Please note that this is a simplified example and you may need to add additional logic and styling to suit your needs. You can find more detailed examples and information in the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/bring-your-own-ui/custom-ui-basic-integration).