mirror of
https://github.com/linsa-io/linsa.git
synced 2026-04-19 07:00:02 +02:00
fix: conflict
This commit is contained in:
@@ -11,11 +11,11 @@ export function DeepLinkProvider({ children }: DeepLinkProviderProps) {
|
||||
const eventHandlers: { [key: string]: (event: Event) => void } = {
|
||||
click: (event: Event) => {
|
||||
const e = event as MouseEvent
|
||||
console.log("Click event:", { x: e.clientX, y: e.clientY })
|
||||
// console.log("Click event:", { x: e.clientX, y: e.clientY })
|
||||
},
|
||||
keydown: (event: Event) => {
|
||||
const e = event as KeyboardEvent
|
||||
console.log("Keydown event:", { key: e.key, code: e.code })
|
||||
// console.log("Keydown event:", { key: e.key, code: e.code })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,105 +2,56 @@
|
||||
|
||||
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 { useAuth, useClerk } from "@clerk/nextjs"
|
||||
import { useJazzClerkAuth } from "jazz-react-auth-clerk"
|
||||
import { usePathname } from "next/navigation"
|
||||
|
||||
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>
|
||||
function assertPeerUrl(url: string | undefined): asserts url is `wss://${string}` | `ws://${string}` {
|
||||
if (!url) {
|
||||
throw new Error("NEXT_PUBLIC_JAZZ_PEER_URL is not defined")
|
||||
}
|
||||
if (!url.startsWith("wss://") && !url.startsWith("ws://")) {
|
||||
throw new Error("NEXT_PUBLIC_JAZZ_PEER_URL must start with wss:// or ws://")
|
||||
}
|
||||
}
|
||||
|
||||
export const JazzClerkAuthCtx = createContext<{
|
||||
errors: string[]
|
||||
}>({
|
||||
errors: []
|
||||
})
|
||||
const rawUrl = process.env.NEXT_PUBLIC_JAZZ_PEER_URL
|
||||
assertPeerUrl(rawUrl)
|
||||
const JAZZ_PEER_URL = rawUrl
|
||||
|
||||
export function JazzClerkAuth({ children }: { children: React.ReactNode }) {
|
||||
interface ChildrenProps {
|
||||
children: React.ReactNode
|
||||
}
|
||||
|
||||
export function JazzAndAuth({ children }: ChildrenProps) {
|
||||
const pathname = usePathname()
|
||||
return pathname === "/" ? <JazzGuest>{children}</JazzGuest> : <JazzAuth>{children}</JazzAuth>
|
||||
}
|
||||
|
||||
export function JazzAuth({ children }: ChildrenProps) {
|
||||
const clerk = useClerk()
|
||||
const [errors, setErrors] = useState<string[]>([])
|
||||
const { isLoaded } = useAuth()
|
||||
const [authMethod] = useJazzClerkAuth(clerk)
|
||||
|
||||
const authMethod = useMemo(() => {
|
||||
return new BrowserClerkAuth(
|
||||
{
|
||||
onError: error => {
|
||||
void clerk.signOut()
|
||||
setErrors(errors => [...errors, error.toString()])
|
||||
}
|
||||
},
|
||||
clerk
|
||||
)
|
||||
}, [clerk])
|
||||
if (!isLoaded) return null
|
||||
|
||||
return (
|
||||
<JazzClerkAuthCtx.Provider value={{ errors }}>
|
||||
<AuthMethodCtx.Provider value={authMethod}>{children}</AuthMethodCtx.Provider>
|
||||
</JazzClerkAuthCtx.Provider>
|
||||
<Jazz.Provider auth={authMethod || "guest"} peer={JAZZ_PEER_URL}>
|
||||
{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
|
||||
}
|
||||
export function JazzGuest({ children }: ChildrenProps) {
|
||||
return (
|
||||
<Jazz.Provider auth="guest" peer={JAZZ_PEER_URL}>
|
||||
{children}
|
||||
</Jazz.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user