Skip to content

Commit f9f3984

Browse files
authored
Merge pull request #160 from JR-prog/156-cannot-login-after-registering-using-oauth
Fixed Google OAuth registration issue
2 parents ba54e21 + 46e7553 commit f9f3984

4 files changed

Lines changed: 35 additions & 44 deletions

File tree

app/page.tsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
'use client';
22

3-
// import { Inter } from '@next/font/google';
4-
// import { Alert } from "react-bootstrap";
53
import { useAuth } from '../contexts/AuthContext';
6-
// import { useEffect, useState } from 'react';
7-
8-
// const inter = Inter({ subsets: ['latin'] });
94

105
const Home = () => {
116
const { currentUser } = useAuth();
12-
// const location = useLocation();
13-
// const ALERT_TIMEOUT = 5000; // Deleted alert timeout in milliseconds
14-
// const [isDeletedAlertShow, setIsDeletedAlertShow] = useState(false);
15-
16-
// TODO reintroduce deleted account alert
17-
/*useEffect(() => {
18-
if (location.state?.deletedAccount) {
19-
setIsDeletedAlertShow(true);
20-
setTimeout(async () => {
21-
setIsDeletedAlertShow(false);
22-
}, ALERT_TIMEOUT);
23-
location.state.deletedAccount = false;
24-
}
25-
}, [location.state])*/
267

278
return (
289
<div className="container mx-auto text-white">
@@ -45,9 +26,6 @@ const Home = () => {
4526
</p>
4627
</>
4728
)}
48-
{/* <Alert variant='danger' show={isDeletedAlertShow}>
49-
Successfully deleted your account.
50-
</Alert> */}
5129
</div>
5230
);
5331
};

components/Card/FullPostCard.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,10 @@ const FullPostCard = ({
2828
testKey?: number;
2929
}) => {
3030
const [commentsOpen, setCommentsOpen] = useState(false);
31-
const [postHeight, setPostHeight] = useState(0);
3231
const [comments, setComments] = useState(post?.comments);
3332

3433
const postContainer = useRef(null);
3534

36-
useEffect(() => {
37-
setTimeout(() => setPostHeight(postContainer.current.clientHeight), 2000);
38-
}, []);
39-
useEffect(() => {
40-
setPostHeight(postContainer.current.clientHeight);
41-
});
42-
4335
return (
4436
<CardGrid
4537
data-testid="card-grid"
@@ -56,7 +48,6 @@ const FullPostCard = ({
5648
id="post-content"
5749
ref={postContainer}
5850
>
59-
{/* <div>{postHeight}</div> */}
6051
<PostHeader
6152
author={author}
6253
authorID={authorID}

config/firebase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ auth.useDeviceLanguage();
2424

2525
// Initialize firestore database
2626
export const firestore = initializeFirestore(app, {
27-
experimentalAutoDetectLongPolling: true,
27+
experimentalAutoDetectLongPolling: !!process.env.NEXT_PUBLIC_EMULATOR,
2828
});
2929
export const storage = getStorage();
3030

contexts/AuthContext.tsx

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import auth from '../config/firebase';
1616
import type { User as AuthUser } from 'firebase/auth';
1717
import type { User } from '../types/User';
1818
import { db } from '../config/firestore';
19-
import { deleteDoc, doc, getDoc, setDoc } from 'firebase/firestore';
19+
import {
20+
deleteDoc,
21+
doc,
22+
getDoc,
23+
getDocFromServer,
24+
setDoc,
25+
} from 'firebase/firestore';
2026
import md5 from 'md5';
2127

2228
interface AuthContextType {
@@ -62,10 +68,8 @@ export function AuthProvider({ children }) {
6268
/**
6369
* Creates user in Firestore and updates current user state
6470
*/
65-
async function createUser(credential: UserCredential) {
71+
async function createUser(newUser: AuthUser) {
6672
// Create a new user document in database using same user id as auth
67-
const newUser = credential.user;
68-
6973
const emptyUser: User = {
7074
awards: [],
7175
certifications: [],
@@ -95,8 +99,10 @@ export function AuthProvider({ children }) {
9599
await setDoc(doc(db.users, newUser.uid), emptyUser);
96100

97101
// Refresh auth user state with signed in user
98-
setAuthUser(credential.user);
102+
setAuthUser(newUser);
99103
setCurrentUser(emptyUser);
104+
105+
console.log('new user created. current and auth user updated.', emptyUser);
100106
}
101107

102108
/**
@@ -109,7 +115,7 @@ export function AuthProvider({ children }) {
109115
password
110116
);
111117

112-
await createUser(credential);
118+
await createUser(credential.user);
113119

114120
return credential;
115121
}
@@ -135,15 +141,19 @@ export function AuthProvider({ children }) {
135141
const credential = await signInWithPopup(auth, googleProvider);
136142

137143
// Get user from database
138-
const userDoc = await getDoc(doc(db.users, credential.user.uid));
144+
const userDoc = await getDocFromServer(doc(db.users, credential.user.uid));
139145

140146
// User exists -> set user as state
141147
if (userDoc.exists()) {
148+
console.log('user doc exists: ', userDoc.data());
149+
142150
setCurrentUser(userDoc.data());
143151
}
144152
// User doesn't exist -> create user
145153
else {
146-
await createUser(credential);
154+
console.log('user doc does not exist. Creating it.');
155+
156+
await createUser(credential.user);
147157
}
148158

149159
return credential;
@@ -157,10 +167,16 @@ export function AuthProvider({ children }) {
157167
await auth.currentUser.reload();
158168

159169
// Refresh current user
160-
const userDoc = await getDoc(doc(db.users, auth.currentUser.uid));
161-
setCurrentUser(userDoc.data());
170+
if (auth.currentUser) {
171+
const userDoc = await getDoc(doc(db.users, auth.currentUser.uid));
172+
if (!userDoc.exists()) {
173+
await createUser(auth.currentUser);
174+
} else {
175+
setCurrentUser(userDoc.data());
176+
}
162177

163-
return userDoc.data();
178+
return userDoc.data();
179+
}
164180
}
165181

166182
/**
@@ -200,6 +216,8 @@ export function AuthProvider({ children }) {
200216
// On first load of the page, prepare an unsubscribe function
201217
useEffect(() => {
202218
const unsubscribe = auth.onAuthStateChanged((user) => {
219+
console.log('auth state changed triggered!');
220+
203221
setLoading(true);
204222

205223
setAuthUser(user);
@@ -213,7 +231,11 @@ export function AuthProvider({ children }) {
213231
// Ensure user data is loaded if user logged in
214232
else if (!currentUser) {
215233
getDoc(doc(db.users, user.uid)).then((res) => {
216-
setCurrentUser(res.data());
234+
if (!res.exists()) {
235+
createUser(auth.currentUser);
236+
} else {
237+
setCurrentUser(res.data());
238+
}
217239
setLoading(false);
218240
});
219241
}

0 commit comments

Comments
 (0)