Skip to content

Commit

Permalink
feat(react-auth): fix types for useTensei hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Jul 4, 2021
1 parent d3e1d91 commit e13db4f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 2 additions & 0 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ export class Auth {
boolean('Two Factor Enabled')
.hideOnCreate()
.hideOnUpdate()
.hideOnInsertApi()
.hideOnUpdateApi()
.nullable(),
text('Two Factor Secret')
.hideOnApi()
Expand Down
7 changes: 4 additions & 3 deletions packages/react-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export {
AuthContext,
AuthContextProvider,
AuthContextWrapperProps,
TenseiAuthContext,
TenseiAuthProvider,
TenseiAuthContextInterface,
TenseiAuthProviderProps,
useAuth,
useTensei,
} from './use-auth'
26 changes: 11 additions & 15 deletions packages/react-auth/src/use-auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { sdk } from '@tensei/sdk'
import React, { createContext, useContext, FunctionComponent, useState, useEffect } from 'react'


export interface AuthContextInterface {
export interface TenseiAuthContextInterface {
isLoading: boolean
accessToken?: string
// @ts-ignore
Expand All @@ -13,23 +13,19 @@ export interface AuthContextInterface {
setAuth: (response?: import('@tensei/sdk').AuthResponse) => void
}

export const AuthContext = createContext<AuthContextInterface>({
export const TenseiAuthContext = createContext<TenseiAuthContextInterface>({
isLoading: true,
tensei: {} as any,
user: undefined as any,
setAuth: () => {},
})

export const useAuth = () => useContext(AuthContext)
export const useAuth = () => useContext(TenseiAuthContext)

export const useTensei = () => {
const { tensei } = useAuth()
export const useTensei: () => TenseiAuthContextInterface['tensei'] = () => useAuth().tensei

return tensei
}

export interface AuthContextWrapperProps {
tensei?: AuthContextInterface['tensei']
export interface TenseiAuthProviderProps {
tensei?: TenseiAuthContextInterface['tensei']

// @ts-ignore
options?: import('@tensei/sdk').SdkOptions
Expand All @@ -42,11 +38,11 @@ function getUrlParameter(name: string) {
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '))
}

export const AuthContextProvider: FunctionComponent<AuthContextWrapperProps> = ({ children, tensei: tenseiInstance, options }) => {
export const TenseiAuthProvider: FunctionComponent<TenseiAuthProviderProps> = ({ children, tensei: tenseiInstance, options }) => {
const [accessToken, setAccessToken] = useState<string>()
const [user, setUser] = useState<AuthContextInterface['user']>()
const [user, setUser] = useState<TenseiAuthContextInterface['user']>()
const [tensei] = useState(tenseiInstance ? tenseiInstance : sdk(options))
const [isLoading, setIsLoading] = useState<AuthContextInterface['isLoading']>(true)
const [isLoading, setIsLoading] = useState<TenseiAuthContextInterface['isLoading']>(true)

const subscribeToAuthChanges = () => {
tensei.auth().listen((auth: any) => {
Expand Down Expand Up @@ -88,14 +84,14 @@ export const AuthContextProvider: FunctionComponent<AuthContextWrapperProps> = (
}, [])

return (
<AuthContext.Provider value={{
<TenseiAuthContext.Provider value={{
user,
tensei,
setAuth,
isLoading,
accessToken
}}>
{children}
</AuthContext.Provider>
</TenseiAuthContext.Provider>
)
}
2 changes: 1 addition & 1 deletion packages/rest/src/sdk/generators/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export const generateAuthApi = (config: PluginSetupConfig) => {
* await tensei.auth().logout()
*
**/
logout(payload: { skipAuthentication?: boolean }): void
logout(payload?: { skipAuthentication?: boolean }): void
/**
*
Expand Down

0 comments on commit e13db4f

Please sign in to comment.