Skip to content

Commit

Permalink
sign up or in via mgmt (#434)
Browse files Browse the repository at this point in the history
* sign up or in via mgmt

* cr fixes
  • Loading branch information
asafshen authored Feb 6, 2025
1 parent 79d28c1 commit 73670fa
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 21 deletions.
108 changes: 107 additions & 1 deletion lib/management/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SdkResponse } from '@descope/core-js-sdk';
import { JWTResponse, SdkResponse } from '@descope/core-js-sdk';
import withManagement from '.';
import apiPaths from './paths';
import { UpdateJWTResponse } from './types';
Expand Down Expand Up @@ -91,4 +91,110 @@ describe('Management JWT', () => {
});
});
});

describe('sign-in', () => {
it('should send the correct request and receive correct response', async () => {
const httpResponse = {
ok: true,
json: () => mockJWTResponse,
clone: () => ({
json: () => Promise.resolve(mockJWTResponse),
}),
status: 200,
};
mockHttpClient.post.mockResolvedValue(httpResponse);

const resp: SdkResponse<JWTResponse> = await management.jwt.signIn('user-id-1', {
customClaims: { k1: 'v1' },
});

expect(mockHttpClient.post).toHaveBeenCalledWith(
apiPaths.jwt.signIn,
{ loginId: 'user-id-1', customClaims: { k1: 'v1' } },
{ token: 'key' },
);

expect(resp).toEqual({ code: 200, data: mockJWTResponse, ok: true, response: httpResponse });
});
});

describe('sign-up', () => {
it('should send the correct request and receive correct response', async () => {
const httpResponse = {
ok: true,
json: () => mockJWTResponse,
clone: () => ({
json: () => Promise.resolve(mockJWTResponse),
}),
status: 200,
};
mockHttpClient.post.mockResolvedValue(httpResponse);

const resp: SdkResponse<JWTResponse> = await management.jwt.signUp(
'user-id-1',
{
email: '[email protected]',
name: 'lorem ipsum',
},
{
customClaims: { k1: 'v1' },
},
);

expect(mockHttpClient.post).toHaveBeenCalledWith(
apiPaths.jwt.signUp,
{
loginId: 'user-id-1',
user: {
email: '[email protected]',
name: 'lorem ipsum',
},
customClaims: { k1: 'v1' },
},
{ token: 'key' },
);

expect(resp).toEqual({ code: 200, data: mockJWTResponse, ok: true, response: httpResponse });
});
});

describe('sign-up-or-in', () => {
it('should send the correct request and receive correct response', async () => {
const httpResponse = {
ok: true,
json: () => mockJWTResponse,
clone: () => ({
json: () => Promise.resolve(mockJWTResponse),
}),
status: 200,
};
mockHttpClient.post.mockResolvedValue(httpResponse);

const resp: SdkResponse<JWTResponse> = await management.jwt.signUpOrIn(
'user-id-1',
{
email: '[email protected]',
name: 'lorem ipsum',
},
{
customClaims: { k1: 'v1' },
},
);

expect(mockHttpClient.post).toHaveBeenCalledWith(
apiPaths.jwt.signUpOrIn,
{
loginId: 'user-id-1',
user: {
email: '[email protected]',
name: 'lorem ipsum',
},
customClaims: { k1: 'v1' },
},
{ token: 'key' },
);

expect(resp).toEqual({ code: 200, data: mockJWTResponse, ok: true, response: httpResponse });
});
});
});
36 changes: 34 additions & 2 deletions lib/management/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SdkResponse, transformResponse } from '@descope/core-js-sdk';
import { JWTResponse, SdkResponse, transformResponse } from '@descope/core-js-sdk';
import { CoreSdk } from '../types';
import apiPaths from './paths';
import { UpdateJWTResponse } from './types';
import { MgmtLoginOptions, MgmtSignUpOptions, MgmtUserOptions, UpdateJWTResponse } from './types';

