Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#340 add authentication pages using firebase UI #346

Closed
wants to merge 18 commits into from

Conversation

detautama
Copy link
Contributor

Description

Implement authentication for the Tanam web application using firebaseui-web. The authentication should only support sign-in with Google. The site should be accessible only if the user is signed in, and upon successful sign-in, the user should be redirected to the landing page.

Requirements

🛑 FirebaseUI Implementation: I think we don't need this. We can create sign in ui by ourself

Access Control

✅ Ensure the site is accessible only to authenticated users.
✅ Redirect unauthenticated users to the sign-in page.

Post-Sign-In Redirection

✅ Redirect signed-in users to the landing page.

Referenced issues

Closes #340

}
return result.user.uid;
} catch (error) {
console.error("Error signing in with Google", error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if we could display information to the user with a popup message when an error occurs and display an error message in the browser console just for developer debugging.
So the user can find out whether the process was a success or an error

Suggested change
console.error("Error signing in with Google", error);
Snackbar.error(new Error('Login failed'))
console.error(error);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever possible, make an error service that wrap the error UI behavior and console log, so that we only call one method and can do customization in one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in this commit

Image example on error handling ⬇️
image

hosting/libs/firebase/auth.ts Outdated Show resolved Hide resolved
hosting/libs/firebase/auth.ts Outdated Show resolved Hide resolved
try {
await firebaseAuth.signOut();
} catch (error) {
console.error("Error signing out with Google", error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if we could handle this error condition and show some message to user with snackbar

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be helpful. Just avoid putting the details of the message from the error in snackbar. The current console.error is good as it is. But it would indeed be helpful to get a snackbar informing that something went wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in this commit

Image example on error handling ⬇️
image

appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};

const firebaseApp =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can optimizing this line code

Suggested change
const firebaseApp =
const firebaseApp = initializeApp(firebaseConfig)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the way it's implemented in the docs of Firebase Frameworks Hosting

// get the authenticated Firebase App
const firebaseApp = getApp(useRouter().query.__firebaseAppName);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DennisAlund I got this error with your solution

Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'string | undefined'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will go with @muzanella11 solution. Updated on this commit

Comment on lines 3 to 4
import { createSession } from "../../../../../actions/auth-action";
import { signInWithGoogle } from "../../../../../libs/firebase/auth";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long and messy import. It would be better to use path alias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in this commit

Comment on lines 4 to 5
import { SignOutWithGoogle } from "./components/SignOutWithGoogle";
import { firebaseAuth } from "../../../../libs/firebase/config";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long and messy import. It would be better to use path alias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in this commit

Comment on lines 2 to 3
import { removeSession } from "../../../../../actions/auth-action";
import { signOutWithGoogle } from "../../../../../libs/firebase/auth";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long and messy import. It would be better to use path alias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in this commit

Comment on lines 2 to 5
import DarkModeSwitcher from "./DarkModeSwitcher";
import DropdownMessage from "./DropdownMessage";
import DropdownNotification from "./DropdownNotification";
import DropdownUser from "./DropdownUser";
import DropdownUser from "./DropdownUser/DropdownUser";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use path alias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in this commit

Comment on lines 2 to 5
import React, { useEffect, useState } from "react";
import Sidebar from "@/components/Sidebar";
import Header from "@/components/Header";
import Loader from "../common/Loader";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long and messy import. It would be better to use path alias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in this commit

Copy link
Member

@DennisAlund DennisAlund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation doesn't look like what I expected from using FirebaseUI: https://firebase.google.com/docs/auth/web/firebaseui

Is there a reason for doing manual buttons and handling?

appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};

const firebaseApp =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the way it's implemented in the docs of Firebase Frameworks Hosting

// get the authenticated Firebase App
const firebaseApp = getApp(useRouter().query.__firebaseAppName);

const [loading, setLoading] = useState<boolean>(true);

useEffect(() => {
setTimeout(() => setLoading(false), 2000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming this loading part is implemented as a placeholder to show usage for later when we are loading data from Firestore?

Please consider either of the following options

  1. Add a comment such as // Fetch your data here and set loading to false when done
  2. Remove the loading things until we're loading data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I choose number 1
Updated in this commit

@detautama detautama linked an issue May 22, 2024 that may be closed by this pull request
@detautama
Copy link
Contributor Author

This implementation doesn't look like what I expected from using FirebaseUI: https://firebase.google.com/docs/auth/web/firebaseui

Is there a reason for doing manual buttons and handling?

We already have it all: A page to show sign in button and a button to sign in with google account, that's a reason. I didn't find any reason to use firebaseUI. Or are there something that I missed from FirebaseUI?

@detautama detautama assigned detautama and unassigned detaoddbit May 22, 2024
# Conflicts:
#	hosting/src/app/auth/signin/page.tsx
#	hosting/src/app/auth/signup/page.tsx
#	hosting/src/app/layout.tsx
#	hosting/src/components/Header/DropdownUser/DropdownUser.tsx
#	hosting/src/components/Header/index.tsx
#	hosting/src/components/Layouts/DefaultLayout.tsx
#	hosting/src/components/Maps/MapOne.tsx
#	hosting/src/firebase.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hook files should be camelCase. Please rename this one to useUserSession.tsx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this one won't be needed if we are using Firebase UI for auth? 🙏

import {removeSession} from "@/actions/auth-action";
import {signOutWithGoogle} from "@/libs/firebase/auth";

export const SignOutWithGoogle = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename the operation to just "sign out" since the sign out flow will be the same for all providers when we add more in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the auth action in terms of Next terminology? Can you help me with a pointer to the docs for me to learn what pattern this is 🙏

Copy link

Visit the preview URL for this PR (updated for commit d4496a8):

https://tanam-testing--pr346-340-add-authenticat-2uc7akdn.web.app

(expires Fri, 31 May 2024 23:33:30 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 5bbe8a1a68d9684d656bffba10008fe532204561

@DennisAlund
Copy link
Member

Replaced by #354

@DennisAlund DennisAlund deleted the #340-Add-Authentication-Pages-using-FirebaseUI branch May 27, 2024 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Authentication Pages using FirebaseUI
5 participants