Skip to content

Commit

Permalink
Add guards for testing on dev environment and allowing logins with re…
Browse files Browse the repository at this point in the history
…gular google accounts.
  • Loading branch information
nimanns committed Dec 8, 2024
1 parent 5efadb6 commit 8b0e504
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
12 changes: 12 additions & 0 deletions booking-app/app/api/isTestEnv/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NextResponse } from "next/server";

export async function GET() {
try {
if(process.env.NEXT_PUBLIC_BRANCH_NAME === "development") {
return NextResponse.json({ isOnTestEnv: true });
}
} catch (error) {
}

return NextResponse.json({ isOnTestEnv: false });
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ type AuthContextType = {
user: User | null;
loading: boolean;
error: string | null;
isOnTestEnv: boolean;
};

const AuthContext = createContext<AuthContextType>({
user: null,
loading: true,
error: null,
isOnTestEnv: false,
});

export const useAuth = () => useContext(AuthContext);
Expand All @@ -25,13 +27,20 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isOnTestEnv, setIsOnTestEnv] = useState<boolean>(false);

const router = useRouter();
const pathname = usePathname();

useEffect(() => {
const handleAuth = async () => {
const testEnvRes = await fetch("/api/isTestEnv");
const { isOnTestEnv } = await testEnvRes.json();
setIsOnTestEnv(isOnTestEnv);

const user = auth.currentUser;
if (user) {
if (user.email?.endsWith("@nyu.edu")) {
if (user.email?.endsWith("@nyu.edu") || isOnTestEnv) {
setUser(user);
} else {
await auth.signOut();
Expand Down Expand Up @@ -62,7 +71,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
}, [error, router]);

return (
<AuthContext.Provider value={{ user, loading, error }}>
<AuthContext.Provider value={{ user, loading, error, isOnTestEnv }}>
{children}
</AuthContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";
import React from "react"; // Added this line
import React, { useContext } from "react"; // Added this line
import { useState } from "react";
import { signInWithPopup } from "firebase/auth";
import { auth, googleProvider } from "@/lib/firebase/firebaseClient";
import { useRouter } from "next/navigation";
import { Box, Button, styled } from "@mui/material";
import { useAuth } from "./AuthProvider";

const Center = styled(Box)`
display: flex;
Expand All @@ -20,7 +21,8 @@ const GoogleSignIn = () => {
try {
const result = await signInWithPopup(auth, googleProvider);
const user = result.user;
if (user.email?.endsWith("@nyu.edu")) {
const {isOnTestEnv} = useAuth();
if (user.email?.endsWith("@nyu.edu") || isOnTestEnv) {
console.log("Google sign-in successful", user);
router.push("/");
} else {
Expand Down
11 changes: 7 additions & 4 deletions booking-app/lib/firebase/firebaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ export const getDb = () => {
};
export const auth = getAuth(app);
export const googleProvider = new GoogleAuthProvider();
googleProvider.setCustomParameters({
hd: "nyu.edu",
});

if(process.env.NEXT_PUBLIC_BRANCH_NAME !== "development") {
googleProvider.setCustomParameters({
hd: "nyu.edu",
});
}

export const signInWithGoogle = async () => {
try {
const result = await signInWithPopup(auth, googleProvider);
const user = result.user;
if (!user.email?.endsWith("@nyu.edu")) {
if (!user.email?.endsWith("@nyu.edu") && process.env.NEXT_PUBLIC_BRANCH_NAME !== "development") {
await auth.signOut();
throw new Error("Only nyu.edu email addresses are allowed.");
}
Expand Down
1 change: 1 addition & 0 deletions booking-app/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default defineConfig({
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
headless:false,
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

Expand Down
8 changes: 6 additions & 2 deletions booking-app/tests/sample.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { test, expect } from '@playwright/test';

test('search for text Media Commons', async ({ page }) => {
test('Search for text Media Commons in the title', async ({ page }) => {
await page.goto('http://localhost:3000');

//make sure there is an h4 element with the text "Media Commons"

await expect(page).toHaveTitle('Media commons booking app');
});
});

test('Go through the booking process', async ({ page }) => {
await page.goto('http://localhost:3000');
});

0 comments on commit 8b0e504

Please sign in to comment.