Skip to content

Commit fa616c4

Browse files
committed
fix: Code reviews.
1 parent f563614 commit fa616c4

7 files changed

Lines changed: 53 additions & 37 deletions

File tree

src/config/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ export const dateRanges: DateRangeType[] = [
5050
{ label: 'the week', value: 'weekly' },
5151
{ label: 'the month', value: 'monthly' },
5252
]
53+
54+
export const FIREBASE_API_KEY = import.meta.env.VITE_FIREBASE_API_KEY as string
55+
export const FIREBASE_AUTH_DOMAIN = import.meta.env.VITE_FIREBASE_AUTH_DOMAIN as string
56+
export const FIREBASE_PROJECT_ID = import.meta.env.VITE_FIREBASE_PROJECT_ID as string
57+
export const FIREBASE_STORAGE_BUCKET = import.meta.env.VITE_FIREBASE_STORAGE_BUCKET as string
58+
export const FIREBASE_MESSAGING_SENDER_ID = import.meta.env
59+
.VITE_FIREBASE_MESSAGING_SENDER_ID as string
60+
export const FIREBASE_APP_ID = import.meta.env.VITE_FIREBASE_APP_ID as string
61+
export const FIREBASE_MEASUREMENT_ID = import.meta.env.VITE_FIREBASE_MEASUREMENT_ID as string

src/features/auth/api/FirebaseConfig.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/features/auth/components/AuthModal.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { FaGithub, FaGoogle } from 'react-icons/fa'
33
import ReactModal from 'react-modal'
44
import toast from 'react-simple-toasts'
55
import { useAuth } from 'src/features/auth'
6-
import { auth, githubProvider, googleProvider } from '../api/FirebaseConfig'
6+
import { firebaseAuth, githubAuthProvider, googleAuthProvider } from 'src/lib/firebase'
77

88
type AuthModalProps = {
99
showAuth: boolean
@@ -14,17 +14,17 @@ export const AuthModal = ({ showAuth, closeModal }: AuthModalProps) => {
1414
const { closeAuthModal, initState } = useAuth()
1515

1616
const signIn = (provider: AuthProvider, providerName: string) => {
17-
signInWithPopup(auth, provider)
17+
signInWithPopup(firebaseAuth, provider)
1818
.then((result) => {
1919
const credential = OAuthProvider.credentialFromResult(result)
20-
const accessToken = credential?.accessToken
20+
const idToken = credential?.idToken
2121
const email = result.user.displayName
2222
const name = result.user.displayName
2323
const imageURL = result.user.photoURL
24-
if (accessToken && name && email && imageURL) {
24+
if (idToken && name && email && imageURL) {
2525
closeAuthModal()
2626
initState({
27-
accessToken: accessToken,
27+
idToken: idToken,
2828
user: {
2929
name: name,
3030
email: email,
@@ -65,14 +65,16 @@ export const AuthModal = ({ showAuth, closeModal }: AuthModalProps) => {
6565
</p>
6666
</div>
6767
<div>
68-
<button className="extraTextWithIconBtn" onClick={() => signIn(githubProvider, 'Github')}>
68+
<button
69+
className="extraTextWithIconBtn"
70+
onClick={() => signIn(githubAuthProvider, 'Github')}>
6971
<FaGithub />
7072
Sign in with Github
7173
</button>
7274
<button
7375
className="extraTextWithIconBtn"
7476
style={{ marginLeft: 10 }}
75-
onClick={() => signIn(googleProvider, 'Google')}>
77+
onClick={() => signIn(googleAuthProvider, 'Google')}>
7678
<FaGoogle />
7779
Sign in with Google
7880
</button>

src/features/auth/hooks/useAuth.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { AuthModalStore, AuthStore } from 'src/features/auth'
2-
import { useBookmarks } from 'src/stores/bookmarks'
32

43
export const useAuth = () => {
54
const { isAuthModalOpen, openAuthModal, closeAuthModal } = AuthModalStore()
65
const authStore = AuthStore()
7-
const bookmarksStore = useBookmarks()
8-
const { accessToken, user, initState } = authStore
6+
const { idToken, user, initState, clear } = authStore
97
const isConnected = () => user != null
108
const logout = () => {
11-
bookmarksStore.clear()
12-
authStore.clear()
9+
clear()
1310
}
1411

1512
return {
@@ -19,7 +16,7 @@ export const useAuth = () => {
1916
isConnected,
2017
logout,
2118
isAuthModalOpen,
22-
accessToken,
19+
idToken,
2320
user,
2421
}
2522
}

src/features/auth/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './api/FirebaseConfig'
21
export * from './components/AuthModal'
32
export * from './hooks/useAuth'
43
export * from './stores/authStore'

src/features/auth/stores/authStore.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface AuthModalState {
99
}
1010

1111
type AuthState = {
12-
accessToken: string | null
12+
idToken: string | null
1313
user: User | null
1414
}
1515

@@ -27,11 +27,11 @@ export const AuthModalStore = create<AuthModalState>((set) => ({
2727
export const AuthStore = create(
2828
persist<AuthState & AuthActions>(
2929
(set) => ({
30-
accessToken: null,
30+
idToken: null,
3131
user: null,
3232
initState: (newState: AuthState) =>
3333
set({
34-
accessToken: newState.accessToken,
34+
idToken: newState.idToken,
3535
user: newState.user,
3636
}),
3737
clear: () => set({ user: null }),

src/lib/firebase.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { initializeApp } from 'firebase/app'
2+
import { getAuth, GithubAuthProvider, GoogleAuthProvider } from 'firebase/auth'
3+
import {
4+
FIREBASE_API_KEY,
5+
FIREBASE_APP_ID,
6+
FIREBASE_AUTH_DOMAIN,
7+
FIREBASE_MEASUREMENT_ID,
8+
FIREBASE_MESSAGING_SENDER_ID,
9+
FIREBASE_PROJECT_ID,
10+
FIREBASE_STORAGE_BUCKET,
11+
} from 'src/config'
12+
13+
const firebaseConfig = {
14+
apiKey: FIREBASE_API_KEY,
15+
authDomain: FIREBASE_AUTH_DOMAIN,
16+
projectId: FIREBASE_PROJECT_ID,
17+
storageBucket: FIREBASE_STORAGE_BUCKET,
18+
messagingSenderId: FIREBASE_MESSAGING_SENDER_ID,
19+
appId: FIREBASE_APP_ID,
20+
measurementId: FIREBASE_MEASUREMENT_ID,
21+
}
22+
23+
// Initialize Firebase
24+
const app = initializeApp(firebaseConfig)
25+
const firebaseAuth = getAuth(app)
26+
const googleAuthProvider = new GoogleAuthProvider()
27+
const githubAuthProvider = new GithubAuthProvider()
28+
29+
export { firebaseAuth, githubAuthProvider, googleAuthProvider }

0 commit comments

Comments
 (0)