Skip to content

Commit

Permalink
feat: basic logging in
Browse files Browse the repository at this point in the history
  • Loading branch information
eldu committed Feb 6, 2024
1 parent 4b3e796 commit f3724e8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { Navbar } from './components/react-ccv-components/Navbar.tsx';
import Footer from './components/react-ccv-components/Footer';

import { ContentPage } from './components/ContentPage';
// import { useAuthStateChanged } from './utils/firebase.ts';
import { useAuthStateChanged } from './utils/firebase.ts';

export function App() {
// useAuthStateChanged()
useAuthStateChanged();

return (
<div aria-live="polite">
Expand Down
12 changes: 12 additions & 0 deletions src/components/LoginButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Button, ButtonProps } from 'react-bootstrap';
import { handleLogin } from '../utils/firebase.ts';

type LoginButtonProps = ButtonProps;

export const LoginButton = ({ ...buttonProps }: LoginButtonProps) => {
return (
<Button onClick={handleLogin} {...buttonProps}>
Log In
</Button>
);
};
14 changes: 8 additions & 6 deletions src/components/react-ccv-components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import React from 'react';
import Container from 'react-bootstrap/Container';
import { Nav, Navbar as DefaultNavbar } from 'react-bootstrap';

import { useSelector } from 'react-redux';
import { handleLogin, handleLogout } from '../../utils/firebase.ts';
import { selectUser } from '../../store/slice/appState';
import { ReactComponent as CCVLogo } from './assets/svg/ccv-logo.svg';
import { ReactComponent as BrownLogo } from './assets/svg/brown-logo.svg';

import '../../styles/custom.scss';

export const Navbar = () => {
const user = useSelector(selectUser);

return (
<DefaultNavbar expand="lg" className="bg-body-tertiary">
<Container>
Expand All @@ -20,10 +23,9 @@ export const Navbar = () => {
<DefaultNavbar.Toggle aria-controls="basic-navbar-nav" />
<DefaultNavbar.Collapse id="basic-navbar-nav" role="" className="justify-content-end">
<Nav className="ml-auto">
{/*<Navbar.Text>*/}
{/* Signed in as: <a href="#login">Mark Otto</a>*/}
{/*</Navbar.Text>*/}
<Nav.Link href="#link">Login</Nav.Link>
<DefaultNavbar.Text>Signed in as: {user.displayName}</DefaultNavbar.Text>
<Nav.Link onClick={handleLogin}>Login</Nav.Link>
<Nav.Link onClick={handleLogout}>Logout</Nav.Link>
</Nav>
</DefaultNavbar.Collapse>
</Container>
Expand Down
7 changes: 6 additions & 1 deletion src/store/slice/appState.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ export const appStateSlice = createSlice({
name: 'appState',
initialState: {
publications: [],
user: {},
},
reducers: {
setPublications: (state, action) => {
state.publications = action.payload;
},
setUser: (state, action) => {
state.user = action.payload;
},
},
});

export const { setPublications } = appStateSlice.actions;
export const { setPublications, setUser } = appStateSlice.actions;
export const selectPublications = (state) => state.publications;
export const selectUser = (state) => state.user;

export default appStateSlice.reducer;
20 changes: 17 additions & 3 deletions src/utils/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'firebase/auth';
import { getFirestore, doc, setDoc, collection, onSnapshot } from 'firebase/firestore';

import { setPublications } from '../store/slice/appState';
import { setPublications, setUser } from '../store/slice/appState';

const firebaseConfig = {
apiKey: 'AIzaSyBlu1GzA5jvM6mh6taIcjtNgcSEVxlxa1Q',
Expand Down Expand Up @@ -53,12 +53,26 @@ export const handleLogout = async () => {
* Custom Reach hook to subscribe to Authentication changes
*/
export const useAuthStateChanged = () => {
const dispatch = useDispatch();

useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (user) {
console.log(user);
const { displayName, email } = user;

dispatch(
setUser({
displayName,
email,
})
);
} else {
console.log('Signed Out');
dispatch(
setUser({
displayName: '',
email: '',
})
);
}
});

Expand Down

0 comments on commit f3724e8

Please sign in to comment.