Skip to content

Commit

Permalink
feat: add login, logout, register to the hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed May 18, 2023
1 parent 1a652a2 commit c3a455b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
46 changes: 31 additions & 15 deletions packages/odd-preact/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
useState,
} from 'preact/hooks'
import * as odd from '@oddjs/odd'
import { Auth } from '@oddjs/passkeys'

/** @type {import('preact').Context<import('./types.js').OddContext>} */
// @ts-ignore - TODO fix this
Expand All @@ -20,6 +19,8 @@ const OddContext = createContext({
program: undefined,
isUsernameAvailable: undefined,
login: undefined,
logout: undefined,
register: undefined,
})
/**
*
Expand Down Expand Up @@ -76,18 +77,6 @@ export function OddContextProvider({
}
}, [components, componentsFactory, config])

useEffect(() => {
if (program) {
// TODO unregister listeners
program.on('session:destroy', () => {
setSession(null)
})
program.on('session:create', ({ session }) => {
setSession(session)
})
}
}, [program])

const isUsernameAvailable = useCallback(
async (/** @type {string} */ username) => {
if (!program) {
Expand All @@ -107,11 +96,30 @@ export function OddContextProvider({
if (!program) {
throw new Error('Needs program.')
}
return await Auth.login(program, username)
// @ts-ignore
await program.auth.register({ username })
const session = await program.auth.session()
if (session) {
setSession(session)
return session
}
},
[program]
)

const logout = useCallback(
async (/** @type {string | undefined} */ username) => {
if (!program) {
throw new Error('Needs program.')
}
if (session) {
await session.destroy()
setSession(null)
}
},
[program, session]
)

/** @type {import('./types.js').OddContext} */
const value = useMemo(() => {
if (!program) {
Expand All @@ -121,7 +129,9 @@ export function OddContextProvider({
session: null,
program: undefined,
isUsernameAvailable,
register: login,
login,
logout,
}
}

Expand All @@ -132,7 +142,9 @@ export function OddContextProvider({
session: null,
program: undefined,
isUsernameAvailable,
register: login,
login,
logout,
}
}
if (program) {
Expand All @@ -142,7 +154,9 @@ export function OddContextProvider({
session,
program,
isUsernameAvailable,
register: login,
login,
logout,
}
}

Expand All @@ -152,9 +166,11 @@ export function OddContextProvider({
session,
program,
isUsernameAvailable,
register: login,
login,
logout,
}
}, [program, error, session, isUsernameAvailable, login])
}, [program, error, session, isUsernameAvailable, login, logout])

return createElement(OddContext.Provider, { value, children })
}
Expand Down
20 changes: 15 additions & 5 deletions packages/odd-preact/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,51 @@ import type {
Configuration,
Components,
ProgramError,
Maybe,
} from '@oddjs/odd'

export type LoginFn = (username?: string) => Promise<Session | undefined>
export type LogoutFn = () => Promise<void>

export type OddContext =
| {
isLoading: true
error: undefined
session: null
program: undefined
isUsernameAvailable: (username: string) => Promise<boolean>
login: (username?: string) => Promise<Maybe<Session>>
login: LoginFn
register: LoginFn
logout: LogoutFn
}
| {
isLoading: false
error: ProgramError
session: null
program: undefined
isUsernameAvailable: (username: string) => Promise<boolean>
login: (username?: string) => Promise<Maybe<Session>>
login: LoginFn
register: LoginFn
logout: LogoutFn
}
| {
isLoading: false
error: undefined
session: Session | null
program: Program
isUsernameAvailable: (username: string) => Promise<boolean>
login: (username?: string) => Promise<Maybe<Session>>
login: LoginFn
register: LoginFn
logout: LogoutFn
}
| {
isLoading: false
error: undefined
session: Session | null
program: Program
isUsernameAvailable: (username: string) => Promise<boolean>
login: (username?: string) => Promise<Maybe<Session>>
login: LoginFn
register: LoginFn
logout: LogoutFn
}

export interface OddContextProviderProps {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c3a455b

Please sign in to comment.