Skip to content

Commit 3a4e1ea

Browse files
Add login_method with default of unknown (#32)
* Add login_method with default of unknown * Bump version * Export types
1 parent 6a2d2dc commit 3a4e1ea

File tree

6 files changed

+584
-215
lines changed

6 files changed

+584
-215
lines changed

package-lock.json

Lines changed: 460 additions & 214 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "git",
55
"url": "https://github.com/PropelAuth/node"
66
},
7-
"version": "2.1.9",
7+
"version": "2.1.10",
88
"license": "MIT",
99
"keywords": [
1010
"auth",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export {
4040
UpdateUserMetadataException,
4141
UserNotFoundException,
4242
} from "./exceptions"
43+
export { LoginMethod, SamlLoginProvider, SocialLoginProvider } from "./loginMethod"
4344
export {
4445
CreatedOrg,
4546
CreatedUser,

src/loginMethod.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
export type SocialLoginProvider =
2+
| "Google"
3+
| "GitHub"
4+
| "Microsoft"
5+
| "Slack"
6+
| "LinkedIn"
7+
| "Salesforce"
8+
| "Xero"
9+
| "QuickBooks Online"
10+
export type SamlLoginProvider = "Google" | "Rippling" | "OneLogin" | "JumpCloud" | "Okta" | "Azure" | "Duo" | "Generic"
11+
12+
export type LoginMethod =
13+
| {
14+
loginMethod: "password"
15+
}
16+
| {
17+
loginMethod: "magic_link"
18+
}
19+
| {
20+
loginMethod: "social_sso"
21+
provider: SocialLoginProvider
22+
}
23+
| {
24+
loginMethod: "email_confirmation_link"
25+
}
26+
| {
27+
loginMethod: "saml_sso"
28+
provider: SamlLoginProvider
29+
orgId: string
30+
}
31+
| {
32+
loginMethod: "impersonation"
33+
}
34+
| {
35+
loginMethod: "generated_from_backend_api"
36+
}
37+
| {
38+
loginMethod: "unknown"
39+
}
40+
41+
export type InternalLoginMethod =
42+
| {
43+
login_method: "password"
44+
}
45+
| {
46+
login_method: "magic_link"
47+
}
48+
| {
49+
login_method: "social_sso"
50+
provider: SocialLoginProvider
51+
}
52+
| {
53+
login_method: "email_confirmation_link"
54+
}
55+
| {
56+
login_method: "saml_sso"
57+
provider: SamlLoginProvider
58+
org_id: string
59+
}
60+
| {
61+
login_method: "impersonation"
62+
}
63+
| {
64+
login_method: "generated_from_backend_api"
65+
}
66+
| {
67+
login_method: "unknown"
68+
}
69+
70+
export function toLoginMethod(snake_case?: InternalLoginMethod): LoginMethod {
71+
if (!snake_case) {
72+
return { loginMethod: "unknown" }
73+
}
74+
75+
switch (snake_case.login_method) {
76+
case "password":
77+
return { loginMethod: "password" }
78+
case "magic_link":
79+
return { loginMethod: "magic_link" }
80+
case "social_sso":
81+
return { loginMethod: "social_sso", provider: snake_case.provider }
82+
case "email_confirmation_link":
83+
return { loginMethod: "email_confirmation_link" }
84+
case "saml_sso":
85+
return { loginMethod: "saml_sso", provider: snake_case.provider, orgId: snake_case.org_id }
86+
case "impersonation":
87+
return { loginMethod: "impersonation" }
88+
case "generated_from_backend_api":
89+
return { loginMethod: "generated_from_backend_api" }
90+
default:
91+
return { loginMethod: "unknown" }
92+
}
93+
}

src/user.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { InternalLoginMethod, LoginMethod, toLoginMethod } from "./loginMethod"
2+
13
export type UserProperties = { [key: string]: unknown }
24

35
export type User = {
@@ -11,6 +13,7 @@ export type User = {
1113
impersonatorUserId?: string
1214
metadata?: { [key: string]: any }
1315
properties?: UserProperties
16+
loginMethod: LoginMethod
1417
}
1518

1619
export class UserClass {
@@ -23,6 +26,7 @@ export class UserClass {
2326
public lastName?: string
2427
public username?: string
2528
public properties?: UserProperties
29+
public loginMethod: LoginMethod
2630

2731
// If you used our migration APIs to migrate this user from a different system,
2832
// this is their original ID from that system.
@@ -41,6 +45,7 @@ export class UserClass {
4145
this.legacyUserId = user.legacyUserId
4246
this.impersonatorUserId = user.impersonatorUserId
4347
this.properties = user.properties
48+
this.loginMethod = user.loginMethod
4449
}
4550

4651
public getOrg(orgId: string): OrgMemberInfo | undefined {
@@ -292,6 +297,7 @@ export type InternalUser = {
292297
username?: string
293298
metadata?: { [key: string]: any }
294299
properties?: { [key: string]: unknown }
300+
login_method?: InternalLoginMethod
295301

296302
// If you used our migration APIs to migrate this user from a different system, this is their original ID from that system.
297303
legacy_user_id?: string
@@ -310,6 +316,7 @@ export function toUser(snake_case: InternalUser): User {
310316
impersonatorUserId: snake_case.impersonator_user_id,
311317
metadata: snake_case.metadata,
312318
properties: snake_case.properties,
319+
loginMethod: toLoginMethod(snake_case.login_method),
313320
}
314321

315322
return camelCase

test/middleware.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ test("toUser converts correctly with orgs", async () => {
193193
[]
194194
),
195195
},
196+
loginMethod: { loginMethod: "unknown" },
196197
}
197198

198199
expect(toUser(internalUser)).toEqual(user)
@@ -210,6 +211,25 @@ test("toUser converts correctly without orgs", async () => {
210211
211212
username: "easteregg",
212213
legacyUserId: "something",
214+
loginMethod: { loginMethod: "unknown" },
215+
}
216+
expect(toUser(internalUser)).toEqual(user)
217+
})
218+
219+
test("toUser converts login_method correctly", async () => {
220+
const internalUser: InternalUser = {
221+
user_id: "cbf064e2-edaa-4d35-b413-a8d857329c12",
222+
223+
username: "easteregg",
224+
legacy_user_id: "something",
225+
login_method: { login_method: "saml_sso", org_id: "someOrgId", provider: "Okta" },
226+
}
227+
const user: User = {
228+
userId: "cbf064e2-edaa-4d35-b413-a8d857329c12",
229+
230+
username: "easteregg",
231+
legacyUserId: "something",
232+
loginMethod: { loginMethod: "saml_sso", orgId: "someOrgId", provider: "Okta" },
213233
}
214234
expect(toUser(internalUser)).toEqual(user)
215235
})
@@ -226,6 +246,7 @@ test("parseSnakeCaseToCamelCase converts correctly", async () => {
226246
org_metadata: { orgdata_a: "orgvalue_a" },
227247
},
228248
},
249+
login_method: { login_method: "password" },
229250
}
230251
const camelCase = {
231252
userId: "cbf064e2-edaa-4d35-b413-a8d857329c12",
@@ -238,6 +259,7 @@ test("parseSnakeCaseToCamelCase converts correctly", async () => {
238259
orgMetadata: { orgdata_a: "orgvalue_a" },
239260
},
240261
},
262+
loginMethod: { loginMethod: "password" },
241263
}
242264

243265
expect(parseSnakeCaseToCamelCase(JSON.stringify(snakeCase))).toEqual(camelCase)

0 commit comments

Comments
 (0)