Skip to content

Commit

Permalink
fix(ui): always identify to posthog (#1900)
Browse files Browse the repository at this point in the history
## Describe your changes

Fixes NAN-636

- Always call identify 
Some users are not identified correctly in PostHog. Looking at their
session it seems that they didn't go through sign-in, so I might be due
to the posthog cookie being deleted but not the nango session one.

- Fix inconsistent types

- Catch export types error
  • Loading branch information
bodinsamuel committed Mar 25, 2024
1 parent ba21794 commit a017d6a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@typescript-eslint/no-invalid-void-type": "warn",
"@typescript-eslint/no-base-to-string": "warn",
"@typescript-eslint/restrict-plus-operands": "warn",
"@typescript-eslint/consistent-type-exports": "error",
"react/prop-types": "off",
"@typescript-eslint/no-unused-vars": [
"error",
Expand Down
11 changes: 10 additions & 1 deletion packages/server/lib/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ import type { Request, Response, NextFunction } from 'express';
import EmailClient from '../clients/email.client.js';
import { errorManager, userService, getBaseUrl, isCloud, isEnterprise } from '@nangohq/shared';

export interface GetUser {
user: {
id: number;
accountId: number;
email: string;
name: string;
};
}

class UserController {
async getUser(req: Request, res: Response, next: NextFunction) {
async getUser(req: Request, res: Response<GetUser>, next: NextFunction) {
try {
const { success, error, response } = await getUserAccountAndEnvironmentFromSession(req);
if (!success || response === null) {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { GetMeta } from './controllers/environment.controller.js';
export type { GetMeta } from './controllers/environment.controller.js';
export type { GetUser } from './controllers/user.controller.js';
10 changes: 10 additions & 0 deletions packages/webapp/src/components/PrivateRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { Outlet, Navigate } from 'react-router-dom';
import { useMeta } from '../hooks/useMeta';
import { useEffect } from 'react';
import { useStore } from '../store';
import { useAnalyticsIdentify } from '../utils/analytics';
import { useUser } from '../hooks/useUser';

const PrivateRoute: React.FC = () => {
const { meta, error, loading } = useMeta();
const { user } = useUser();
const identify = useAnalyticsIdentify();

const setStoredEnvs = useStore((state) => state.setEnvs);
const setBaseUrl = useStore((state) => state.setBaseUrl);
Expand All @@ -23,6 +27,12 @@ const PrivateRoute: React.FC = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [meta, error]);

useEffect(() => {
if (user) {
identify(user);
}
}, [user, identify]);

if (loading) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/webapp/src/hooks/useUser.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import useSWR from 'swr';
import { User } from '../types';
import { swrFetcher } from '../utils/api';
import type { GetUser } from '@nangohq/server';

export function useUser() {
const { data, error, mutate } = useSWR<{ user: User }>('/api/v1/user', swrFetcher);
const { data, error, mutate } = useSWR<GetUser>('/api/v1/user', swrFetcher);

const loading = !data && !error;

Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/src/utils/analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { usePostHog } from 'posthog-js/react';
import { User } from './user';
import type { User } from './user';

export function useAnalyticsTrack() {
const posthog = usePostHog();
Expand Down

0 comments on commit a017d6a

Please sign in to comment.