praveenn77 / docusaurus-lunr-search

Local / Offline Search for docusaurus
https://praveenn77.github.io/docusaurus-lunr-search-demo/
MIT License
471 stars 79 forks source link

Search crawler not working after adding authentication in to my docs #105

Open usmanramay07 opened 1 year ago

usmanramay07 commented 1 year ago

I have added the authentication to my docs. After that search is not working. Search files are empty in the build folder. Below are the build logs. Any help will be great Thanks!

docusaurus-lunr-search:: Building search docs and lunr index file docusaurus-lunr-search:: Start scanning documents in 16 threads docusaurus-lunr-search:: Indexing time: 309.268ms docusaurus-lunr-search:: indexed 0 documents out of 90 docusaurus-lunr-search:: writing search-doc.json docusaurus-lunr-search:: writing search-doc-1684321241892.json docusaurus-lunr-search:: writing lunr-index.json docusaurus-lunr-search:: writing lunr-index-1684321241892.json docusaurus-lunr-search:: End of process

lxix commented 1 year ago

Which version of node, docusaurus and docusaurus-lunr-search do you use? How do you implemented the authentication? Can you provide an example code/project?

justCXQ commented 7 months ago

I encounted the same issue with docusaurus 3.0.1 and docusaurus-lunr-search 3.3.1, placed the authentication logic in src/theme/Root.js. The code is as follows. Thank you for any help.

 "ues client";
import React, { useEffect, useState } from "react";
import { validToken } from "../../utils/tools";
import styles from './index.module.css';
import Translate, { translate } from "@docusaurus/Translate";

// Default implementation, that you can customize
export default function Root({ children }) {
  const [wrong, setWrong] = useState("");
  const [token, setToken] = useState('');

  const handleSubmit = async (e) => {
    e.preventDefault();
    if (!e.currentTarget?.token.value) return;
    setWrong("");
    const data = e.currentTarget?.token.value;
    if (!validToken(data)) {
      setWrong(translate({
        message: 'Wrong token',
        description: 'The wrong token message',
      }));
      return;
    }
    if (typeof window !== 'undefined') {
      sessionStorage.setItem("token", data);
      setToken(data)
    }
  };

  useEffect(() => {
    const currentToken = sessionStorage.getItem('token');
    if (currentToken && validToken(currentToken)) {
      setToken(sessionStorage.getItem('token'));
    }
    return () => {
      sessionStorage.setItem("token", null);
    }
  }, [])

  return (
    <>
      {token ? children :
        <div
          style={{
            display: "flex",
            flexDirection: "column",
            justifyContent: "center",
            alignItems: "center",
            height: "50vh",
            fontSize: "20px",
          }}
        >

          <div className={styles['token-card']}>
            <div className={styles["token-label"]}><Translate>Please enter your Token</Translate></div>
            <form onSubmit={handleSubmit}>
              <input
                type="token"
                id="token"
                name="token"
                className={styles["token-input"]}
                placeholder={"token"}
              />
              <div className={styles["wrong-msg"]}>{wrong}</div>
              <button
                type="submit"
                className={styles["normal-line"]}
              >
                <Translate>Submit</Translate>
              </button>
            </form>
          </div>
        </div>}
    </>
  );

}
lxix commented 7 months ago

@justCXQ My guess is that in your case the authentication code gets executed in the build process without a valid token, so ofc the access will be denied and the indexing fails. Take a look at useIsBrowser and maybe try to bypass the authentication when this value is false.

Although this approach could work, for various security concerns I strongly advise implementing authentication before clients reach docusaurus: BasicAuth, ForwardAuth, ...

Edit: Also https://github.com/praveenn77/docusaurus-lunr-search/issues/119#issuecomment-1732476424

prescottprue commented 7 months ago

Also ran into this and the workaround in https://github.com/praveenn77/docusaurus-lunr-search/issues/119#issuecomment-1732476424 worked great thanks @lxix - might be worth calling this out in the documentation or maybe a warn since all most will see is docusaurus-lunr-search:: indexed 0 documents out of X message (doesn't give much context)