Skip to content

Commit 73670fa

Browse files
authored
sign up or in via mgmt (#434)
* sign up or in via mgmt * cr fixes
1 parent 79d28c1 commit 73670fa

File tree

5 files changed

+180
-21
lines changed

5 files changed

+180
-21
lines changed

lib/management/jwt.test.ts

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SdkResponse } from '@descope/core-js-sdk';
1+
import { JWTResponse, SdkResponse } from '@descope/core-js-sdk';
22
import withManagement from '.';
33
import apiPaths from './paths';
44
import { UpdateJWTResponse } from './types';
@@ -91,4 +91,110 @@ describe('Management JWT', () => {
9191
});
9292
});
9393
});
94+
95+
describe('sign-in', () => {
96+
it('should send the correct request and receive correct response', async () => {
97+
const httpResponse = {
98+
ok: true,
99+
json: () => mockJWTResponse,
100+
clone: () => ({
101+
json: () => Promise.resolve(mockJWTResponse),
102+
}),
103+
status: 200,
104+
};
105+
mockHttpClient.post.mockResolvedValue(httpResponse);
106+
107+
const resp: SdkResponse<JWTResponse> = await management.jwt.signIn('user-id-1', {
108+
customClaims: { k1: 'v1' },
109+
});
110+
111+
expect(mockHttpClient.post).toHaveBeenCalledWith(
112+
apiPaths.jwt.signIn,
113+
{ loginId: 'user-id-1', customClaims: { k1: 'v1' } },
114+
{ token: 'key' },
115+
);
116+
117+
expect(resp).toEqual({ code: 200, data: mockJWTResponse, ok: true, response: httpResponse });
118+
});
119+
});
120+
121+
describe('sign-up', () => {
122+
it('should send the correct request and receive correct response', async () => {
123+
const httpResponse = {
124+
ok: true,
125+
json: () => mockJWTResponse,
126+
clone: () => ({
127+
json: () => Promise.resolve(mockJWTResponse),
128+
}),
129+
status: 200,
130+
};
131+
mockHttpClient.post.mockResolvedValue(httpResponse);
132+
133+
const resp: SdkResponse<JWTResponse> = await management.jwt.signUp(
134+
'user-id-1',
135+
{
136+
email: 'test@example.com',
137+
name: 'lorem ipsum',
138+
},
139+
{
140+
customClaims: { k1: 'v1' },
141+
},
142+
);
143+
144+
expect(mockHttpClient.post).toHaveBeenCalledWith(
145+
apiPaths.jwt.signUp,
146+
{
147+
loginId: 'user-id-1',
148+
user: {
149+
email: 'test@example.com',
150+
name: 'lorem ipsum',
151+
},
152+
customClaims: { k1: 'v1' },
153+
},
154+
{ token: 'key' },
155+
);
156+
157+
expect(resp).toEqual({ code: 200, data: mockJWTResponse, ok: true, response: httpResponse });
158+
});
159+
});
160+
161+
describe('sign-up-or-in', () => {
162+
it('should send the correct request and receive correct response', async () => {
163+
const httpResponse = {
164+
ok: true,
165+
json: () => mockJWTResponse,
166+
clone: () => ({
167+
json: () => Promise.resolve(mockJWTResponse),
168+
}),
169+
status: 200,
170+
};
171+
mockHttpClient.post.mockResolvedValue(httpResponse);
172+
173+
const resp: SdkResponse<JWTResponse> = await management.jwt.signUpOrIn(
174+
'user-id-1',
175+
{
176+
email: 'test@example.com',
177+
name: 'lorem ipsum',
178+
},
179+
{
180+
customClaims: { k1: 'v1' },
181+
},
182+
);
183+
184+
expect(mockHttpClient.post).toHaveBeenCalledWith(
185+
apiPaths.jwt.signUpOrIn,
186+
{
187+
loginId: 'user-id-1',
188+
user: {
189+
email: 'test@example.com',
190+
name: 'lorem ipsum',
191+
},
192+
customClaims: { k1: 'v1' },
193+
},
194+
{ token: 'key' },
195+
);
196+
197+
expect(resp).toEqual({ code: 200, data: mockJWTResponse, ok: true, response: httpResponse });
198+
});
199+
});
94200
});

lib/management/jwt.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { SdkResponse, transformResponse } from '@descope/core-js-sdk';
1+
import { JWTResponse, SdkResponse, transformResponse } from '@descope/core-js-sdk';
22
import { CoreSdk } from '../types';
33
import apiPaths from './paths';
4-
import { UpdateJWTResponse } from './types';
4+
import { MgmtLoginOptions, MgmtSignUpOptions, MgmtUserOptions, UpdateJWTResponse } from './types';
55

