@propelauth/nextjs 0.0.115 → 0.0.119

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,85 @@
1
1
  import React from 'react';
2
2
 
3
+ declare enum SocialLoginProvider {
4
+ Google = "Google",
5
+ GitHub = "GitHub",
6
+ Microsoft = "Microsoft",
7
+ Slack = "Slack",
8
+ LinkedIn = "LinkedIn",
9
+ Salesforce = "Salesforce",
10
+ Xero = "Xero",
11
+ QuickBooksOnline = "QuickBooks Online"
12
+ }
13
+ declare enum SamlLoginProvider {
14
+ Google = "Google",
15
+ Rippling = "Rippling",
16
+ OneLogin = "OneLogin",
17
+ JumpCloud = "JumpCloud",
18
+ Okta = "Okta",
19
+ Azure = "Azure",
20
+ Duo = "Duo",
21
+ Generic = "Generic"
22
+ }
23
+ type InternalPasswordLoginMethod = {
24
+ login_method: 'password';
25
+ };
26
+ type InternalMagicLinkLoginMethod = {
27
+ login_method: 'magic_link';
28
+ };
29
+ type InternalSocialSsoLoginMethod = {
30
+ login_method: 'social_sso';
31
+ provider: SocialLoginProvider;
32
+ };
33
+ type InternalEmailConfirmationLinkLoginMethod = {
34
+ login_method: 'email_confirmation_link';
35
+ };
36
+ type InternalSamlSsoLoginMethod = {
37
+ login_method: 'saml_sso';
38
+ provider: SamlLoginProvider;
39
+ org_id: string;
40
+ };
41
+ type InternalImpersonationLoginMethod = {
42
+ login_method: 'impersonation';
43
+ };
44
+ type InternalGeneratedFromBackendApiLoginMethod = {
45
+ login_method: 'generated_from_backend_api';
46
+ };
47
+ type InternalUnknownLoginMethod = {
48
+ login_method: 'unknown';
49
+ };
50
+ type InternalLoginMethod = InternalPasswordLoginMethod | InternalMagicLinkLoginMethod | InternalSocialSsoLoginMethod | InternalEmailConfirmationLinkLoginMethod | InternalSamlSsoLoginMethod | InternalImpersonationLoginMethod | InternalGeneratedFromBackendApiLoginMethod | InternalUnknownLoginMethod;
51
+ type PasswordLoginMethod = {
52
+ loginMethod: 'password';
53
+ };
54
+ type MagicLinkLoginMethod = {
55
+ loginMethod: 'magic_link';
56
+ };
57
+ type SocialSsoLoginMethod = {
58
+ loginMethod: 'social_sso';
59
+ provider: SocialLoginProvider;
60
+ };
61
+ type EmailConfirmationLinkLoginMethod = {
62
+ loginMethod: 'email_confirmation_link';
63
+ };
64
+ type SamlSsoLoginMethod = {
65
+ loginMethod: 'saml_sso';
66
+ provider: SamlLoginProvider;
67
+ orgId: string;
68
+ };
69
+ type ImpersonationLoginMethod = {
70
+ loginMethod: 'impersonation';
71
+ };
72
+ type GeneratedFromBackendApiLoginMethod = {
73
+ loginMethod: 'generated_from_backend_api';
74
+ };
75
+ type UnknownLoginMethod = {
76
+ loginMethod: 'unknown';
77
+ };
78
+ type LoginMethod = PasswordLoginMethod | MagicLinkLoginMethod | SocialSsoLoginMethod | EmailConfirmationLinkLoginMethod | SamlSsoLoginMethod | ImpersonationLoginMethod | GeneratedFromBackendApiLoginMethod | UnknownLoginMethod;
79
+
3
80
  declare class UserFromToken {
4
81
  userId: string;
82
+ activeOrgId?: string;
5
83
  orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo;
6
84
  email: string;
7
85
  firstName?: string;
@@ -10,16 +88,20 @@ declare class UserFromToken {
10
88
  properties?: {
11
89
  [key: string]: unknown;
12
90
  };
91
+ loginMethod?: LoginMethod;
13
92
  legacyUserId?: string;
14
93
  impersonatorUserId?: string;
15
94
  constructor(userId: string, email: string, orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo, firstName?: string, lastName?: string, username?: string, legacyUserId?: string, impersonatorUserId?: string, properties?: {
16
95
  [key: string]: unknown;
17
- });
96
+ }, activeOrgId?: string, loginMethod?: LoginMethod);
97
+ getActiveOrg(): OrgMemberInfo | undefined;
98
+ getActiveOrgId(): string | undefined;
18
99
  getOrg(orgId: string): OrgMemberInfo | undefined;