const withJWT = (sdk: CoreSdk, managementKey?: string) => ({
update: (
Expand Down Expand Up @@ -30,6 +30,38 @@ const withJWT = (sdk: CoreSdk, managementKey?: string) => ({
{ token: managementKey },
),
),
signIn: (loginId: string, loginOptions?: MgmtLoginOptions): Promise<SdkResponse<JWTResponse>> =>
transformResponse(
sdk.httpClient.post(
apiPaths.jwt.signIn,
{ loginId, ...loginOptions },
{ token: managementKey },
),
),
signUp: (
loginId: string,
user?: MgmtUserOptions,
signUpOptions?: MgmtSignUpOptions,
): Promise<SdkResponse<JWTResponse>> =>
transformResponse(
sdk.httpClient.post(
apiPaths.jwt.signUp,
{ loginId, user, ...signUpOptions },
{ token: managementKey },
),
),
signUpOrIn: (
loginId: string,
user?: MgmtUserOptions,
signUpOptions?: MgmtSignUpOptions,
): Promise<SdkResponse<JWTResponse>> =>
transformResponse(
sdk.httpClient.post(
apiPaths.jwt.signUpOrIn,
{ loginId, user, ...signUpOptions },
{ token: managementKey },
),
),
});

export default withJWT;
3 changes: 3 additions & 0 deletions lib/management/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export default {
jwt: {
update: '/v1/mgmt/jwt/update',
impersonate: '/v1/mgmt/impersonate',
signIn: '/v1/mgmt/auth/signin',
signUp: '/v1/mgmt/auth/signup',
signUpOrIn: '/v1/mgmt/auth/signup-in',
},
password: {
settings: '/v1/mgmt/password/settings',
Expand Down
36 changes: 35 additions & 1 deletion lib/management/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserResponse } from '@descope/core-js-sdk';
import { UserResponse, LoginOptions } from '@descope/core-js-sdk';

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

Expand Down Expand Up @@ -790,3 +790,37 @@ export type CheckResponseRelation = {
allowed: boolean;
tuple: FGARelation;
};

// should have the type of loginoptions expect templateId and templateOptions
export type MgmtLoginOptions = Omit<LoginOptions, 'templateId' | 'templateOptions'> & {
jwt?: string;
};

export type MgmtSignUpOptions = {
// we can replace this with partial `SignUpOptions` from core-js-sdk once its exported
customClaims?: Record<string, any>;
};

export interface UserOptions {
email?: string;
phone?: string;
displayName?: string;
roles?: string[];
userTenants?: AssociatedTenant[];
customAttributes?: Record<string, AttributesTypes>;
picture?: string;
verifiedEmail?: boolean;
verifiedPhone?: boolean;
givenName?: string;
middleName?: string;
familyName?: string;
additionalLoginIds?: string[];
ssoAppIds?: string[];
}

export type MgmtUserOptions = Omit<
UserOptions,
'roles' | 'userTenants' | 'customAttributes' | 'picture' | 'additionalLoginIds' | 'displayName'
> & {
name?: string;
};
18 changes: 1 addition & 17 deletions lib/management/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
InviteBatchResponse,
TemplateOptions,
ProviderTokenOptions,
UserOptions,
} from './types';
import { CoreSdk, DeliveryMethodForTestUser } from '../types';
import apiPaths from './paths';
Expand Down Expand Up @@ -1003,23 +1004,6 @@ const withUser = (sdk: CoreSdk, managementKey?: string) => {
};
};

export interface UserOptions {
email?: string;
phone?: string;
displayName?: string;
roles?: string[];
userTenants?: AssociatedTenant[];
customAttributes?: Record<string, AttributesTypes>;
picture?: string;
verifiedEmail?: boolean;
verifiedPhone?: boolean;
givenName?: string;
middleName?: string;
familyName?: string;
additionalLoginIds?: string[];
ssoAppIds?: string[];
}

export interface PatchUserOptions {
email?: string;
phone?: string;
Expand Down

0 comments on commit 73670fa

Please sign in to comment.