0.1.2 • Published 3 years ago

react-matrix-auth v0.1.2

Weekly downloads
-
License
-
Repository
-
Last release
3 years ago

ATTENTION This is work in progress. Use at your own peril. Interfaces might break without announcements up to version 1

Demo:

Check out the DemoApp in the src/demo folder for a basic login flow

Public API:

react-router-dom is currently required to read the loginToken parameter for single sign on.

In your index.js, add the MatrixAuthProvider around your App.

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import {createBrowserHistory} from "history";
import {Router} from "react-router-dom";
import {MatrixAuthProvider} from "./context/MatrixAuthContext";

const history = createBrowserHistory();
ReactDOM.render(
    <React.StrictMode>
        <Router history={history}>
            <MatrixAuthProvider sessionName={"your-matrix-client-session-name"} ssoRedirectUrl={"https://custom/redirect/URL"}>
                <App/>
            </MatrixAuthProvider>
        </Router>
    </React.StrictMode>,
    document.getElementById('root')
);

You can then use the useMatrixAuth() hook

const MyComponent = () =>{
    const matrix = useMatrixAuth();
    return <div>{matrix.isLoggedIn() ? "Logged In" : "Not logged in"}</div>
}

Check the MatrixAuthContextType interface for all available properties and methods.

// this might be outdated, please check the source interface for 100% up to date definitions
export interface MatrixAuthContextType {
    accessToken: string,
    deviceId: string,
    loginToken: string,
    userId: string,
    sessionName?: string,
    ssoRedirectUrl?: string,
    homeserverUrl: string,
    authMethods: GetAuthMethodsResponse,
    setHomeserverUrl: (url: string) => void,
    loadAuthMethods: () => void,
    logout: () => void,
    isLoggedIn: () => boolean,
}