react-thau v1.0.8
react-thau
Interact with the Thau API from you React application. Authentication made easy.
Installation
npm install react-thauor
yarn add react-thauUsage
Firstly you need to have the Thau API running. Let's assume that you ran it using the following command:
docker run \
-e ENABLED_STRATEGIES=password,google,facebook \
-e ENV=local \
-e DATA_BACKEND=sqlite \
-e GOOGLE_CLIENT_ID=<YOUR_GOOGLE_CLIENT_ID> \
-e FACEBOOK_CLIENT_ID=<YOUR_FACEBOOK_CLIENT_ID> \
-e FACEBOOK_CLIENT_SECRET=<YOUR_FACEBOOK_CLIENT_SECRET> \
mgrin/thauThen, wrap your application with AuthProvider:
import { AuthProvider } from 'react-thau'
export default function App() {
<AuthProvider authUrl="http://localhost:9000">
...
</AuthProvider>
}And now down in the tree you can use any of the available exported hooks (see documentation for more details about each of the hooks):
useAuth()useCreateUserWithPassword()useCurrentProvider()useLoginWithFacebook()useLoginWithGoogle()useLoginWithPassword()useLogout()useUser()
Documentation
AuthProvider
Wrapper component. Is required to pass the context value to hooks. Takes following props:
authUrl- REQUIRED URL to the Thau API instancetokenStorage- place to store the auth token. Possible values:localStorage,cookie. Default:localStoragefetchOptions- options to pass to thefetchcall. Used to set custom headers
useAuth()
Takes no arguments. Returns you the auth state.
import * as React from 'react'
import { useAuth } from 'react-thau'
export default () => {
const auth = useAuth()
return <p>{JSON.stringify(auth)}</p>
}Auth state type is defined as follows:
loading: booleanerror: Error | nullavailableStrategies: string[] | nulltokenStorage: 'cookie' | 'localStorage'google?: { clientId: string }facebook?: { clientId: string; graphVersion: string }setUser: (user?: User, provider?: string) => voiduser?: Userprovider?: string
useUser()
Takes no arguments. Returns you the logged in user or undefined.
import * as React from 'react'
import { useUser } from 'react-thau'
export default () => {
const { loading, error, user } = useUser()
return <p>{JSON.stringify(user)}</p>
}User fields:
id: stringemail: stringusername: stringfirst_name?: stringlast_name?: stringdate_of_birth?: numbergender?: stringpicture?: string
useCurrentProvider()
Takes no arguments. Reeturns you the login strategy user used to login
import * as React from 'react'
import { useCurrentProvider } from 'react-thau'
export default () => {
const { loading, error, provider } = useCurrentProvider()
return <p>{provider}</p>
}useCreateUserWithPassword({ fetchOptions?: FetchOptions })
Takes an optional argument, fetchOptions, that will be passed to the respective fetch call. Returns the [state, createUser: (newUser) => void] tuple.
import * as React from 'react'
import { useCreateUserWithPassword } from 'react-thau'
export default () => {
const [state, createUser] = useCreateUserWithPassword()
return <p>{JSON.stringify(state)}</p>
}State fields:
loading: booleanerror: APIError | nulltoken: string | null
createUser function arguments:
newUser: NewUserType:email: stringpassword: stringusername: stringfirst_name?: stringlast_name?: stringdate_of_birth?: numbergender?: stringpicture?: string
useLoginWithFacebook({ fetchOptions?: FetchOptions })
Takes an optional argument, fetchOptions, that will be passed to the respective fetch call. Returns the [state, login: () => void] tuple.
import * as React from 'react'
import { useLoginWithFacebook } from 'react-thau'
export default () => {
const [state, login] = useLoginWithFacebook()
return <p>{JSON.stringify(state)}</p>
}State fields:
loading: booleanerror: APIError | nulltoken: string | nulluser: User | null
useLoginWithGoogle({ fetchOptions?: FetchOptions })
Takes an optional argument, fetchOptions, that will be passed to the respective fetch call. Returns the [state, login: () => void] tuple.
import * as React from 'react'
import { useLoginWithGoogle } from 'react-thau'
export default () => {
const [state, login] = useLoginWithGoogle()
return <p>{JSON.stringify(state)}</p>
}State fields:
loading: booleanerror: APIError | nulltoken: string | nulluser: User | null
useLoginWithPassword({ fetchOptions?: FetchOptions })
Takes an optional argument, fetchOptions, that will be passed to the respective fetch call. Returns the [state, login: (credentials: LoginWithPasswordCredentials) => void] tuple.
import * as React from 'react'
import { useLoginWithPassword } from 'react-thau'
export default () => {
const email = "username@yourapp.com"
const password = "very_secrt_password"
const [state, login] = useLoginWithPassword({ email, password })
return <p>{JSON.stringify(state)}</p>
}State fields:
loading: booleanerror: APIError | nulltoken: string | nulluser: User | null
login function arguments:
credentials: LoginWithPasswordCredentials:email: stringpassword: string
useLogout()
Takes an optional argument, fetchOptions, that will be passed to the respective fetch call. Returns the [state, logout: () => void] tuple.
import * as React from 'react'
import { useLogout } from 'react-thau'
export default () => {
const [state, logout] = useLogout()
return <p>{JSON.stringify(state)}</p>
}5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago