Skip to content

Commit

Permalink
refactor(research): updated the get and post user to have x-client-id
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldsouzax committed Sep 22, 2023
1 parent bd5dc16 commit 40d206d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { Title, Text } = Typography;
const { useToken } = theme;

const features = [
'The weekly market report keeping you up to speed',
'Ahead of the Curve – The Weekly Market Report',
'The monthly outlook helping you plan ahead',
'Token assessments based on data and economic theory',
'Industry insights from our in-depth reports',
Expand Down
13 changes: 13 additions & 0 deletions apps/research/src/services/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ export const researchApi = createApi({
baseQuery: fetchBaseQuery({
baseUrl: `https://${process.env.NEXT_PUBLIC_API_DOMAIN}`,
prepareHeaders: async (headers) => {
//@ts-ignore
window.gtag(
'get',
process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID!,
'client_id',
(clientId: string) => {
// TODO: check if localstorage says cookie-product yes
const check = localStorage.getItem('cookies-product');
if (check === 'YES') {
headers.set('x-client-id', `${clientId}`);
}
}
);
const token = await getIdToken();
headers.set('Content-Type', 'application/json');
if (token) {
Expand Down
70 changes: 61 additions & 9 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ export const acceptCookie = () => {
//@ts-ignore
localStorage.setItem('showCookies', 'NO');
//@ts-ignore
localStorage.setItem('cookies-product', 'YES');
//@ts-ignore
window.gtag('consent', 'update', {
ad_storage: 'denied',
analytics_storage: 'granted',
Expand All @@ -207,6 +209,8 @@ export const acceptCookie = () => {
export const denyCookie = () => {
//@ts-ignore
localStorage.setItem('showCookies', 'NO');
//@ts-ignore
localStorage.setItem('cookies-product', 'NO');
};

// returns a boolean
Expand All @@ -223,6 +227,9 @@ interface AppConfig {

export const getAppState = async (config?: AppConfig) => {
try {



const data = await fetcher(
`https://${process.env.NEXT_PUBLIC_API_DOMAIN}/user`
);
Expand All @@ -236,6 +243,7 @@ export const getAppState = async (config?: AppConfig) => {
return REGISTRED;
}
} catch (err) {
console.log(err);
return SIGNED_OUT;
}
};
Expand All @@ -248,28 +256,54 @@ export const register = async (config?: AppConfig) => {
{}
);
const user = await data.json();

console.log(user);
// register analytics
if (config) {
registerAnalytics(user.analyticsId, config.tagId);
}
return REGISTRED;
} catch (err) {
console.log(err);
return SIGNED_OUT;
}
};

// auth fetcher for api calls
export const fetcher = async (url: string) => {
const token = await getIdToken();

//@ts-ignore

if (token) {
//@ts-ignore
const headers = new Headers({
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
});

//@ts-ignore
window.gtag(
'get',
process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID!,
'client_id',
(clientId: string) => {
//@ts-ignore
const check = localStorage.getItem('cookies-product');
//@ts-ignore
if (check === 'YES') {
console.log(clientId);
headers.set('x-client-id', `${clientId}`);
}
}
);

console.log(headers.entries());

//@ts-ignore
return fetch(url, {
method: 'GET',
//@ts-ignore
headers: new Headers({
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
}),
headers,
});
} else {
return Promise.reject(new Error('user not authenticated'));
Expand All @@ -283,15 +317,33 @@ export const mutator = async <T extends object>(
) => {
const token = await getIdToken();
if (token) {
//@ts-ignore
const headers = new Headers({
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
});
//@ts-ignore
window.gtag(
'get',
process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID!,
'client_id',
(clientId: string) => {
//@ts-ignore
const check = localStorage.getItem('cookies-product');

if (check === 'YES') {
console.log(clientId);
headers.set('x-client-id', `${clientId}`);
}
}
);

//@ts-ignore
return fetch(url, {
method,
body: JSON.stringify(body),
//@ts-ignore
headers: new Headers({
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
}),
headers: headers,
});
} else {
return Promise.reject(new Error('user not authenticated'));
Expand Down

0 comments on commit 40d206d

Please sign in to comment.