Skip to content

Commit

Permalink
chore: the hanko-elements can be used in nextjs without dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoern-m committed Aug 2, 2024
1 parent b0fb3e1 commit fd93beb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
8 changes: 5 additions & 3 deletions frontend/examples/nextjs/components/HankoAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hanko, register } from "@teamhanko/hanko-elements";
import { useCallback, useEffect, useMemo } from "react";
import { useCallback, useEffect, useState } from "react";
import { useRouter } from "next/router";

const api = process.env.NEXT_PUBLIC_HANKO_API!;
Expand All @@ -10,17 +10,19 @@ interface Props {

function HankoAuth({ setError }: Props) {
const router = useRouter();
const hankoClient = useMemo(() => new Hanko(api), []);
const [hankoClient, setHankoClient] = useState<Hanko>();

const redirectToTodos = useCallback(() => {
router.replace("/todo").catch(setError);
}, [router, setError]);

useEffect(() => setHankoClient(new Hanko(api)), []);

useEffect(() => {
register(api).catch(setError);
}, [setError]);

useEffect(() => hankoClient.onAuthFlowCompleted(() => {
useEffect(() => hankoClient?.onAuthFlowCompleted(() => {
redirectToTodos()
}), [hankoClient, redirectToTodos]);

Expand Down
5 changes: 1 addition & 4 deletions frontend/examples/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { NextPage } from "next";
import dynamic from "next/dynamic";
import styles from "../styles/Todo.module.css";
import React, { useState } from "react";
import HankoAuth from "../components/HankoAuth";

const HankoAuth = dynamic(() => import("../components/HankoAuth"), {
ssr: false,
});

const Home: NextPage = () => {
const [error, setError] = useState<Error | null>(null);
Expand Down
15 changes: 4 additions & 11 deletions frontend/examples/nextjs/pages/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import React, { useCallback, useEffect, useRef, useState } from "react";
import { NextPage } from "next";
import { useRouter } from "next/router";
import dynamic from "next/dynamic";
import styles from "../styles/Todo.module.css";

import { SessionExpiredModal } from "../components/SessionExpiredModal";
import { Hanko } from "@teamhanko/hanko-elements";
import { SessionExpiredModal } from "../components/SessionExpiredModal";
import HankoProfile from "../components/HankoProfile";

const hankoAPI = process.env.NEXT_PUBLIC_HANKO_API!;
const HankoProfile = dynamic(() => import("../components/HankoProfile"), {
ssr: false,
});
const api = process.env.NEXT_PUBLIC_HANKO_API!;

const Profile: NextPage = () => {
const router = useRouter();
const [hankoClient, setHankoClient] = useState<Hanko>();

useEffect(() => {
import("@teamhanko/hanko-elements").then(({ Hanko }) => setHankoClient(new Hanko(hankoAPI)));
}, []);

const modalRef = useRef<HTMLDialogElement>(null);
const [error, setError] = useState<Error | null>(null);

Expand Down Expand Up @@ -49,6 +41,7 @@ const Profile: NextPage = () => {
}
}, [hankoClient, redirectToLogin]);

useEffect(() => setHankoClient(new Hanko(api)), []);

useEffect(() => hankoClient?.onUserLoggedOut(() => {
redirectToLogin();
Expand Down

0 comments on commit fd93beb

Please sign in to comment.