-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrganisationContext.tsx
129 lines (115 loc) · 3.82 KB
/
OrganisationContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// src/contexts/UserOrganisationContext.tsx
import React, { createContext, useContext, useEffect, useState, ReactNode } from 'react';
import { useAuth, useUser } from "@clerk/clerk-react";
import { useNavigate } from 'react-router-dom';
import { getOrganisationById } from '../services/OrganisationService';
import { getUserByClerkId } from '../services/UserService';
import { Organisation } from "./interface/Organisation.ts";
interface User {
_id: string;
firstName: string;
lastName: string;
email: string;
organisation: Organisation;
clerkId: string;
isOnboarded: string;
role: string;
primaryContact: string;
isActive: boolean;
}
interface UserContextType {
user: User | null;
isOnboarded: boolean;
organisation: Organisation | null;
loading: boolean;
token: string | null;
refreshToken: () => Promise<void>;
}
const UserContext = createContext<UserContextType | undefined>(undefined);
const isTokenExpired = (token: string | null): boolean => {
if (!token) return true;
const payload = JSON.parse(atob(token.split('.')[1]));
return payload.exp * 1000 < Date.now();
};
export const useUserContext = (): UserContextType => {
const context = useContext(UserContext);
if (!context) {
throw new Error('useUserContext must be used within a UserProvider');
}
return context;
};
interface UserProviderProps {
children: ReactNode;
}
export const UserProvider: React.FC<UserProviderProps> = ({ children }) => {
const { user: clerkUser, isLoaded, isSignedIn } = useUser();
const [organisation, setOrganisation] = useState<Organisation | null>(null);
const [mongoUser, setMongoUser] = useState<User | null>(null);
const [isOnboarded, setIsOnboarded] = useState<boolean>(false);
const [loading, setLoading] = useState(true);
const [token, setToken] = useState<string | null>(sessionStorage.getItem('authToken'));
const navigate = useNavigate();
const auth = useAuth();
useEffect(() => {
const fetchMongoUser = async (clerkId: string) => {
const response = await getUserByClerkId(clerkId);
if (response.success) {
setMongoUser(response.user);
setIsOnboarded(response.user.isOnboarded === 'true');
return response.user;
} else {
setLoading(false);
console.error('Failed to fetch MongoDB user:', response.message);
return null;
}
};
const fetchOrganisation = async (orgId: string) => {
const response = await getOrganisationById(orgId);
if (response.success) {
setOrganisation(response.organisation);
}
setLoading(false);
};
const fetchToken = async () => {
const userToken = await auth.getToken({template: 'VAJWT'});
console.log('userToken', {userToken})
sessionStorage.setItem('authToken', userToken || '');
setToken(userToken);
};
if (isLoaded) {
if (isSignedIn) {
if (clerkUser) {
fetchMongoUser(clerkUser.id).then((user) => {
if (user) {
if (!user.isOnboarded) {
navigate('/onboarding');
} else {
fetchOrganisation(user.organisation);
fetchToken();
}
} else {
setLoading(false);
}
});
}
} else {
setLoading(false);
}
}
}, [isLoaded, isSignedIn, clerkUser, navigate]);
useEffect(() => {
const interval = setInterval(() => {
if (token) {
const expired = isTokenExpired(token);
console.log('Is token expired:', expired);
}
}, 5000); // 5 seconds interval
// Cleanup interval on component unmount
return () => clearInterval(interval);
}, [token]);
return (
<UserContext.Provider value={{ user: mongoUser, isOnboarded, organisation, loading, token, refreshToken }}>
{children}
</UserContext.Provider>
);
};