Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): always identify to posthog #1900

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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