-
Notifications
You must be signed in to change notification settings - Fork 0
/
oktaService.ts
81 lines (75 loc) · 1.88 KB
/
oktaService.ts
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
import axios from "axios";
import { OktaUser } from "./types";
// Axios instance for Okta API
const oktaApi = axios.create({
baseURL: `https://${process.env.OKTA_DOMAIN}/api/v1`,
headers: {
"Content-Type": "application/json",
Authorization: `SSWS ${process.env.OKTA_TOKEN}`,
},
});
// List users from Okta
export const fetchOktaUsers = async (): Promise<OktaUser[]> => {
try {
const response = await oktaApi.get("/users");
return response.data as OktaUser[];
} catch (error: any) {
console.error(
`Error fetching Okta users: ${
error.response?.statusText || error.message
}`
);
throw new Error("Failed to fetch Okta users");
}
};
// Create user in Okta
export const onboardToOkta = async (
email: string,
firstName: string,
lastName: string
): Promise<void> => {
try {
const response = await oktaApi.post(
"/users",
{
profile: {
firstName,
lastName,
email,
login: email,
},
},
{
params: {
activate: "true",
provider: "false",
nextLogin: "changePassword",
},
}
);
console.log(
`Onboarded user: ${firstName} ${lastName} , email : (${email})`
);
} catch (error: any) {
console.error(
`Error onboarding user ${email}: ${
error.response?.statusText || error.message
}`
);
throw new Error(`Failed to onboard user ${email}`);
}
};
// Delete user from Okta
export const removeFromOkta = async (userId: string): Promise<void> => {
try {
await oktaApi.delete(`/users/${userId}`);
console.log(`Removed user with ID: ${userId}`);
} catch (error: any) {
console.error(
`Error removing user with ID ${userId}: ${
error.response?.statusText || error.message
}`
);
throw new Error(`Failed to remove user with ID ${userId}`);
}
};