feat: guest auth (#141)

* feat: Start using guest auth

* feat: Implement more functionality to work as guest

* chore: update package and tweak public route

* chore: update root package json

* chore: update web package json

---------

Co-authored-by: Aslam H <iupin5212@gmail.com>
This commit is contained in:
Anselm Eickhoff
2024-09-06 21:11:43 +01:00
committed by GitHub
parent e61aae02d5
commit 844b1ae334
16 changed files with 138 additions and 205 deletions

View File

@@ -3,104 +3,27 @@
import { createJazzReactApp } from "jazz-react"
import { LaAccount } from "@/lib/schema"
import { useClerk } from "@clerk/nextjs"
import { createContext, useMemo, useState } from "react"
import { AuthMethodCtx } from "jazz-react"
import { useJazzClerkAuth } from "jazz-react-auth-clerk"
const Jazz = createJazzReactApp({
AccountSchema: LaAccount
})
export const { useAccount, useCoState, useAcceptInvite } = Jazz
export const { useAccount, useAccountOrGuest, useCoState, useAcceptInvite } = Jazz
export function JazzProvider({ children }: { children: React.ReactNode }) {
return <Jazz.Provider peer="wss://mesh.jazz.tools/?key=example@gmail.com">{children}</Jazz.Provider>
}
export const JazzClerkAuthCtx = createContext<{
errors: string[]
}>({
errors: []
})
export function JazzClerkAuth({ children }: { children: React.ReactNode }) {
export function JazzAndAuth({ children }: { children: React.ReactNode }) {
const clerk = useClerk()
const [errors, setErrors] = useState<string[]>([])
const authMethod = useMemo(() => {
return new BrowserClerkAuth(
{
onError: error => {
void clerk.signOut()
setErrors(errors => [...errors, error.toString()])
}
},
clerk
)
}, [clerk])
const [auth, state] = useJazzClerkAuth(clerk)
return (
<JazzClerkAuthCtx.Provider value={{ errors }}>
<AuthMethodCtx.Provider value={authMethod}>{children}</AuthMethodCtx.Provider>
</JazzClerkAuthCtx.Provider>
<>
{state.errors.map((error) => (
<div key={error}>{error}</div>
))}
<Jazz.Provider auth={auth || "guest"} peer="wss://mesh.jazz.tools/?key=example@gmail.com">
{children}
</Jazz.Provider>
</>
)
}
import { Account, AuthMethod, AuthResult, ID } from "jazz-tools"
import type { LoadedClerk } from "@clerk/types"
import { AgentSecret } from "cojson"
export class BrowserClerkAuth implements AuthMethod {
constructor(
public driver: BrowserClerkAuth.Driver,
private readonly clerkClient: LoadedClerk
) {}
async start(): Promise<AuthResult> {
if (this.clerkClient.user) {
const storedCredentials = this.clerkClient.user.unsafeMetadata
if (storedCredentials.jazzAccountID) {
if (!storedCredentials.jazzAccountSecret) {
throw new Error("No secret for existing user")
}
return {
type: "existing",
credentials: {
accountID: storedCredentials.jazzAccountID as ID<Account>,
secret: storedCredentials.jazzAccountSecret as AgentSecret
},
onSuccess: () => {},
onError: (error: string | Error) => {
this.driver.onError(error)
}
}
} else {
return {
type: "new",
creationProps: {
name: this.clerkClient.user.fullName || this.clerkClient.user.username || this.clerkClient.user.id
},
saveCredentials: async (credentials: { accountID: ID<Account>; secret: AgentSecret }) => {
await this.clerkClient.user?.update({
unsafeMetadata: {
jazzAccountID: credentials.accountID,
jazzAccountSecret: credentials.secret
}
})
},
onSuccess: () => {},
onError: (error: string | Error) => {
this.driver.onError(error)
}
}
}
} else {
throw new Error("Not signed in")
}
}
}
export namespace BrowserClerkAuth {
export interface Driver {
onError: (error: string | Error) => void
}
}
}