adding better auth logic
This commit is contained in:
@@ -154,9 +154,9 @@ function ListPeople({ people }: { people: User[] }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ListConversations() {
|
function ListConversations() {
|
||||||
const { conversations } = useTerminal();
|
const { conversationMap } = useTerminal();
|
||||||
|
|
||||||
if (conversations.length === 0) {
|
if (Object.keys(conversationMap).length === 0) {
|
||||||
return (
|
return (
|
||||||
<Typography align="center" variant="caption">
|
<Typography align="center" variant="caption">
|
||||||
Look for someone by name or email to start a conversation!
|
Look for someone by name or email to start a conversation!
|
||||||
|
|||||||
@@ -10,11 +10,7 @@ import Conversations from './Conversations';
|
|||||||
import Navbar from './Navbar';
|
import Navbar from './Navbar';
|
||||||
import Conversation from '@nirvana/core/src/models/conversation.model';
|
import Conversation from '@nirvana/core/src/models/conversation.model';
|
||||||
import useAuth from '../providers/AuthProvider';
|
import useAuth from '../providers/AuthProvider';
|
||||||
import {
|
import { createOneOnOneConversation, useGetConversationsQueryLIVE } from '../firebase/firestore';
|
||||||
createOneOnOneConversation,
|
|
||||||
getConversationsQueryLIVE,
|
|
||||||
useGetConversationsQueryLIVE,
|
|
||||||
} from '../firebase/firestore';
|
|
||||||
import { Link, AudioClip, Image } from '@nirvana/core/src/models/content.model';
|
import { Link, AudioClip, Image } from '@nirvana/core/src/models/content.model';
|
||||||
import { useImmer } from 'use-immer';
|
import { useImmer } from 'use-immer';
|
||||||
import { User } from '@nirvana/core/src/models/user.model';
|
import { User } from '@nirvana/core/src/models/user.model';
|
||||||
@@ -91,6 +87,8 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
|
|||||||
|
|
||||||
// cache of selected conversation
|
// cache of selected conversation
|
||||||
const selectedConversation: Conversation | undefined = useMemo(() => {
|
const selectedConversation: Conversation | undefined = useMemo(() => {
|
||||||
|
if (!selectedConversationId) return undefined;
|
||||||
|
|
||||||
// select if we do have it
|
// select if we do have it
|
||||||
|
|
||||||
const conversation = conversationMap[selectedConversationId];
|
const conversation = conversationMap[selectedConversationId];
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ import {
|
|||||||
getAuth,
|
getAuth,
|
||||||
} from 'firebase/auth';
|
} from 'firebase/auth';
|
||||||
import { useSnackbar } from 'notistack';
|
import { useSnackbar } from 'notistack';
|
||||||
import { createUser } from '../firebase/firestore';
|
import { createUser, getUserById } from '../firebase/firestore';
|
||||||
|
|
||||||
import CircularProgress from '@mui/material/CircularProgress';
|
import CircularProgress from '@mui/material/CircularProgress';
|
||||||
import NirvanaLogo from '../components/NirvanaLogo';
|
import NirvanaLogo from '../components/NirvanaLogo';
|
||||||
|
import { User } from '@nirvana/core/src/models/user.model';
|
||||||
|
|
||||||
interface IAuthContext {
|
interface IAuthContext {
|
||||||
user?: FirebaseUser;
|
user?: FirebaseUser;
|
||||||
|
nirvanaUser?: User;
|
||||||
logout?: () => void;
|
logout?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
|
|
||||||
// since auth.currentuser isn't updating
|
// since auth.currentuser isn't updating
|
||||||
const [currentUser, setCurrentUser] = useState<FirebaseUser>(null);
|
const [currentUser, setCurrentUser] = useState<FirebaseUser>(null);
|
||||||
|
const [currentNirvanaUser, setCurrentNirvanaUser] = useState<User>(null);
|
||||||
|
|
||||||
// are we still waiting on initial auth to trigger?
|
// are we still waiting on initial auth to trigger?
|
||||||
const [authIniting, setAuthIniting] = useState<boolean>(true);
|
const [authIniting, setAuthIniting] = useState<boolean>(true);
|
||||||
@@ -43,8 +46,16 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
console.log('auth changed!!!!', user);
|
console.log('auth changed!!!!', user);
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
// TODO: add user to user's table with new sign in infp
|
// getting from our data store of users
|
||||||
await createUser(user);
|
let fetchedNirvanaUser = await getUserById(user.uid);
|
||||||
|
|
||||||
|
if (!fetchedNirvanaUser) {
|
||||||
|
await createUser(user);
|
||||||
|
|
||||||
|
fetchedNirvanaUser = await getUserById(user.uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
setCurrentNirvanaUser(fetchedNirvanaUser);
|
||||||
|
|
||||||
setCurrentUser(user);
|
setCurrentUser(user);
|
||||||
} else {
|
} else {
|
||||||
@@ -58,7 +69,7 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return () => authListener();
|
return () => authListener();
|
||||||
}, [setCurrentUser, enqueueSnackbar, setAuthIniting]);
|
}, [setCurrentUser, enqueueSnackbar, setAuthIniting, setCurrentNirvanaUser]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const tokenListener = window.electronAPI.once(
|
const tokenListener = window.electronAPI.once(
|
||||||
@@ -124,8 +135,8 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthContext.Provider value={{ user: currentUser, logout }}>
|
<AuthContext.Provider value={{ user: currentUser, logout, nirvanaUser: currentNirvanaUser }}>
|
||||||
{currentUser ? <>{children}</> : <Login />}
|
{currentUser && currentNirvanaUser ? <>{children}</> : <Login />}
|
||||||
</AuthContext.Provider>
|
</AuthContext.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user