Skip to content

Commit 58eb198

Browse files
authored
Merge pull request #56 from untilhamza/release/v1.11.0
Release/v1.11.0
2 parents 9f9d93b + b9b3172 commit 58eb198

File tree

10 files changed

+410
-443
lines changed

10 files changed

+410
-443
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"autoprefixer": "10.4.5",
2424
"bootstrap": "^5.1.3",
2525
"eslint": "^8.17.0",
26-
"firebase": "^9.8.1",
26+
"firebase": "^9.18.0",
27+
"firebaseui": "^6.0.2",
2728
"formik": "^2.2.9",
2829
"react": "^18.1.0",
2930
"react-bootstrap": "^2.3.1",

src/App.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContext } from "react";
1+
import { useContext, useEffect } from "react";
22
import { Switch, Route, Redirect } from "react-router-dom";
33
import { Layout } from "antd";
44
import Navbar from "./components/NavBar/Navbar";
@@ -36,8 +36,8 @@ const App = () => {
3636
</Route>
3737

3838
<Route path="/admin">
39-
{authCtx.isLoggedIn && <Admin />}
40-
{!authCtx.isLoggedIn && <Redirect to="/login" />}
39+
{authCtx.isAdmin && <Admin />}
40+
{!authCtx.isAdmin && <Redirect to="/login" />}
4141
</Route>
4242
<Route path="/check-appointment">
4343
{/* maybe pass the appointment id so it can be fetched */}
@@ -50,16 +50,20 @@ const App = () => {
5050
<Logout />
5151
</Route>
5252
<Route path="/settings">
53-
<SettingsPage />
53+
{authCtx.isAdmin && <SettingsPage />}
54+
{!authCtx.isAdmin && <Redirect to="/login" />}
5455
</Route>
5556
<Route path="/general-settings">
56-
<GeneralSettingsPage />
57+
{authCtx.isAdmin && <GeneralSettingsPage />}
58+
{!authCtx.isAdmin && <Redirect to="/login" />}
5759
</Route>
5860
<Route path="/slot-settings">
59-
<SlotSettingsPage />
61+
{authCtx.isAdmin && <SlotSettingsPage />}
62+
{!authCtx.isAdmin && <Redirect to="/login" />}
6063
</Route>
6164
<Route path="/special-settings">
62-
<SpecialDaySettingsPage />
65+
{authCtx.isAdmin && <SpecialDaySettingsPage />}
66+
{!authCtx.isAdmin && <Redirect to="/login" />}
6367
</Route>
6468
<Route path="/">
6569
<Customer />

src/components/BookingForm/BookingForm.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import moment from "moment";
44
import { Form, Button } from "react-bootstrap";
55
import { DatePicker } from "antd";
66
import TimeSelector from "../TimeSelector/TimeSelector";
7+
import { useContext } from "react";
8+
import AuthContext from "../../store/auth-context";
79

810
import "./BookingForm.css";
911

@@ -18,6 +20,7 @@ const schema = yup.object().shape({
1820
});
1921

2022
const BookingForm = ({ onCancel, onConfirm, oldData, slots, onGetSlots, slotStatus, settings }) => {
23+
const authCtx = useContext(AuthContext);
2124
function handleGetSlots(date) {
2225
onGetSlots(date);
2326
}
@@ -34,7 +37,11 @@ const BookingForm = ({ onCancel, onConfirm, oldData, slots, onGetSlots, slotStat
3437
validationSchema={schema}
3538
onSubmit={(values, { resetForm }) => {
3639
//submitting data!
37-
onConfirm(values);
40+
if (!authCtx.isLoggedIn) {
41+
authCtx.handleCustomerLogin();
42+
return;
43+
}
44+
onConfirm({ ...values, userId: authCtx.userId, googleAccountName: authCtx.user.displayName, email: authCtx.user.email, photoURL: authCtx.user.photoURL });
3845
resetForm();
3946
}}
4047
initialValues={{

src/components/NavBar/Navbar.js

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import SideBar from "../SideBar/SideBar";
1010
import "./NavBar.css";
1111
const Navbar = () => {
1212
const [showSidebar, setShowSidebar] = useState(false);
13-
1413
const handleToggleSidebar = () => setShowSidebar((isShown) => !isShown);
1514

1615
// const authCtx = useContext(AuthContext)

src/components/SideBar/SideBar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const SideBar = ({ show, onToggle }) => {
3838
if (!item.protected) {
3939
items.push(listItem);
4040
}
41-
if (item.protected && authCtx.isLoggedIn) {
41+
if (item.protected && authCtx.isAdmin) {
4242
items.push(listItem);
4343
}
4444
return items;

src/env.json

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
11
{
2-
"current-env": "production",
3-
"production": {
4-
"apiKey": "AIzaSyDyl-tV8qgWpgTMISb3M9ybaVxNPoGKuGA",
5-
"authDomain": "appointments-9fa9d.firebaseapp.com",
6-
"projectId": "appointments-9fa9d",
7-
"storageBucket": "appointments-9fa9d.appspot.com",
8-
"messagingSenderId": "514032231488",
9-
"appId": "1:514032231488:web:a841adecd9dffad5aaaba5",
10-
"measurementId": "G-N2D42P9687"
11-
},
12-
"development": {
13-
"apiKey": "AIzaSyBNipwPtKZOGsNA6ezsEv2f_LjQ0Kg7J0k",
14-
"authDomain": "appointments-dev-ff26f.firebaseapp.com",
15-
"projectId": "appointments-dev-ff26f",
16-
"storageBucket": "appointments-dev-ff26f.appspot.com",
17-
"messagingSenderId": "1051559849613",
18-
"appId": "1:1051559849613:web:e8e80694637eedde697cc4"
19-
},
20-
"test": {
21-
"apiKey": "AIzaSyBNipwPtKZOGsNA6ezsEv2f_LjQ0Kg7J0k",
22-
"authDomain": "appointments-dev-ff26f.firebaseapp.com",
23-
"projectId": "appointments-dev-ff26f",
24-
"storageBucket": "appointments-dev-ff26f.appspot.com",
25-
"messagingSenderId": "1051559849613",
26-
"appId": "1:1051559849613:web:e8e80694637eedde697cc4"
27-
}
2+
"current-env": "production"
283
}

src/pages/NewBooking.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const NewBooking = () => {
2727
}
2828

2929
async function handleConfirm(bookingData) {
30-
sendRequest(bookingData);
30+
sendRequest({ ...bookingData });
3131
}
3232

3333
// useEffect(() => {

src/store/auth-context.js

+46-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import React, { useState } from "react";
22
import { auth } from "../database/firebase-config";
3+
import { useHistory } from "react-router-dom";
34
// import { httpGetSettings } from "../hooks/request"
45
// import useHttp from "../hooks/useHttp"
5-
import { createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signOut } from "firebase/auth";
6+
import { createUserWithEmailAndPassword, onAuthStateChanged, signOut, GoogleAuthProvider, signInWithPopup, signInWithEmailAndPassword } from "firebase/auth";
67
//describes how the context looks like
8+
9+
const provider = new GoogleAuthProvider();
10+
11+
const ADMINS = ["Nwzxrf32Uee9i6hbTXSN2mWVzlC2", "lHxJifUfgHhJkECibwAudvf3MGp1", "lru8dL4JVWTycq0LHhHgyaWqX133"];
12+
713
const AuthContext = React.createContext({
814
name: "",
915
email: "",
1016
isLoggedIn: false,
17+
isAdmin: false,
18+
userId: "",
19+
user: null,
1120
onLogin: ({ email, password }) => {},
1221
onLogout: ({ email, password }) => {},
1322
onRegister: ({ email, password }) => {},
1423
});
1524

1625
export const AuthContextProvider = (props) => {
1726
const [user, setUser] = useState(null);
27+
const history = useHistory();
1828

1929
const handleLogout = async () => {
2030
try {
@@ -33,6 +43,35 @@ export const AuthContextProvider = (props) => {
3343
}
3444
};
3545

46+
const handleCustomerLogin = async () => {
47+
signInWithPopup(auth, provider)
48+
.then((result) => {
49+
// This gives you a Google Access Token. You can use it to access the Google API.
50+
const credential = GoogleAuthProvider.credentialFromResult(result);
51+
const token = credential.accessToken;
52+
// The signed-in user info.
53+
const user = result.user;
54+
//alert("user logged in" + JSON.stringify(user));
55+
setUser(user);
56+
// IdP data available using getAdditionalUserInfo(result)
57+
// ...
58+
//TODO: handle customer user login
59+
history.push("/new-booking");
60+
})
61+
.catch((error) => {
62+
// Handle Errors here.
63+
const errorCode = error.code;
64+
const errorMessage = error.message;
65+
// The email of the user's account used.
66+
const email = error.customData.email;
67+
// The AuthCredential type that was used.
68+
const credential = GoogleAuthProvider.credentialFromError(error);
69+
// ...
70+
alert("error when logging in" + JSON.stringify(error));
71+
setUser(null);
72+
});
73+
};
74+
3675
const handleRegister = async ({ email, password }) => {
3776
try {
3877
const user = await createUserWithEmailAndPassword(auth, email, password);
@@ -43,21 +82,20 @@ export const AuthContextProvider = (props) => {
4382
};
4483

4584
onAuthStateChanged(auth, (currentUser) => {
46-
//TODO: admins should be put in firebase table not here
47-
const ADMINS = ["Nwzxrf32Uee9i6hbTXSN2mWVzlC2", "lHxJifUfgHhJkECibwAudvf3MGp1", "lru8dL4JVWTycq0LHhHgyaWqX133"];
48-
if (currentUser && ADMINS.includes(currentUser.uid)) {
49-
setUser(currentUser);
50-
} else {
51-
setUser(null);
52-
}
85+
console.log("user", currentUser);
86+
setUser(currentUser);
5387
});
5488
const contextValue = {
5589
name: user?.displayname,
5690
email: user?.email,
91+
userId: user?.uid,
92+
isAdmin: user && ADMINS.includes(user?.uid),
5793
isLoggedIn: user ? true : false,
94+
user: user,
5895
onLogin: handleLogin,
5996
onLogout: handleLogout,
6097
onRegister: handleRegister,
98+
handleCustomerLogin,
6199
};
62100

63101
return <AuthContext.Provider value={contextValue}>{props.children}</AuthContext.Provider>;

src/version.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "1.10.0"
2+
"version": "1.11.0"
33
}

0 commit comments

Comments
 (0)