Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions frontend/src/app/(auth)/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { env } from '~/env';
import { loginUser } from './actions';
import { authConfig } from './auth.config';
import { loginFormSchema } from './schema';
import { UserInterface } from '~/types/user';
import { UserInterface, UserBase } from '~/types/user';
import type { DefaultJWT } from 'next-auth/jwt';
import NextAuth, { type DefaultSession } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
Expand All @@ -12,7 +12,8 @@ declare module 'next-auth' {
user: UserInterface & DefaultSession['user'] & { access_token: string };
}

interface User extends UserInterface {
interface User extends UserBase {
email: string;
access_token: string;
}
}
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import './globals.css';
import { Suspense } from 'react';
import Providers from './providers';
import type { Metadata } from 'next';
import { Suspense, cache } from 'react';
import { auth } from '~/app/(auth)/auth';
import Loader from '~/components/loader';
import { Outfit } from 'next/font/google';
import { Toaster } from '~/components/ui/sonner';
Expand All @@ -17,11 +18,15 @@ export const metadata: Metadata = {
description: 'Retailytics aka Retail Intelligence',
};

export default function RootLayout({
const getSession = cache(() => auth());

export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const session = await getSession();

return (
<html lang="en" suppressHydrationWarning>
<head>
Expand All @@ -32,7 +37,7 @@ export default function RootLayout({
/>
</head>
<body className={`${outfit.variable} antialiased`}>
<Providers>
<Providers session={session}>
<Suspense fallback={<Loader />}>{children}</Suspense>
<Toaster />
<Analytics />
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import * as React from 'react';
import { ThemeProvider } from './theme';
import type { Session } from 'next-auth';
import { initMixpanel } from '~/lib/mixpanel';
import { ProgressProvider } from '@bprogress/next/app';

const Providers = ({ children }: { children: React.ReactNode }) => {
const Providers = ({
children,
session,
}: {
children: React.ReactNode;
session: Session | null;
}) => {
React.useEffect(() => {
initMixpanel();
initMixpanel(session);
}, []);

return (
Expand Down
13 changes: 12 additions & 1 deletion frontend/src/lib/mixpanel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { env } from '~/env';
import mixpanel from 'mixpanel-browser';
import type { Session } from 'next-auth';

export const initMixpanel = () => {
export const initMixpanel = (session: Session | null) => {
if (!env.NEXT_PUBLIC_MIXPANEL_TOKEN) {
console.warn('Mixpanel token is missing! Check your .env file.');
return;
Expand All @@ -11,4 +12,14 @@ export const initMixpanel = () => {
autocapture: true,
record_sessions_percent: 100,
});

if (session?.user) {
mixpanel.identify(session.user.id);
mixpanel.people.set({
$email: session.user.email,
role: session.user.role,
});
} else {
mixpanel.identify('Guest');
}
};
7 changes: 5 additions & 2 deletions frontend/src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export enum AuthProvider {
GOOGLE = 'google',
}

export interface UserInterface extends AbstractBaseInterface {
email: string;
export interface UserBase extends AbstractBaseInterface {
password: string;
role: UserRole;
status: UserStatus;
Expand All @@ -30,3 +29,7 @@ export interface UserInterface extends AbstractBaseInterface {
assigned_phase_id?: string;
assigned_district_id?: string;
}

export interface UserInterface extends UserBase {
email: string;
}
Loading