feat(mobile): get user role
This commit is contained in:
parent
d107127349
commit
537ef01c83
|
|
@ -6,6 +6,7 @@ import type { EntryModel } from "../store/entry/types"
|
|||
import type { FeedModel } from "../store/feed/types"
|
||||
import type { ListModel } from "../store/list/store"
|
||||
import type { SubscriptionModel } from "../store/subscription/store"
|
||||
import type { UserModel } from "../store/user/store"
|
||||
import type { HonoApiClient } from "./types"
|
||||
|
||||
class Morph {
|
||||
|
|
@ -200,6 +201,17 @@ class Morph {
|
|||
siteUrl: data.siteUrl!,
|
||||
}
|
||||
}
|
||||
|
||||
toUser(data: HonoApiClient.User_Get, isMe?: boolean): UserModel {
|
||||
return {
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
email: data.email,
|
||||
handle: data.handle,
|
||||
image: data.image,
|
||||
isMe: isMe ? 1 : 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const honoMorph = new Morph()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
/* eslint-disable @typescript-eslint/no-namespace */
|
||||
import type { AuthSession } from "@follow/shared"
|
||||
|
||||
import type { apiClient } from "../lib/api-fetch"
|
||||
|
||||
// Add ExtractData type utility
|
||||
|
|
@ -14,6 +16,7 @@ export namespace HonoApiClient {
|
|||
export type Entry_Inbox_Get = ExtractData<typeof apiClient.entries.inbox.$get>
|
||||
export type List_List_Get = ExtractData<typeof apiClient.lists.list.$get>[number]
|
||||
export type Feed_Get = ExtractData<typeof apiClient.feeds.$get>
|
||||
export type User_Get = Exclude<AuthSession, null>["user"]
|
||||
|
||||
export type ActionRule = Exclude<
|
||||
ExtractData<typeof apiClient.actions.$get>["rules"],
|
||||
|
|
|
|||
|
|
@ -3,3 +3,7 @@ import { useUserStore } from "./store"
|
|||
export const whoami = () => {
|
||||
return useUserStore.getState().whoami
|
||||
}
|
||||
|
||||
export const role = () => {
|
||||
return useUserStore.getState().role
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
import type { AuthSession } from "@follow/shared"
|
||||
import { getAnalytics } from "@react-native-firebase/analytics"
|
||||
|
||||
import type { UserSchema } from "@/src/database/schemas/types"
|
||||
import { apiClient } from "@/src/lib/api-fetch"
|
||||
import { changeEmail, sendVerificationEmail, twoFactor, updateUser } from "@/src/lib/auth"
|
||||
import { toast } from "@/src/lib/toast"
|
||||
import { honoMorph } from "@/src/morph/hono"
|
||||
import { UserService } from "@/src/services/user"
|
||||
|
||||
import { createImmerSetter, createTransaction, createZustandStore } from "../internal/helper"
|
||||
|
|
@ -18,11 +20,13 @@ export type MeModel = UserModel & {
|
|||
type UserStore = {
|
||||
users: Record<string, UserModel>
|
||||
whoami: MeModel | null
|
||||
role: "user" | "trial"
|
||||
}
|
||||
|
||||
export const useUserStore = createZustandStore<UserStore>("user")(() => ({
|
||||
users: {},
|
||||
whoami: null,
|
||||
role: "trial",
|
||||
}))
|
||||
|
||||
const get = useUserStore.getState
|
||||
|
|
@ -30,14 +34,16 @@ const immerSet = createImmerSetter(useUserStore)
|
|||
|
||||
class UserSyncService {
|
||||
async whoami() {
|
||||
const res = (await (apiClient["better-auth"] as any)["get-session"].$get()) as {
|
||||
user: UserModel
|
||||
} | null // TODO
|
||||
const res = (await (apiClient["better-auth"] as any)[
|
||||
"get-session"
|
||||
].$get()) as AuthSession | null
|
||||
if (res) {
|
||||
const user = honoMorph.toUser(res.user, true)
|
||||
immerSet((state) => {
|
||||
state.whoami = res.user
|
||||
state.whoami = user
|
||||
state.role = res.role
|
||||
})
|
||||
userActions.upsertMany([res.user])
|
||||
userActions.upsertMany([user])
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
export * from "./src/bridge"
|
||||
export * from "./src/env"
|
||||
export { type AppType } from "./src/hono"
|
||||
export * from "./src/hono"
|
||||
export * from "./src/language"
|
||||
|
|
|
|||
Loading…
Reference in New Issue