mt-flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
47 lines (46 loc) • 1.62 kB
JavaScript
'use client';
import { getSession } from 'next-auth/react';
import { create } from 'zustand';
import { persist, subscribeWithSelector } from 'zustand/middleware';
import { mtmClient } from '../mtmapi/mtm.client';
import { getMe } from '../mtmapi/mtmapi';
const createSessionStore = (initProps) => {
const DEFAULT_PROPS = {};
return create()(persist(subscribeWithSelector((set, get) => ({
...DEFAULT_PROPS,
isLoadingMe: false,
...initProps,
async loadNextAuthSession() {
const session = await getSession();
set({ ...get(), sessionData: session });
},
async loadMe() {
set({ ...get(), isLoadingMe: true });
const me = await 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 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;
},
}));
};
//全局。
export const useSessionStore = createSessionStore();