66
const withJWT = (sdk: CoreSdk, managementKey?: string) => ({
77
update: (
@@ -30,6 +30,38 @@ const withJWT = (sdk: CoreSdk, managementKey?: string) => ({
3030
{ token: managementKey },
3131
),
3232
),
33+
signIn: (loginId: string, loginOptions?: MgmtLoginOptions): Promise<SdkResponse<JWTResponse>> =>
34+
transformResponse(
35+
sdk.httpClient.post(
36+
apiPaths.jwt.signIn,
37+
{ loginId, ...loginOptions },
38+
{ token: managementKey },
39+
),
40+
),
41+
signUp: (
42+
loginId: string,
43+
user?: MgmtUserOptions,
44+
signUpOptions?: MgmtSignUpOptions,
45+
): Promise<SdkResponse<JWTResponse>> =>
46+
transformResponse(
47+
sdk.httpClient.post(
48+
apiPaths.jwt.signUp,
49+
{ loginId, user, ...signUpOptions },
50+
{ token: managementKey },
51+
),
52+
),
53+
signUpOrIn: (
54+
loginId: string,
55+
user?: MgmtUserOptions,
56+
signUpOptions?: MgmtSignUpOptions,
57+
): Promise<SdkResponse<JWTResponse>> =>
58+
transformResponse(
59+
sdk.httpClient.post(
60+
apiPaths.jwt.signUpOrIn,
61+
{ loginId, user, ...signUpOptions },
62+
{ token: managementKey },
63+
),
64+
),
3365
});
3466

3567
export default withJWT;

lib/management/paths.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ export default {
9292
jwt: {
9393
update: '/v1/mgmt/jwt/update',
9494
impersonate: '/v1/mgmt/impersonate',
95+
signIn: '/v1/mgmt/auth/signin',
96+
signUp: '/v1/mgmt/auth/signup',
97+
signUpOrIn: '/v1/mgmt/auth/signup-in',
9598
},
9699
password: {
97100
settings: '/v1/mgmt/password/settings',

lib/management/types.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UserResponse } from '@descope/core-js-sdk';
1+
import { UserResponse, LoginOptions } from '@descope/core-js-sdk';
22

33
export type ExpirationUnit = 'minutes' | 'hours' | 'days' | 'weeks';
44

@@ -790,3 +790,37 @@ export type CheckResponseRelation = {
790790
allowed: boolean;
791791
tuple: FGARelation;
792792
};
793+
794+
// should have the type of loginoptions expect templateId and templateOptions
795+
export type MgmtLoginOptions = Omit<LoginOptions, 'templateId' | 'templateOptions'> & {
796+
jwt?: string;
797+
};
798+
799+
export type MgmtSignUpOptions = {
800+
// we can replace this with partial `SignUpOptions` from core-js-sdk once its exported
801+
customClaims?: Record<string, any>;
802+
};
803+
804+
export interface UserOptions {
805+
email?: string;
806+
phone?: string;
807+
displayName?: string;
808+
roles?: string[];
809+
userTenants?: AssociatedTenant[];
810+
customAttributes?: Record<string, AttributesTypes>;
811+
picture?: string;
812+
verifiedEmail?: boolean;
813+
verifiedPhone?: boolean;
814+
givenName?: string;
815+
middleName?: string;
816+
familyName?: string;
817+
additionalLoginIds?: string[];
818+
ssoAppIds?: string[];
819+
}
820+
821+
export type MgmtUserOptions = Omit<
822+
UserOptions,
823+
'roles' | 'userTenants' | 'customAttributes' | 'picture' | 'additionalLoginIds' | 'displayName'
824+
> & {
825+
name?: string;
826+
};

lib/management/user.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
InviteBatchResponse,
1919
TemplateOptions,
2020
ProviderTokenOptions,
21+
UserOptions,
2122
} from './types';
2223
import { CoreSdk, DeliveryMethodForTestUser } from '../types';
2324
import apiPaths from './paths';
@@ -1003,23 +1004,6 @@ const withUser = (sdk: CoreSdk, managementKey?: string) => {
10031004
};
10041005
};
10051006

1006-
export interface UserOptions {
1007-
email?: string;
1008-
phone?: string;
1009-
displayName?: string;
1010-
roles?: string[];
1011-
userTenants?: AssociatedTenant[];
1012-
customAttributes?: Record<string, AttributesTypes>;
1013-
picture?: string;
1014-
verifiedEmail?: boolean;
1015-
verifiedPhone?: boolean;
1016-
givenName?: string;
1017-
middleName?: string;
1018-
familyName?: string;
1019-
additionalLoginIds?: string[];
1020-
ssoAppIds?: string[];
1021-
}
1022-
10231007
export interface PatchUserOptions {
10241008
email?: string;
10251009
phone?: string;

0 commit comments

Comments
 (0)