Skip to content

Commit

Permalink
fix: auth key errors (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
rutajdash authored Feb 6, 2023
1 parent 891eef2 commit 3941988
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
4 changes: 3 additions & 1 deletion client/src/config/ApolloClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
import { onError } from '@apollo/client/link/error';
import { parseCookies } from 'nookies';

const cache = new InMemoryCache();

Expand Down Expand Up @@ -43,10 +44,11 @@ const link = from([
]);

const getApolloLink = (token) => {
const cookies = parseCookies();
const authLink = setContext((_, { headers }) => ({
headers: {
...headers,
Authorization: token ? token : '',
Authorization: token || cookies.firebaseToken || '',
},
}));
return authLink.concat(link);
Expand Down
28 changes: 15 additions & 13 deletions client/src/context/auth/AuthState.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ const AuthState = ({ children }) => {
sameSite: true,
maxAge: 3600,
});
}, [firebaseToken]);
}, [user, firebaseToken, refreshToken]);

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

const res = await signInWithCredential(auth, credential);

const { user, _tokenResponse } = res;
setUser(user);

GraphClient.setLink(getApolloLink(user.accessToken));
setFirebaseToken(user.accessToken);
setRefreshToken(user.refreshToken);
setUser(user);

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

const newAccount = await GraphClient.mutate({
mutation: registerUser,
variables: {
fullName: user.displayName,
email: user.email,
},
});
console.log('Account Created ', newAccount);

const userPicture = await (await fetch(user.photoURL)).blob();

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

const imageUpload = await imagekit
.upload({
file: userPicture,
file: user.photoURL,
folder: '/user',
fileName: `${user.uid}.${
fileName: `${newAccount.data.registerUser.id}.${
userPicture.type.toString().split('/')[1]
}`,
tags: [user.uid, 'user', 'profilePicture'],
})
.then((result) => {
console.log('Upload Success', result);
});

const newAccount = await GraphClient.mutate({
mutation: registerUser,
variables: {
fullName: user.displayName,
email: user.email,
},
});
console.log('Account Created ', newAccount);
} catch (err) {
console.log(err);
}
Expand Down

0 comments on commit 3941988

Please sign in to comment.