feat: create auth provider identity (#8675)
This commit is contained in:
1 parent
3706bf51af
commit
29830f0077
3 files changed
+142
-5
No files matched your search
@@ -1,14 +1,16 @@
|
|||||||
|
import { FindConfig } from "../common"
|
||||||
|
import { IModuleService } from "../modules-sdk"
|
||||||
|
import { Context } from "../shared-context"
|
||||||
import {
|
import {
|
||||||
AuthIdentityDTO,
|
AuthIdentityDTO,
|
||||||
AuthenticationInput,
|
AuthenticationInput,
|
||||||
AuthenticationResponse,
|
AuthenticationResponse,
|
||||||
CreateAuthIdentityDTO,
|
CreateAuthIdentityDTO,
|
||||||
|
CreateProviderIdentityDTO,
|
||||||
FilterableAuthIdentityProps,
|
FilterableAuthIdentityProps,
|
||||||
|
ProviderIdentityDTO,
|
||||||
UpdateAuthIdentityDTO,
|
UpdateAuthIdentityDTO,
|
||||||
} from "./common"
|
} from "./common"
|
||||||
import { Context } from "../shared-context"
|
|
||||||
import { FindConfig } from "../common"
|
|
||||||
import { IModuleService } from "../modules-sdk"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main service interface for the Auth Module.
|
* The main service interface for the Auth Module.
|
||||||
@@ -274,4 +276,49 @@ export interface IAuthModuleService extends IModuleService {
|
|||||||
* await authModuleService.deleteAuthIdentities(["authusr_123", "authusr_321"])
|
* await authModuleService.deleteAuthIdentities(["authusr_123", "authusr_321"])
|
||||||
*/
|
*/
|
||||||
deleteAuthIdentities(ids: string[], sharedContext?: Context): Promise<void>
|
deleteAuthIdentities(ids: string[], sharedContext?: Context): Promise<void>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method creates provider identities.
|
||||||
|
*
|
||||||
|
* @param {CreateProviderIdentityDTO[]} data - The provider identities to be created.
|
||||||
|
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||||
|
* @returns {Promise<ProviderIdentityDTO[]>} The created provider identities.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const providerIdentities = await authModuleService.createProviderIdentities([
|
||||||
|
* {
|
||||||
|
* provider: "emailpass",
|
||||||
|
* entity_id: "user@example.com",
|
||||||
|
* auth_identity_id: "uid_1"
|
||||||
|
* },
|
||||||
|
* {
|
||||||
|
* provider: "github",
|
||||||
|
* entity_id: "github_handle",
|
||||||
|
* auth_identity_id: "uid_1"
|
||||||
|
* },
|
||||||
|
* ])
|
||||||
|
*/
|
||||||
|
createProviderIdentities(
|
||||||
|
data: CreateProviderIdentityDTO[],
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<ProviderIdentityDTO[]>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method creates a provider identity.
|
||||||
|
*
|
||||||
|
* @param {CreateProviderIdentityDTO} data - The provider identity to be created.
|
||||||
|
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||||
|
* @returns {Promise<ProviderIdentityDTO>} The created provider identity.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const providerIdentity = await authModuleService.createProviderIdentities({
|
||||||
|
* provider: "github",
|
||||||
|
* entity_id: "github_handle",
|
||||||
|
* auth_identity_id: "uid_1"
|
||||||
|
* })
|
||||||
|
*/
|
||||||
|
createProviderIdentities(
|
||||||
|
data: CreateProviderIdentityDTO,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<ProviderIdentityDTO>
|
||||||
}
|
}
|
||||||
+62
-2
@@ -1,7 +1,7 @@
|
|||||||
import { IAuthModuleService } from "@medusajs/types"
|
import { IAuthModuleService } from "@medusajs/types"
|
||||||
import { createAuthIdentities } from "../../__fixtures__/auth-identity"
|
|
||||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
|
||||||
import { Modules } from "@medusajs/utils"
|
import { Modules } from "@medusajs/utils"
|
||||||
|
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||||
|
import { createAuthIdentities } from "../../__fixtures__/auth-identity"
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
@@ -259,6 +259,66 @@ moduleIntegrationTestRunner<IAuthModuleService>({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("createProviderIdentity", () => {
|
||||||
|
it("should create a providerIdentity successfully", async () => {
|
||||||
|
let authIdentity = await service.retrieveAuthIdentity("test-id", {
|
||||||
|
relations: ["provider_identities"],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(authIdentity).toEqual(
|
||||||
|
expect.not.objectContaining({
|
||||||
|
provider_identities: [
|
||||||
|
expect.objectContaining({ provider: "manual" }),
|
||||||
|
expect.objectContaining({ provider: "github" }),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
await service.createProviderIdentities({
|
||||||
|
id: "test",
|
||||||
|
entity_id: "christian@medusajs.com",
|
||||||
|
provider: "github",
|
||||||
|
auth_identity_id: authIdentity.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
authIdentity = await service.retrieveAuthIdentity("test-id", {
|
||||||
|
relations: ["provider_identities"],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(authIdentity).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
provider_identities: [
|
||||||
|
expect.objectContaining({ provider: "manual" }),
|
||||||
|
expect.objectContaining({ provider: "github" }),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should list authIdentities by newly created provider", async () => {
|
||||||
|
await service.createProviderIdentities([
|
||||||
|
{
|
||||||
|
id: "test",
|
||||||
|
entity_id: "christian@medusajs.com",
|
||||||
|
provider: "github",
|
||||||
|
auth_identity_id: "test-id",
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const authIdentities = await service.listAuthIdentities({
|
||||||
|
provider_identities: {
|
||||||
|
provider: "github",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(authIdentities).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: "test-id",
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -121,6 +121,36 @@ export default class AuthModuleService
|
|||||||
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
|
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
createProviderIdentities(
|
||||||
|
data: AuthTypes.CreateProviderIdentityDTO[],
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<AuthTypes.ProviderIdentityDTO[]>
|
||||||
|
|
||||||
|
createProviderIdentities(
|
||||||
|
data: AuthTypes.CreateProviderIdentityDTO,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<AuthTypes.ProviderIdentityDTO>
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async createProviderIdentities(
|
||||||
|
data:
|
||||||
|
| AuthTypes.CreateProviderIdentityDTO[]
|
||||||
|
| AuthTypes.CreateProviderIdentityDTO,
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<AuthTypes.ProviderIdentityDTO | AuthTypes.ProviderIdentityDTO[]> {
|
||||||
|
const providerIdentities = await this.providerIdentityService_.create(
|
||||||
|
data,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<
|
||||||
|
AuthTypes.ProviderIdentityDTO[]
|
||||||
|
>(providerIdentities, {
|
||||||
|
populate: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async authenticate(
|
async authenticate(
|
||||||
provider: string,
|
provider: string,
|
||||||
authenticationData: AuthenticationInput
|
authenticationData: AuthenticationInput
|
||||||
|
|||||||
Reference in new issue
Block a user