diff --git a/lib/management/jwt.test.ts b/lib/management/jwt.test.ts index f5e10eee..2e322604 100644 --- a/lib/management/jwt.test.ts +++ b/lib/management/jwt.test.ts @@ -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'; @@ -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 = 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 = await management.jwt.signUp( + 'user-id-1', + { + email: 'test@example.com', + name: 'lorem ipsum', + }, + { + customClaims: { k1: 'v1' }, + }, + ); + + expect(mockHttpClient.post).toHaveBeenCalledWith( + apiPaths.jwt.signUp, + { + loginId: 'user-id-1', + user: { + email: 'test@example.com', + 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 = await management.jwt.signUpOrIn( + 'user-id-1', + { + email: 'test@example.com', + name: 'lorem ipsum', + }, + { + customClaims: { k1: 'v1' }, + }, + ); + + expect(mockHttpClient.post).toHaveBeenCalledWith( + apiPaths.jwt.signUpOrIn, + { + loginId: 'user-id-1', + user: { + email: 'test@example.com', + name: 'lorem ipsum', + }, + customClaims: { k1: 'v1' }, + }, + { token: 'key' }, + ); + + expect(resp).toEqual({ code: 200, data: mockJWTResponse, ok: true, response: httpResponse }); + }); + }); }); diff --git a/lib/management/jwt.ts b/lib/management/jwt.ts index 41f4d34e..4a667684 100644 --- a/lib/management/jwt.ts +++ b/lib/management/jwt.ts @@ -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: ( @@ -30,6 +30,38 @@ const withJWT = (sdk: CoreSdk, managementKey?: string) => ({ { token: managementKey }, ), ), + signIn: (loginId: string, loginOptions?: MgmtLoginOptions): Promise> => + transformResponse( + sdk.httpClient.post( + apiPaths.jwt.signIn, + { loginId, ...loginOptions }, + { token: managementKey }, + ), + ), + signUp: ( + loginId: string, + user?: MgmtUserOptions, + signUpOptions?: MgmtSignUpOptions, + ): Promise> => + transformResponse( + sdk.httpClient.post( + apiPaths.jwt.signUp, + { loginId, user, ...signUpOptions }, + { token: managementKey }, + ), + ), + signUpOrIn: ( + loginId: string, + user?: MgmtUserOptions, + signUpOptions?: MgmtSignUpOptions, + ): Promise> => + transformResponse( + sdk.httpClient.post( + apiPaths.jwt.signUpOrIn, + { loginId, user, ...signUpOptions }, + { token: managementKey }, + ), + ), }); export default withJWT; diff --git a/lib/management/paths.ts b/lib/management/paths.ts index 652cb145..8c248942 100644 --- a/lib/management/paths.ts +++ b/lib/management/paths.ts @@ -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', diff --git a/lib/management/types.ts b/lib/management/types.ts index 8a13b531..e5ffeb13 100644 --- a/lib/management/types.ts +++ b/lib/management/types.ts @@ -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'; @@ -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 & { + jwt?: string; +}; + +export type MgmtSignUpOptions = { + // we can replace this with partial `SignUpOptions` from core-js-sdk once its exported + customClaims?: Record; +}; + +export interface UserOptions { + email?: string; + phone?: string; + displayName?: string; + roles?: string[]; + userTenants?: AssociatedTenant[]; + customAttributes?: Record; + 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; +}; diff --git a/lib/management/user.ts b/lib/management/user.ts index bed36ab9..cdc77026 100644 --- a/lib/management/user.ts +++ b/lib/management/user.ts @@ -18,6 +18,7 @@ import { InviteBatchResponse, TemplateOptions, ProviderTokenOptions, + UserOptions, } from './types'; import { CoreSdk, DeliveryMethodForTestUser } from '../types'; import apiPaths from './paths'; @@ -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; - picture?: string; - verifiedEmail?: boolean; - verifiedPhone?: boolean; - givenName?: string; - middleName?: string; - familyName?: string; - additionalLoginIds?: string[]; - ssoAppIds?: string[]; -} - export interface PatchUserOptions { email?: string; phone?: string;