* feat: add better-auth * feat: remove hono/auth-js * fix: authention check * fix: signout refetch * feat: init server auth plugins * feat: remove types/authjs * feat: server auth create session plugin * feat: web login returning * fix: types * fix: test env
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import type { User } from "@auth/core/types"
|
|
import type { Credentials } from "@eneris/push-receiver/dist/types"
|
|
|
|
import { logger } from "~/logger"
|
|
|
|
import { apiClient } from "./api-client"
|
|
import { store } from "./store"
|
|
|
|
const BetterAuthKey = "betterAuthSessionCookie"
|
|
export const setBetterAuthSessionCookie = (cookie: string) => store.set(BetterAuthKey, cookie)
|
|
export const getBetterAuthSessionCookie = (): string | null => store.get(BetterAuthKey)
|
|
export const cleanBetterAuthSessionCookie = () => store.set(BetterAuthKey, null)
|
|
|
|
const UserKey = "user"
|
|
export const setUser = (user: User) => store.set(UserKey, JSON.stringify(user))
|
|
export const getUser = (): User | null => {
|
|
const user = store.get(UserKey)
|
|
return user ? JSON.parse(user) : null
|
|
}
|
|
|
|
export const cleanUser = () => store.set(UserKey, null)
|
|
|
|
export const updateNotificationsToken = async (newCredentials?: Credentials) => {
|
|
if (newCredentials) {
|
|
store.set("notifications-credentials", newCredentials)
|
|
}
|
|
const credentials = newCredentials || store.get("notifications-credentials")
|
|
if (credentials?.fcm?.token) {
|
|
try {
|
|
await apiClient.messaging.$post({
|
|
json: {
|
|
token: credentials.fcm.token,
|
|
channel: "desktop",
|
|
},
|
|
})
|
|
} catch (error) {
|
|
logger.error("updateNotificationsToken error: ", error)
|
|
}
|
|
}
|
|
}
|