Skip to content

Commit

Permalink
Merge pull request #977 from chaynHQ/develop
Browse files Browse the repository at this point in the history
Merge Develop onto Main
  • Loading branch information
annarhughes authored Jun 18, 2024
2 parents c71118b + c5d3dc0 commit 8cab9d1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 37 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/new-assignee-comment.yml

This file was deleted.

19 changes: 14 additions & 5 deletions app/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
FetchBaseQueryError,
} from '@reduxjs/toolkit/query/react';
import { getAuth } from 'firebase/auth';
import { PARTNER_ACCESS_CODE_STATUS } from '../constants/enums';
import { delay } from '../utils/delay';
import { EVENT_LOG_NAME, PARTNER_ACCESS_CODE_STATUS } from '../constants/enums';
import { EventLog } from '../constants/eventLog';
import { Course, Courses } from './coursesSlice';
import { PartnerAccess, PartnerAccesses } from './partnerAccessSlice';
import { PartnerAdmin } from './partnerAdminSlice';
import { Partner, PartnerFeature } from './partnersSlice';
import { AppState } from './store';
import { Subscription, Subscriptions, User } from './userSlice';
import { setUserToken, Subscription, Subscriptions, User } from './userSlice';

export interface GetUserResponse {
user: User;
Expand Down Expand Up @@ -56,8 +56,7 @@ const baseQueryWithReauth: BaseQueryFn<string | FetchArgs, unknown, FetchBaseQue
const token = await auth.currentUser?.getIdToken(true);

if (token) {
// allow time for new token to update in state
await delay(200);
await api.dispatch(setUserToken(token));
// retry the initial query
result = await baseQuery(args, api, extraOptions);
}
Expand Down Expand Up @@ -207,6 +206,15 @@ export const api = createApi({
}),
},
),
createEventLog: builder.mutation<EventLog, { event: EVENT_LOG_NAME }>({
query(body) {
return {
url: 'event-logger',
method: 'POST',
body,
};
},
}),
}),
});

Expand All @@ -227,4 +235,5 @@ export const {
useSubscribeToWhatsappMutation,
useUnsubscribeFromWhatsappMutation,
useUpdatePartnerAccessMutation,
useCreateEventLogMutation,
} = api;
5 changes: 5 additions & 0 deletions components/forms/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';
import { useTranslations } from 'next-intl';
import * as React from 'react';
import { useEffect, useState } from 'react';
import { useCreateEventLogMutation } from '../../app/api';
import { setAuthStateLoading } from '../../app/userSlice';
import { EVENT_LOG_NAME } from '../../constants/enums';
import {
GET_LOGIN_USER_ERROR,
GET_LOGIN_USER_REQUEST,
Expand Down Expand Up @@ -37,6 +39,8 @@ const LoginForm = () => {
const [emailInput, setEmailInput] = useState<string>('');
const [passwordInput, setPasswordInput] = useState<string>('');

const [createEventLog] = useCreateEventLogMutation();

useEffect(() => {
if (userId) {
logEvent(GET_LOGIN_USER_SUCCESS);
Expand Down Expand Up @@ -65,6 +69,7 @@ const LoginForm = () => {

signInWithEmailAndPassword(auth, emailInput, passwordInput)
.then(async (userCredential) => {
createEventLog({ event: EVENT_LOG_NAME.LOGGED_IN });
await dispatch(setAuthStateLoading(false)); // important - triggers getUser in useLoadUser
logEvent(LOGIN_SUCCESS);
logEvent(GET_LOGIN_USER_REQUEST);
Expand Down
10 changes: 4 additions & 6 deletions components/forms/ResetPasswordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ export const EmailForm = () => {
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;

logEvent(RESET_PASSWORD_ERROR, { message: errorCode });
(window as any).Rollbar?.error('User send reset password email firebase error', error);

if (errorCode === 'auth/invalid-email') {
setFormError(t('firebase.invalidEmail'));
}
if (errorCode === 'auth/user-not-found') {
} else if (errorCode === 'auth/user-not-found') {
setFormError(t('firebase.authError'));
} else {
logEvent(RESET_PASSWORD_ERROR, { message: errorCode });
(window as any).Rollbar?.error('User send reset password email firebase error', error);
}
});
};
Expand Down
1 change: 1 addition & 0 deletions components/head/RollbarScript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var _rollbarConfig = {
accessToken: "${process.env.NEXT_PUBLIC_ROLLBAR_CLIENT_TOKEN}",
captureUncaught: true,
captureUnhandledRejections: true,
captureIP: "anonymize",
payload: {
environment: "${process.env.NEXT_PUBLIC_ENV}",
client: {
Expand Down
6 changes: 6 additions & 0 deletions constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ export enum ENVIRONMENT {
export enum FEATURES {
AUTOMATIC_ACCESS_CODE = 'AUTOMATIC_ACCESS_CODE',
}

export enum EVENT_LOG_NAME {
CHAT_MESSAGE_SENT = 'CHAT_MESSAGE_SENT',
LOGGED_IN = 'LOGGED_IN',
LOGGED_OUT = 'LOGGED_OUT',
}
12 changes: 12 additions & 0 deletions constants/eventLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EVENT_LOG_NAME } from './enums';

export interface EventLog {
createdAt: string;
updatedAt: string;
id: string;
date: string;
event: {
event: EVENT_LOG_NAME;
};
userId: string;
}
11 changes: 7 additions & 4 deletions hooks/useLoadUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { getAuth, onIdTokenChanged, signOut } from 'firebase/auth';
import { useEffect } from 'react';
import { useGetUserQuery } from '../app/api';
import { useCreateEventLogMutation, useGetUserQuery } from '../app/api';
import { setAuthStateLoading, setLoadError, setUserLoading, setUserToken } from '../app/userSlice';
import { EVENT_LOG_NAME } from '../constants/enums';
import {
GET_AUTH_USER_ERROR,
GET_AUTH_USER_SUCCESS,
Expand All @@ -23,26 +24,28 @@ export default function useLoadUser() {
const userToken = useTypedSelector((state) => state.user.token);
const userAuthLoading = useTypedSelector((state) => state.user.authStateLoading);
const { clearState } = useStateUtils();
const [createEventLog] = useCreateEventLogMutation();

// 1. Listen for firebase auth state or auth token updated, triggered by firebase auth loaded
// When a user token is available, set the token in state to be used in request headers
useEffect(() => {
const unsubscribe = onIdTokenChanged(auth, async (firebaseUser) => {
const token = await firebaseUser?.getIdToken();
if (token) {
// User logged in or started a new authenticated session
// User logged in or started a new authenticated session - state changes trigger call to get user record
await dispatch(setUserToken(token));
await dispatch(setUserLoading(true));
logEvent(GET_USER_REQUEST); // deprecated event
} else if (!firebaseUser && userToken) {
// User logged out or token was removed, clear state
await clearState();
createEventLog({ event: EVENT_LOG_NAME.LOGGED_OUT });
logEvent(LOGOUT_SUCCESS);
await clearState();
}
await dispatch(setAuthStateLoading(false)); // triggers step 2
});
return () => unsubscribe();
}, [userToken, auth, dispatch, clearState]);
}, [userToken, auth, dispatch, clearState, createEventLog]);

// 2. Once firebase auth is complete, get the user database resource
// skip property prevents the API query being called unless there is a user token and the user is not already set
Expand Down

0 comments on commit 8cab9d1

Please sign in to comment.