19
100
  getOrgByName(orgName: string): OrgMemberInfo | undefined;
20
101
  getOrgs(): OrgMemberInfo[];
21
102
  isImpersonating(): boolean;
22
103
  static fromJSON(json: string): UserFromToken;
104
+ static fromJwtPayload(payload: InternalUser): UserFromToken;
23
105
  }
24
106
  type OrgIdToOrgMemberInfo = {
25
107
  [orgId: string]: OrgMemberInfo;
@@ -46,6 +128,34 @@ declare class OrgMemberInfo {
46
128
  get inheritedRolesPlusCurrentRole(): string[];
47
129
  get permissions(): string[];
48
130
  }
131
+ type InternalOrgMemberInfo = {
132
+ org_id: string;
133
+ org_name: string;
134
+ org_metadata: {
135
+ [key: string]: any;
136
+ };
137
+ url_safe_org_name: string;
138
+ user_role: string;
139
+ inherited_user_roles_plus_current_role: string[];
140
+ user_permissions: string[];
141
+ };
142
+ type InternalUser = {
143
+ user_id: string;
144
+ org_member_info?: InternalOrgMemberInfo;
145
+ org_id_to_org_member_info?: {
146
+ [org_id: string]: InternalOrgMemberInfo;
147
+ };
148
+ email: string;
149
+ first_name?: string;
150
+ last_name?: string;
151
+ username?: string;
152
+ properties?: {
153
+ [key: string]: unknown;
154
+ };
155
+ login_method?: InternalLoginMethod;
156
+ legacy_user_id?: string;
157
+ impersonatorUserId?: string;
158
+ };
49
159
 
50
160
  declare class User {
51
161
  userId: string;
@@ -57,6 +167,7 @@ declare class User {
57
167
  lastName?: string;
58
168
  pictureUrl?: string;
59
169
  orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo;
170
+ activeOrgId?: string;
60
171
  mfaEnabled: boolean;
61
172
  canCreateOrgs: boolean;
62
173
  updatePasswordRequired: boolean;
@@ -67,7 +178,7 @@ declare class User {
67
178
  };
68
179
  legacyUserId?: string;
69
180
  impersonatorUserId?: string;
70
- constructor({ userId, email, emailConfirmed, hasPassword, username, firstName, lastName, pictureUrl, orgIdToOrgMemberInfo, mfaEnabled, canCreateOrgs, updatePasswordRequired, createdAt, lastActiveAt, legacyUserId, properties, impersonatorUserId, }: {
181
+ constructor({ userId, email, emailConfirmed, hasPassword, username, firstName, lastName, pictureUrl, orgIdToOrgMemberInfo, activeOrgId, mfaEnabled, canCreateOrgs, updatePasswordRequired, createdAt, lastActiveAt, legacyUserId, properties, impersonatorUserId, }: {
71
182
  userId: string;
72
183
  email: string;
73
184
  emailConfirmed: boolean;
@@ -77,6 +188,7 @@ declare class User {
77
188
  lastName?: string;
78
189
  pictureUrl?: string;
79
190
  orgIdToOrgMemberInfo?: OrgIdToOrgMemberInfo;
191
+ activeOrgId?: string;
80
192
  mfaEnabled: boolean;
81
193
  canCreateOrgs: boolean;
82
194
  updatePasswordRequired: boolean;
@@ -88,6 +200,8 @@ declare class User {
88
200
  };
89
201
  impersonatorUserId?: string;
90
202
  });
203
+ getActiveOrg(): OrgMemberInfo | undefined;
204
+ getActiveOrgId(): string | undefined;
91
205
  getOrg(orgId: string): OrgMemberInfo | undefined;
92
206
  getOrgByName(orgName: string): OrgMemberInfo | undefined;
93
207
  getOrgs(): OrgMemberInfo[];
@@ -98,18 +212,21 @@ type UseUserLoading = {
98
212
  isLoggedIn: never;
99
213
  user: never;
100
214
  accessToken: never;
215
+ setActiveOrg: never;
101
216
  };
102
217
  type UseUserLoggedIn = {
103
218
  loading: false;
104
219
  isLoggedIn: true;
105
220
  user: User;
106
221
  accessToken: string;
222
+ setActiveOrg: (orgId: string) => Promise<User | undefined>;
107
223
  };
108
224
  type UseUserNotLoggedIn = {
109
225
  loading: false;
110
226
  isLoggedIn: false;
111
227
  user: undefined;
112
228
  accessToken: undefined;
229
+ setActiveOrg: never;
113
230
  };
114
231
  type UseUser = UseUserLoading | UseUserLoggedIn | UseUserNotLoggedIn;
115
232
  declare function useUser(): UseUser;
@@ -82,10 +82,36 @@ __export(client_exports, {
82
82
  });
83
83
  module.exports = __toCommonJS(client_exports);
84
84
 
85
+ // src/loginMethod.ts
86
+ function toLoginMethod(snake_case) {
87
+ if (!snake_case) {
88
+ return { loginMethod: "unknown" };
89
+ }
90
+ switch (snake_case.login_method) {
91
+ case "password":
92
+ return { loginMethod: "password" };
93
+ case "magic_link":
94
+ return { loginMethod: "magic_link" };
95
+ case "social_sso":
96
+ return { loginMethod: "social_sso", provider: snake_case.provider };
97
+ case "email_confirmation_link":
98
+ return { loginMethod: "email_confirmation_link" };
99
+ case "saml_sso":
100
+ return { loginMethod: "saml_sso", provider: snake_case.provider, orgId: snake_case.org_id };
101
+ case "impersonation":
102
+ return { loginMethod: "impersonation" };
103
+ case "generated_from_backend_api":
104
+ return { loginMethod: "generated_from_backend_api" };
105
+ default:
106
+ return { loginMethod: "unknown" };
107
+ }
108
+ }
109
+
85
110
  // src/user.ts
86
111
  var UserFromToken = class {
87
- constructor(userId, email, orgIdToOrgMemberInfo, firstName, lastName, username, legacyUserId, impersonatorUserId, properties) {
112
+ constructor(userId, email, orgIdToOrgMemberInfo, firstName, lastName, username, legacyUserId, impersonatorUserId, properties, activeOrgId, loginMethod) {
88
113
  this.userId = userId;
114
+ this.activeOrgId = activeOrgId;
89
115
  this.orgIdToOrgMemberInfo = orgIdToOrgMemberInfo;
90
116
  this.email = email;
91
117
  this.firstName = firstName;
@@ -94,6 +120,16 @@ var UserFromToken = class {
94
120
  this.legacyUserId = legacyUserId;
95
121
  this.impersonatorUserId = impersonatorUserId;
96
122
  this.properties = properties;
123
+ this.loginMethod = loginMethod;
124
+ }
125
+ getActiveOrg() {
126
+ if (!this.activeOrgId || !this.orgIdToOrgMemberInfo) {
127
+ return void 0;
128
+ }
129
+ return this.orgIdToOrgMemberInfo[this.activeOrgId];
130
+ }
131
+ getActiveOrgId() {
132
+ return this.activeOrgId;
97
133
  }
98
134
  getOrg(orgId) {
99
135
  if (!this.orgIdToOrgMemberInfo) {
@@ -127,9 +163,7 @@ var UserFromToken = class {
127
163
  const obj = JSON.parse(json);
128
164
  const orgIdToOrgMemberInfo = {};
129
165
  for (const orgId in obj.orgIdToOrgMemberInfo) {
130
- orgIdToOrgMemberInfo[orgId] = OrgMemberInfo.fromJSON(
131
- JSON.stringify(obj.orgIdToOrgMemberInfo[orgId])
132
- );
166
+ orgIdToOrgMemberInfo[orgId] = OrgMemberInfo.fromJSON(JSON.stringify(obj.orgIdToOrgMemberInfo[orgId]));
133
167
  }
134
168
  return new UserFromToken(
135
169
  obj.userId,
@@ -140,7 +174,34 @@ var UserFromToken = class {
140
174
  obj.username,
141
175
  obj.legacyUserId,
142
176
  obj.impersonatorUserId,
143
- obj.properties
177
+ obj.properties,
178
+ obj.activeOrgId,
179
+ obj.loginMethod
180
+ );
181
+ }
182
+ static fromJwtPayload(payload) {
183
+ let activeOrgId;
184
+ let orgIdToOrgMemberInfo;
185
+ if (payload.org_member_info) {
186
+ activeOrgId = payload.org_member_info.org_id;
187
+ orgIdToOrgMemberInfo = toOrgIdToOrgMemberInfo({ [activeOrgId]: payload.org_member_info });
188
+ } else {
189
+ activeOrgId = void 0;
190
+ orgIdToOrgMemberInfo = toOrgIdToOrgMemberInfo(payload.org_id_to_org_member_info);
191
+ }
192
+ const loginMethod = toLoginMethod(payload.login_method);
193
+ return new UserFromToken(
194
+ payload.user_id,
195
+ payload.email,
196
+ orgIdToOrgMemberInfo,
197
+ payload.first_name,
198
+ payload.last_name,
199
+ payload.username,
200
+ payload.legacy_user_id,
201
+ payload.impersonatorUserId,
202
+ payload.properties,
203
+ activeOrgId,
204
+ loginMethod
144
205
  );
145
206
  }
146
207
  };
@@ -292,6 +353,7 @@ var User = class {
292
353
  lastName,
293
354
  pictureUrl,
294
355
  orgIdToOrgMemberInfo,
356
+ activeOrgId,
295
357
  mfaEnabled,
296
358
  canCreateOrgs,
297
359
  updatePasswordRequired,
@@ -310,6 +372,7 @@ var User = class {
310
372
  this.lastName = lastName;
311
373
  this.pictureUrl = pictureUrl;
312
374
  this.orgIdToOrgMemberInfo = orgIdToOrgMemberInfo;
375
+ this.activeOrgId = activeOrgId;
313
376
  this.mfaEnabled = mfaEnabled;
314
377
  this.canCreateOrgs = canCreateOrgs;
315
378
  this.updatePasswordRequired = updatePasswordRequired;
@@ -319,6 +382,15 @@ var User = class {
319
382
  this.properties = properties;
320
383
  this.impersonatorUserId = impersonatorUserId;
321
384
  }
385
+ getActiveOrg() {
386
+ if (!this.activeOrgId) {
387
+ return void 0;
388
+ }
389
+ return this.getOrg(this.activeOrgId);
390
+ }
391
+ getActiveOrgId() {
392
+ return this.activeOrgId;
393
+ }
322
394
  getOrg(orgId) {
323
395
  var _a;
324
396
  return (_a = this.orgIdToOrgMemberInfo) == null ? void 0 : _a[orgId];
@@ -357,21 +429,24 @@ function useUser() {
357
429
  loading: true,
358
430
  isLoggedIn: void 0,
359
431
  user: void 0,
360
- accessToken: void 0
432
+ accessToken: void 0,
433
+ setActiveOrg: void 0
361
434
  };
362
435
  } else if (userAndAccessToken.user) {
363
436
  return {
364
437
  loading: false,
365
438
  isLoggedIn: true,
366
439
  user: userAndAccessToken.user,
367
- accessToken: userAndAccessToken.accessToken
440
+ accessToken: userAndAccessToken.accessToken,
441
+ setActiveOrg: context.setActiveOrg
368
442
  };
369
443
  } else {
370
444
  return {
371
445
  loading: false,
372
446
  isLoggedIn: false,
373
447
  user: void 0,
374
- accessToken: void 0
448
+ accessToken: void 0,
449
+ setActiveOrg: void 0
375
450
  };
376
451
  }
377
452
  }
@@ -558,7 +633,7 @@ var AuthProvider = (props) => {
558
633
  const redirectToOrgPage = (orgId, opts) => redirectTo(getOrgPageUrl(orgId, opts));
559
634
  const redirectToCreateOrgPage = (opts) => redirectTo(getCreateOrgPageUrl(opts));
560
635
  const redirectToSetupSAMLPage = (orgId, opts) => redirectTo(getSetupSAMLPageUrl(orgId, opts));
561
- const refreshAuthInfo = () => __async(void 0, null, function* () {
636
+ const refreshAuthInfo = (0, import_react2.useCallback)(() => __async(void 0, null, function* () {
562
637
  const action = yield apiGetUserInfo();
563
638
  if (action.error) {
564
639
  throw new Error("Failed to refresh token");
@@ -566,7 +641,19 @@ var AuthProvider = (props) => {
566
641
  dispatch(action);
567
642
  return action.user;
568
643
  }
569
- });
644
+ }), [dispatch]);
645
+ const setActiveOrg = (0, import_react2.useCallback)(
646
+ (orgId) => __async(void 0, null, function* () {
647
+ const action = yield apiPostSetActiveOrg(orgId);
648
+ if (action.error === "not_in_org") {
649
+ return void 0;
650
+ } else {
651
+ dispatch(action);
652
+ return action.user;
653
+ }
654
+ }),
655
+ [dispatch]
656
+ );
570
657
  const value = {
571
658
  loading: authState.loading,
572
659
  userAndAccessToken: authState.userAndAccessToken,
@@ -583,7 +670,8 @@ var AuthProvider = (props) => {
583
670
  getOrgPageUrl,
584
671
  getCreateOrgPageUrl,
585
672
  getSetupSAMLPageUrl,
586
- refreshAuthInfo
673
+ refreshAuthInfo,
674
+ setActiveOrg
587
675
  };
588
676
  return /* @__PURE__ */ import_react2.default.createElement(AuthContext.Provider, { value }, props.children);
589
677
  };
@@ -598,7 +686,7 @@ function apiGetUserInfo() {
598
686
  credentials: "include"
599
687
  });
600
688
  if (userInfoResponse.ok) {
601
- const { userinfo, accessToken, impersonatorUserId } = yield userInfoResponse.json();
689
+ const { userinfo, accessToken, impersonatorUserId, activeOrgId } = yield userInfoResponse.json();
602
690
  const user = new User({
603
691
  userId: userinfo.user_id,
604
692
  email: userinfo.email,
@@ -609,6 +697,7 @@ function apiGetUserInfo() {
609
697
  lastName: userinfo.last_name,
610
698
  pictureUrl: userinfo.picture_url,
611
699
  orgIdToOrgMemberInfo: toOrgIdToOrgMemberInfo(userinfo.org_id_to_org_info),
700
+ activeOrgId,
612
701
  mfaEnabled: userinfo.mfa_enabled,
613
702
  canCreateOrgs: userinfo.can_create_orgs,
614
703
  updatePasswordRequired: userinfo.update_password_required,
@@ -630,6 +719,51 @@ function apiGetUserInfo() {
630
719
  }
631
720
  });
632
721
  }
722
+ function apiPostSetActiveOrg(orgId) {
723
+ return __async(this, null, function* () {
724
+ try {
725
+ const queryParams = new URLSearchParams({ active_org_id: orgId }).toString();
726
+ const url = `/api/auth/set-active-org?${queryParams}`;
727
+ const userInfoResponse = yield fetch(url, {
728
+ method: "POST",
729
+ headers: {
730
+ "Content-Type": "application/json"
731
+ },
732
+ credentials: "include"
733
+ });
734
+ if (userInfoResponse.ok) {
735
+ const { userinfo, accessToken, impersonatorUserId, activeOrgId } = yield userInfoResponse.json();
736
+ const user = new User({
737
+ userId: userinfo.user_id,
738
+ email: userinfo.email,
739
+ emailConfirmed: userinfo.email_confirmed,
740
+ hasPassword: userinfo.has_password,
741
+ username: userinfo.username,
742
+ firstName: userinfo.first_name,
743
+ lastName: userinfo.last_name,
744
+ pictureUrl: userinfo.picture_url,
745
+ orgIdToOrgMemberInfo: toOrgIdToOrgMemberInfo(userinfo.org_id_to_org_info),
746
+ activeOrgId,
747
+ mfaEnabled: userinfo.mfa_enabled,
748
+ canCreateOrgs: userinfo.can_create_orgs,
749
+ updatePasswordRequired: userinfo.update_password_required,
750
+ createdAt: userinfo.created_at,
751
+ lastActiveAt: userinfo.last_active_at,
752
+ properties: userinfo.properties,
753
+ impersonatorUserId
754
+ });
755
+ return { user, accessToken, error: void 0 };
756
+ } else if (userInfoResponse.status === 401) {
757
+ return { error: "not_in_org" };
758
+ } else {
759
+ console.info("Failed to set active org", userInfoResponse);
760
+ }
761
+ } catch (e) {
762
+ console.info("Failed to set active org", e);
763
+ }
764
+ throw new Error("Failed to set active org");
765
+ });
766
+ }
633
767
  var encodeBase64 = (str) => {
634
768
  const encode = window ? window.btoa : btoa;
635
769
  return encode(str);