@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
64 lines (55 loc) • 1.4 kB
text/typescript
import { StateCreator } from 'zustand/vanilla';
import { enableAuth, enableClerk, enableNextAuth } from '@/const/auth';
import { UserStore } from '../../store';
export interface UserAuthAction {
enableAuth: () => boolean;
/**
* universal logout method
*/
logout: () => Promise<void>;
/**
* universal login method
*/
openLogin: () => Promise<void>;
}
export const createAuthSlice: StateCreator<
UserStore,
[['zustand/devtools', never]],
[],
UserAuthAction
> = (set, get) => ({
enableAuth: () => {
return enableAuth;
},
logout: async () => {
if (enableClerk) {
get().clerkSignOut?.({ redirectUrl: location.toString() });
return;
}
if (enableNextAuth) {
const { signOut } = await import('next-auth/react');
signOut();
}
},
openLogin: async () => {
if (enableClerk) {
const reditectUrl = location.toString();
get().clerkSignIn?.({
fallbackRedirectUrl: reditectUrl,
signUpForceRedirectUrl: reditectUrl,
signUpUrl: '/signup',
});
return;
}
if (enableNextAuth) {
const { signIn } = await import('next-auth/react');
// Check if only one provider is available
const providers = get()?.oAuthSSOProviders;
if (providers && providers.length === 1) {
signIn(providers[0]);
return;
}
signIn();
}
},
});