UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

46 lines (45 loc) 2.05 kB
import { CMS_ENCRYPTION_KEY } from "astro:env/server"; import { Notifications } from "studiocms:notifier"; import { SDKCoreJs as sdk } from "studiocms:sdk"; import { AuthKit } from "@withstudiocms/auth-kit"; import { AuthKitOptions } from "@withstudiocms/auth-kit/config"; import { AuthSessionCookieName } from "../../consts.js"; import { Effect, Layer, runEffect } from "../../effect.js"; const notifyAdmin = Effect.fn(function* (type, message) { const notifier = yield* Notifications.pipe(Effect.provide(Notifications.Default)); yield* notifier.sendAdminNotification(type, message); }); const authKitConfig = AuthKitOptions.Live({ CMS_ENCRYPTION_KEY, session: { cookieName: AuthSessionCookieName, expTime: 1e3 * 60 * 60 * 24 * 14, sessionTools: { createSession: async (params) => runEffect(sdk.AUTH.session.create(params)), deleteSession: async (sessionId) => runEffect(sdk.AUTH.session.delete(sessionId)).then(() => void 0), sessionAndUserData: async (sessionId) => runEffect(sdk.AUTH.session.sessionWithUser(sessionId)), updateSession: async (sessionId, params) => runEffect(sdk.AUTH.session.update(sessionId, params.expiresAt)) } }, userTools: { idGenerator: () => crypto.randomUUID(), createLocalUser: async (params) => runEffect(sdk.AUTH.user.create(params)), createOAuthUser: async (params) => runEffect(sdk.AUTH.oAuth.create(params)), getCurrentPermissions: async (userId) => runEffect(sdk.AUTH.permission.currentStatus(userId)), getUserById: async (id) => runEffect(sdk.GET.users.byId(id)), getUserByEmail: async (email) => runEffect(sdk.GET.users.byEmail(email)), updateLocalUser: async (id, params) => runEffect(sdk.AUTH.user.update(id, params)), notifier: { admin: (type, message) => runEffect(notifyAdmin(type, message)) } } }); const { Encryption, Password, Session, User } = await runEffect( AuthKit.pipe(Effect.provide(Layer.provideMerge(AuthKit.Default, authKitConfig))) ); export { Encryption, Password, Session, User };