Skip to content

Commit 3941988

Browse files
authored
fix: auth key errors (#411)
1 parent 891eef2 commit 3941988

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

client/src/config/ApolloClient.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@apollo/client';
1010
import { setContext } from '@apollo/client/link/context';
1111
import { onError } from '@apollo/client/link/error';
12+
import { parseCookies } from 'nookies';
1213

1314
const cache = new InMemoryCache();
1415

@@ -43,10 +44,11 @@ const link = from([
4344
]);
4445

4546
const getApolloLink = (token) => {
47+
const cookies = parseCookies();
4648
const authLink = setContext((_, { headers }) => ({
4749
headers: {
4850
...headers,
49-
Authorization: token ? token : '',
51+
Authorization: token || cookies.firebaseToken || '',
5052
},
5153
}));
5254
return authLink.concat(link);

client/src/context/auth/AuthState.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,19 @@ const AuthState = ({ children }) => {
4545
sameSite: true,
4646
maxAge: 3600,
4747
});
48-
}, [firebaseToken]);
48+
}, [user, firebaseToken, refreshToken]);
4949

5050
const loginWithToken = async (_token) => {
5151
const credential = GoogleAuthProvider.credential(_token);
5252

5353
const res = await signInWithCredential(auth, credential);
5454

5555
const { user, _tokenResponse } = res;
56-
setUser(user);
56+
57+
GraphClient.setLink(getApolloLink(user.accessToken));
5758
setFirebaseToken(user.accessToken);
5859
setRefreshToken(user.refreshToken);
60+
setUser(user);
5961

6062
if (getAdditionalUserInfo(res).isNewUser) {
6163
try {
@@ -65,6 +67,15 @@ const AuthState = ({ children }) => {
6567
authenticationEndpoint: `${process.env.NEXT_PUBLIC_SERVER_ADDRESS}${process.env.NEXT_PUBLIC_IMAGEKIT_AUTHENTICATION_ENDPOINT}`,
6668
});
6769

70+
const newAccount = await GraphClient.mutate({
71+
mutation: registerUser,
72+
variables: {
73+
fullName: user.displayName,
74+
email: user.email,
75+
},
76+
});
77+
console.log('Account Created ', newAccount);
78+
6879
const userPicture = await (await fetch(user.photoURL)).blob();
6980

7081
if (!['image/png', 'image/jpeg'].includes(userPicture.type)) {
@@ -73,25 +84,16 @@ const AuthState = ({ children }) => {
7384

7485
const imageUpload = await imagekit
7586
.upload({
76-
file: userPicture,
87+
file: user.photoURL,
7788
folder: '/user',
78-
fileName: `${user.uid}.${
89+
fileName: `${newAccount.data.registerUser.id}.${
7990
userPicture.type.toString().split('/')[1]
8091
}`,
8192
tags: [user.uid, 'user', 'profilePicture'],
8293
})
8394
.then((result) => {
8495
console.log('Upload Success', result);
8596
});
86-
87-
const newAccount = await GraphClient.mutate({
88-
mutation: registerUser,
89-
variables: {
90-
fullName: user.displayName,
91-
email: user.email,
92-
},
93-
});
94-
console.log('Account Created ', newAccount);
9597
} catch (err) {
9698
console.log(err);
9799
}

0 commit comments

Comments
 (0)