Set autogenerated guest username
This commit is contained in:
		
					parent
					
						
							
								ea2cf98160
							
						
					
				
			
			
				commit
				
					
						0d771e0aa4
					
				
			
		
					 6 changed files with 70 additions and 78 deletions
				
			
		| 
						 | 
					@ -91,18 +91,22 @@ export function useClient(homeserverUrl) {
 | 
				
			||||||
        const authStore = localStorage.getItem("matrix-auth-store");
 | 
					        const authStore = localStorage.getItem("matrix-auth-store");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (authStore) {
 | 
					        if (authStore) {
 | 
				
			||||||
          const { user_id, device_id, access_token } = JSON.parse(authStore);
 | 
					          const { user_id, device_id, access_token, guest } =
 | 
				
			||||||
 | 
					            JSON.parse(authStore);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          const client = await initClient({
 | 
					          const client = await initClient(
 | 
				
			||||||
            baseUrl: homeserverUrl,
 | 
					            {
 | 
				
			||||||
            accessToken: access_token,
 | 
					              baseUrl: homeserverUrl,
 | 
				
			||||||
            userId: user_id,
 | 
					              accessToken: access_token,
 | 
				
			||||||
            deviceId: device_id,
 | 
					              userId: user_id,
 | 
				
			||||||
          });
 | 
					              deviceId: device_id,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            guest
 | 
				
			||||||
 | 
					          );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          localStorage.setItem(
 | 
					          localStorage.setItem(
 | 
				
			||||||
            "matrix-auth-store",
 | 
					            "matrix-auth-store",
 | 
				
			||||||
            JSON.stringify({ user_id, device_id, access_token })
 | 
					            JSON.stringify({ user_id, device_id, access_token, guest })
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          return client;
 | 
					          return client;
 | 
				
			||||||
| 
						 | 
					@ -153,7 +157,7 @@ export function useClient(homeserverUrl) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }, []);
 | 
					  }, []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const registerGuest = useCallback(async (displayName) => {
 | 
					  const registerGuest = useCallback(async () => {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const registrationClient = matrix.createClient(homeserverUrl);
 | 
					      const registrationClient = matrix.createClient(homeserverUrl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -170,9 +174,13 @@ export function useClient(homeserverUrl) {
 | 
				
			||||||
        true
 | 
					        true
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      await client.setProfileInfo("displayname", {
 | 
				
			||||||
 | 
					        displayname: `Guest ${client.getUserIdLocalpart()}`,
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      localStorage.setItem(
 | 
					      localStorage.setItem(
 | 
				
			||||||
        "matrix-auth-store",
 | 
					        "matrix-auth-store",
 | 
				
			||||||
        JSON.stringify({ user_id, device_id, access_token })
 | 
					        JSON.stringify({ user_id, device_id, access_token, guest: true })
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      setState({ client, loading: false, authenticated: true });
 | 
					      setState({ client, loading: false, authenticated: true });
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										30
									
								
								src/ErrorModal.jsx
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/ErrorModal.jsx
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,30 @@
 | 
				
			||||||
 | 
					import React, { useEffect } from "react";
 | 
				
			||||||
 | 
					import { Center, Content, Modal } from "./Layout";
 | 
				
			||||||
 | 
					import { Link } from "react-router-dom";
 | 
				
			||||||
 | 
					import { ErrorMessage } from "./Input";
 | 
				
			||||||
 | 
					import styles from "./ErrorModal.module.css";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function ErrorModal({ error }) {
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    console.error(error);
 | 
				
			||||||
 | 
					  }, [error]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <Content>
 | 
				
			||||||
 | 
					      <Center>
 | 
				
			||||||
 | 
					        <Modal>
 | 
				
			||||||
 | 
					          <h2>Error</h2>
 | 
				
			||||||
 | 
					          <div className={styles.errorModalContent}>
 | 
				
			||||||
 | 
					            <ErrorMessage>{error.message}</ErrorMessage>
 | 
				
			||||||
 | 
					            <p>
 | 
				
			||||||
 | 
					              <Link to="/login">Login</Link>
 | 
				
			||||||
 | 
					            </p>
 | 
				
			||||||
 | 
					            <p>
 | 
				
			||||||
 | 
					              <Link to="/register">Register</Link>
 | 
				
			||||||
 | 
					            </p>
 | 
				
			||||||
 | 
					          </div>
 | 
				
			||||||
 | 
					        </Modal>
 | 
				
			||||||
 | 
					      </Center>
 | 
				
			||||||
 | 
					    </Content>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										9
									
								
								src/ErrorModal.module.css
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/ErrorModal.module.css
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,9 @@
 | 
				
			||||||
 | 
					.errorModalContent {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.errorModalContent p {
 | 
				
			||||||
 | 
					  margin-bottom: 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -14,26 +14,21 @@ See the License for the specific language governing permissions and
 | 
				
			||||||
limitations under the License.
 | 
					limitations under the License.
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import React, { useState, useRef, useCallback } from "react";
 | 
					import React, { useState, useEffect } from "react";
 | 
				
			||||||
import styles from "./GuestAuthPage.module.css";
 | 
					import styles from "./GuestAuthPage.module.css";
 | 
				
			||||||
import { useLocation, useHistory, Link } from "react-router-dom";
 | 
					import { useLocation, useHistory } from "react-router-dom";
 | 
				
			||||||
import { Header, LeftNav } from "./Header";
 | 
					import { Header, LeftNav } from "./Header";
 | 
				
			||||||
import { Button, FieldRow, InputField, ErrorMessage } from "./Input";
 | 
					import { Center, Content, Modal } from "./Layout";
 | 
				
			||||||
import { Center, Content, Info, Modal } from "./Layout";
 | 
					import { ErrorModal } from "./ErrorModal";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function GuestAuthPage({ onLoginAsGuest }) {
 | 
					export function GuestAuthPage({ onLoginAsGuest }) {
 | 
				
			||||||
  const displayNameRef = useRef();
 | 
					 | 
				
			||||||
  const history = useHistory();
 | 
					  const history = useHistory();
 | 
				
			||||||
  const location = useLocation();
 | 
					  const location = useLocation();
 | 
				
			||||||
  const [error, setError] = useState();
 | 
					  const [error, setError] = useState();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const onSubmitLoginForm = useCallback(
 | 
					  useEffect(() => {
 | 
				
			||||||
    (e) => {
 | 
					    onLoginAsGuest("Guest " + Math.round(Math.random() * 999)).catch(setError);
 | 
				
			||||||
      e.preventDefault();
 | 
					  }, [onLoginAsGuest, location, history]);
 | 
				
			||||||
      onLoginAsGuest(displayNameRef.current.value).catch(setError);
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    [onLoginAsGuest, location, history]
 | 
					 | 
				
			||||||
  );
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
    <div className={styles.guestAuthPage}>
 | 
					    <div className={styles.guestAuthPage}>
 | 
				
			||||||
| 
						 | 
					@ -43,46 +38,7 @@ export function GuestAuthPage({ onLoginAsGuest }) {
 | 
				
			||||||
      <Content>
 | 
					      <Content>
 | 
				
			||||||
        <Center>
 | 
					        <Center>
 | 
				
			||||||
          <Modal>
 | 
					          <Modal>
 | 
				
			||||||
            <h2>Login As Guest</h2>
 | 
					            {error ? <ErrorModal error={error} /> : <div>Loading...</div>}
 | 
				
			||||||
            <form onSubmit={onSubmitLoginForm}>
 | 
					 | 
				
			||||||
              <FieldRow>
 | 
					 | 
				
			||||||
                <InputField
 | 
					 | 
				
			||||||
                  type="text"
 | 
					 | 
				
			||||||
                  ref={displayNameRef}
 | 
					 | 
				
			||||||
                  placeholder="Display Name"
 | 
					 | 
				
			||||||
                  label="Display Name"
 | 
					 | 
				
			||||||
                  autoCorrect="off"
 | 
					 | 
				
			||||||
                  autoCapitalize="none"
 | 
					 | 
				
			||||||
                />
 | 
					 | 
				
			||||||
              </FieldRow>
 | 
					 | 
				
			||||||
              {error && (
 | 
					 | 
				
			||||||
                <FieldRow>
 | 
					 | 
				
			||||||
                  <ErrorMessage>{error.message}</ErrorMessage>
 | 
					 | 
				
			||||||
                </FieldRow>
 | 
					 | 
				
			||||||
              )}
 | 
					 | 
				
			||||||
              <FieldRow rightAlign>
 | 
					 | 
				
			||||||
                <Button type="submit">Login as guest</Button>
 | 
					 | 
				
			||||||
              </FieldRow>
 | 
					 | 
				
			||||||
            </form>
 | 
					 | 
				
			||||||
            <Info>
 | 
					 | 
				
			||||||
              <Link
 | 
					 | 
				
			||||||
                to={{
 | 
					 | 
				
			||||||
                  pathname: "/login",
 | 
					 | 
				
			||||||
                  state: location.state,
 | 
					 | 
				
			||||||
                }}
 | 
					 | 
				
			||||||
              >
 | 
					 | 
				
			||||||
                Sign in
 | 
					 | 
				
			||||||
              </Link>
 | 
					 | 
				
			||||||
              {" or "}
 | 
					 | 
				
			||||||
              <Link
 | 
					 | 
				
			||||||
                to={{
 | 
					 | 
				
			||||||
                  pathname: "/register",
 | 
					 | 
				
			||||||
                  state: location.state,
 | 
					 | 
				
			||||||
                }}
 | 
					 | 
				
			||||||
              >
 | 
					 | 
				
			||||||
                Create account
 | 
					 | 
				
			||||||
              </Link>
 | 
					 | 
				
			||||||
            </Info>
 | 
					 | 
				
			||||||
          </Modal>
 | 
					          </Modal>
 | 
				
			||||||
        </Center>
 | 
					        </Center>
 | 
				
			||||||
      </Content>
 | 
					      </Content>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										19
									
								
								src/Room.jsx
									
										
									
									
									
								
							
							
						
						
									
										19
									
								
								src/Room.jsx
									
										
									
									
									
								
							| 
						 | 
					@ -25,7 +25,7 @@ import {
 | 
				
			||||||
  ScreenshareButton,
 | 
					  ScreenshareButton,
 | 
				
			||||||
} from "./RoomButton";
 | 
					} from "./RoomButton";
 | 
				
			||||||
import { Header, LeftNav, RightNav, CenterNav } from "./Header";
 | 
					import { Header, LeftNav, RightNav, CenterNav } from "./Header";
 | 
				
			||||||
import { Button, ErrorMessage } from "./Input";
 | 
					import { Button } from "./Input";
 | 
				
			||||||
import { GroupCallState } from "matrix-js-sdk/src/webrtc/groupCall";
 | 
					import { GroupCallState } from "matrix-js-sdk/src/webrtc/groupCall";
 | 
				
			||||||
import VideoGrid, {
 | 
					import VideoGrid, {
 | 
				
			||||||
  useVideoGridLayout,
 | 
					  useVideoGridLayout,
 | 
				
			||||||
| 
						 | 
					@ -35,6 +35,7 @@ import { useGroupCall } from "matrix-react-sdk/src/hooks/useGroupCall";
 | 
				
			||||||
import { useCallFeed } from "matrix-react-sdk/src/hooks/useCallFeed";
 | 
					import { useCallFeed } from "matrix-react-sdk/src/hooks/useCallFeed";
 | 
				
			||||||
import { useMediaStream } from "matrix-react-sdk/src/hooks/useMediaStream";
 | 
					import { useMediaStream } from "matrix-react-sdk/src/hooks/useMediaStream";
 | 
				
			||||||
import { fetchGroupCall } from "./ConferenceCallManagerHooks";
 | 
					import { fetchGroupCall } from "./ConferenceCallManagerHooks";
 | 
				
			||||||
 | 
					import { ErrorModal } from "./ErrorModal";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function useLoadGroupCall(client, roomId) {
 | 
					function useLoadGroupCall(client, roomId) {
 | 
				
			||||||
  const [state, setState] = useState({
 | 
					  const [state, setState] = useState({
 | 
				
			||||||
| 
						 | 
					@ -70,7 +71,7 @@ export function Room({ client }) {
 | 
				
			||||||
  if (error) {
 | 
					  if (error) {
 | 
				
			||||||
    return (
 | 
					    return (
 | 
				
			||||||
      <div className={styles.room}>
 | 
					      <div className={styles.room}>
 | 
				
			||||||
        <LoadingErrorView error={error} />
 | 
					        <ErrorModal error={error} />
 | 
				
			||||||
      </div>
 | 
					      </div>
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -160,20 +161,6 @@ export function EnteringRoomView() {
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function LoadingErrorView({ error }) {
 | 
					 | 
				
			||||||
  useEffect(() => {
 | 
					 | 
				
			||||||
    console.error(error);
 | 
					 | 
				
			||||||
  }, [error]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return (
 | 
					 | 
				
			||||||
    <>
 | 
					 | 
				
			||||||
      <div className={styles.centerMessage}>
 | 
					 | 
				
			||||||
        <ErrorMessage>{error.message}</ErrorMessage>
 | 
					 | 
				
			||||||
      </div>
 | 
					 | 
				
			||||||
    </>
 | 
					 | 
				
			||||||
  );
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function RoomSetupView({
 | 
					function RoomSetupView({
 | 
				
			||||||
  roomName,
 | 
					  roomName,
 | 
				
			||||||
  state,
 | 
					  state,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -80,10 +80,12 @@ limitations under the License.
 | 
				
			||||||
  flex: 1;
 | 
					  flex: 1;
 | 
				
			||||||
  justify-content: center;
 | 
					  justify-content: center;
 | 
				
			||||||
  align-items: center;
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.centerMessage p {
 | 
					.centerMessage p {
 | 
				
			||||||
  display: block;
 | 
					  display: block;
 | 
				
			||||||
 | 
					  margin-bottom: 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.roomContainer {
 | 
					.roomContainer {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue