Home Assistant authentication + React Hooks, with Typescript
Easily add Home Assistant authentication to your React application. This is a thin wrapper over the home-assistant-js-websocket library for easily accessing its authentication functionality in your app, with React Context and Hooks. Types are provided.
Library is written in TypeScript. File an issue if you find any problems.
yarn add react-netlify-identity
When you call useHassAuth()
, you can destructure these variables and methods:
-
authed: boolean | undefined
: if the user is logged in, undefined while loading -
connection: Connection | undefined
: thehome-assistant-js-websocket
connection object once established -
loading: boolean
: if we are determining auth state -
login(hassUrl?: string)
: function to authenticate against provided Home Assistant instance URL (optional if provided throughHassAuthContextProvider
) -
logout()
: logs the user out -
_hassUrl: string | undefined
: the hassUrl used in login method
import React from 'react';
import { HassAuthContextProvider } from 'react-hass-auth';
const App = () => {
const url = 'http://hassio.local:8123'; // Supply the url of your Home Assistant instance. If left out, you are required to specify it when calling login.
return (
<HassAuthContextProvider hassUrl={url}>
{/* rest of your app */}
</HassAuthContextProvider>
);
};
MIT © nielsrowinbik