Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
usmanramay07 opened this issue May 17, 2023 · 4 comments
Open

Comments

@usmanramay07
Copy link

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
Copy link
Contributor

lxix commented May 23, 2023

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
Copy link

justCXQ commented Jan 11, 2024

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
Copy link
Contributor

lxix commented Jan 11, 2024

@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 #119 (comment)

@prescottprue
Copy link

prescottprue commented Jan 27, 2024

Also ran into this and the workaround in #119 (comment) 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants