Skip to content

Commit

Permalink
Merge branch 'master' into 134-filter-by-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
AzizPatel786 authored Apr 3, 2024
2 parents 99b2340 + 0cf0441 commit 45023dd
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 27 deletions.
37 changes: 10 additions & 27 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import "App.css"
import Navbar from "components/composite/Navbar/Navbar"
import Home from "pages/Home"
import About from "pages/About"
import Register from "pages/Register"
import Login from "pages/Login"
import Events from "pages/Events"
import Contact from "pages/Contact"
import Checkout from "pages/Checkout"
import Booking from "pages/Booking"
import Profile from "pages/Profile"
import Admin from "pages/Admin"
import Thanks from "pages/Thanks"
import AdminBookingsDetailedView from "pages/AdminBookingsDetailedView"
import { BrowserRouter as Router, Route, Routes } from "react-router-dom"
import { ThemeProvider } from "@mui/material"
import { LocalizationProvider } from "@mui/x-date-pickers"
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"
import queryClient from "services/QueryClient"
import theme from "theme"
import { QueryClientProvider } from "@tanstack/react-query"
import { AllRoutes, RouteProps } from "./routes/routes"

function App() {
return (
Expand All @@ -31,21 +20,15 @@ function App() {
<Navbar />
<div className="content" style={{ height: "100%" }}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/events" element={<Events />} />
<Route path="/contact" element={<Contact />} />
<Route path="/register" element={<Register />} />
<Route path="/login" element={<Login />} />
<Route path="/checkout" element={<Checkout />} />
<Route path="/booking" element={<Booking />} />
<Route path="/profile" element={<Profile />} />
<Route path="/admin" element={<Admin />} />
<Route path="/thanks" element={<Thanks />} />
<Route
path="/admin/bookings"
element={<AdminBookingsDetailedView />}
/>
{AllRoutes.map(
(routeDetails: RouteProps, index: number) => (
<Route
key={index}
path={routeDetails.path}
element={routeDetails.element}
/>
)
)}
</Routes>
</div>
</div>
Expand Down
33 changes: 33 additions & 0 deletions client/src/routes/routes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import AdminBookingDetails from "components/composite/AdminBookingDetails/AdminBookingDetails"
import About from "pages/About"
import Admin from "pages/Admin"
import Booking from "pages/Booking"
import Checkout from "pages/Checkout"
import Contact from "pages/Contact"
import Events from "pages/Events"
import Home from "pages/Home"
import Login from "pages/Login"
import Profile from "pages/Profile"
import Register from "pages/Register"
import Thanks from "pages/Thanks"
import { JSX } from "react"

export interface RouteProps {
path: string
element: JSX.Element
}

export const AllRoutes: RouteProps[] = [
{ path: "/", element: <Home /> },
{ path: "/about", element: <About /> },
{ path: "/events", element: <Events /> },
{ path: "/contact", element: <Contact /> },
{ path: "/register", element: <Register /> },
{ path: "/login", element: <Login /> },
{ path: "/checkout", element: <Checkout /> },
{ path: "/booking", element: <Booking /> },
{ path: "/profile", element: <Profile /> },
{ path: "/admin", element: <Admin /> },
{ path: "/thanks", element: <Thanks /> },
{ path: "/admin/bookings", element: <AdminBookingDetails /> }
]
39 changes: 39 additions & 0 deletions server/src/business-layer/services/AuthService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { auth } from "business-layer/security/Firebase"
import AuthService from "./AuthService"
import { UserRecord } from "firebase-admin/auth"

describe("AuthService Integration Tests", () => {
it("deletes a user", async () => {
await auth.createUser({ uid: "test" })
new AuthService().deleteUser("test")
let user
try {
user = await auth.getUser("test")
} catch {}
expect(user).toBe(undefined)
})

it("creates a user", async () => {
const createdUser = await new AuthService().createUser("[email protected]")
let user
try {
user = await auth.getUser(createdUser.uid)
} catch {}
expect(createdUser).toEqual(user)
expect(user.email).toEqual("[email protected]")
})

it("sets custom claim on a user", async () => {
const authService: AuthService = new AuthService()
let createdUser: UserRecord = await authService.createUser("[email protected]")

try {
await authService.setCustomUserClaim(createdUser.uid, "member")
} catch {}

// refresh user record to get access to newly added custom claim
createdUser = await auth.getUser(createdUser.uid)

expect(createdUser.customClaims.member).not.toBe(undefined)
})
})
50 changes: 50 additions & 0 deletions server/src/business-layer/services/AuthService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { UserRecord } from "firebase-admin/auth"
import { auth } from "business-layer/security/Firebase"
import { AuthServiceClaims } from "business-layer/utils/AuthServiceClaims"

export default class AuthService {
/**
* Deletes a user account from the Firebase Authentication Service.
* @param uid
*/
public async deleteUser(uid: string): Promise<void> {
try {
await auth.deleteUser(uid)
} catch (err) {
console.error("Error deleting user", err)
throw err
}
}

/**
* Creates a new user account in the Firebase Authentication Service.
* @param args
* @param claimRole
*/
public async createUser(email: string): Promise<UserRecord> {
// get the user record
let userRecord: UserRecord
try {
userRecord = await auth.createUser({ email })
} catch (err) {
console.error("Error creating user", err)
throw err
}

return userRecord
}

public async setCustomUserClaim(
uid: string,
role: typeof AuthServiceClaims.MEMBER | typeof AuthServiceClaims.ADMIN
) {
let userRecord: UserRecord
try {
userRecord = await auth.getUser(uid)
auth.setCustomUserClaims(userRecord.uid, { [role]: true })
} catch (err) {
console.error("Error setting custom claim on user", err)
throw err
}
}
}
4 changes: 4 additions & 0 deletions server/src/business-layer/utils/AuthServiceClaims.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const AuthServiceClaims = {
MEMBER: "member",
ADMIN: "admin"
} as const

0 comments on commit 45023dd

Please sign in to comment.