mt-flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
50 lines (49 loc) • 1.81 kB
JavaScript
'use client';
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSessionStore = void 0;
const react_1 = require("next-auth/react");
const zustand_1 = require("zustand");
const middleware_1 = require("zustand/middleware");
const mtm_client_1 = require("../mtmapi/mtm.client");
const mtmapi_1 = require("../mtmapi/mtmapi");
const createSessionStore = (initProps) => {
const DEFAULT_PROPS = {};
return (0, zustand_1.create)()((0, middleware_1.persist)((0, middleware_1.subscribeWithSelector)((set, get) => ({
...DEFAULT_PROPS,
isLoadingMe: false,
...initProps,
async loadNextAuthSession() {
const session = await (0, react_1.getSession)();
set({ ...get(), sessionData: session });
},
async loadMe() {
set({ ...get(), isLoadingMe: true });
const me = await (0, mtmapi_1.getMe)();
set({ ...get(), isLoadingMe: false, me });
},
isAdmin() {
const pre = get();
return !!pre.me?.roles?.find((x) => x == 'admin');
},
async login(logininput) {
console.log('TODO: 用户密码登录', logininput);
const result = await mtm_client_1.mtmClient.auth.login(logininput);
console.log('登录结果', result);
},
})), {
name: 'session',
version: 3,
// skipHydration: true,
onRehydrateStorage(state) {
console.log('[onRehydrateStorage][session]', state);
state.loadNextAuthSession();
},
migrate(persistedState, version) {
console.log('version', version, persistedState);
return persistedState;
},
}));
};
//全局。
exports.useSessionStore = createSessionStore();
;