Skip to content

Commit

Permalink
update pr with return type
Browse files Browse the repository at this point in the history
  • Loading branch information
pKorsholm committed Jan 17, 2024
1 parent 0d5332b commit ae8a5a1
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ export async function createAuthUsers(
userData: any[] = [
{
id: "test-id",
entity_id: "test-id",
provider: "manual",
},
{
id: "test-id-1",
entity_id: "test-id-1",
provider: "manual",
},
{
entity_id: "test-id-2",
provider: "store",
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ describe("AuthUser Service", () => {
{
id: "test",
provider_id: "manual",
entity_id: "test"
},
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ describe("AuthenticationModuleService - AuthUser", () => {
{
id: "test",
provider_id: "manual",
entity_id: "test"
},
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ describe("AuthenticationModuleService - AuthProvider", () => {
},
])

let error
const { success, error } = await service.authenticate("notRegistered", {})

try {
await service.authenticate("notRegistered", {})
} catch (err) {
error = err
}

expect(error.message).toEqual(
expect(success).toBe(false)
expect(error).toEqual(
"AuthenticationProvider with for provider: notRegistered wasn't registered in the module. Have you configured your options correctly?"
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,25 @@ describe("AuthenticationModuleService - AuthProvider", () => {
// Add authenticated user
{
provider: "usernamePassword",
entity_id: email,
provider_metadata: {
email,
password: passwordHash,
},
},
])

const res = await service.authenticate("usernamePassword", {
email: "test@test.com",
password: password,
body: {
email: "test@test.com",
password: password,
},
})

expect(res).toEqual({
success: true,
authUser: expect.objectContaining({
entity_id: email,
provider_metadata: {
email,
password_hash: passwordHash,
},
}),
})
Expand All @@ -78,7 +79,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
await seedDefaultData(testManager)

const res = await service.authenticate("usernamePassword", {
email: "test@test.com",
body: { email: "test@test.com" },
})

expect(res).toEqual({
Expand All @@ -91,7 +92,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
await seedDefaultData(testManager)

const res = await service.authenticate("usernamePassword", {
password: "supersecret",
body: { password: "supersecret" },
})

expect(res).toEqual({
Expand All @@ -112,16 +113,18 @@ describe("AuthenticationModuleService - AuthProvider", () => {
// Add authenticated user
{
provider: "usernamePassword",
entity_id: email,
provider_metadata: {
email,
password_hash: passwordHash,
},
},
])

const res = await service.authenticate("usernamePassword", {
email: "test@test.com",
password: "password",
body: {
email: "test@test.com",
password: "password",
},
})

expect(res).toEqual({
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json",
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
"test": "jest --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts",
"test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.ts",
"test:integration": "jest --runInBand --forceExit -- ",
"test:integration:single": "jest --runInBand --forceExit ",
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
Expand Down
4 changes: 3 additions & 1 deletion packages/authentication/src/providers/username-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ class UsernamePasswordProvider extends AbstractAuthenticationModuleProvider {
UsernamePasswordProvider.PROVIDER
)

const password_hash = authUser.provider_metadata?.password_hash
const password_hash = authUser.provider_metadata?.password

if (password_hash && typeof password_hash === "string") {
const buf = Buffer.from(password_hash, "base64")

const success = await Scrypt.verify(buf, password)

if (success) {
delete authUser.provider_metadata!.password

return { success, authUser: JSON.parse(JSON.stringify(authUser)) }
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/authentication/src/services/authentication-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,10 @@ export default class AuthenticationModuleService<
): AbstractAuthenticationModuleProvider {
let containerProvider: AbstractAuthenticationModuleProvider
try {
console.log("containerProvider")
containerProvider = this.__container__[`auth_provider_${provider}`]
} catch (error) {
console.log(error)
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`AuthenticationProvider with for provider: ${provider} wasn't registered in the module. Have you configured your options correctly?`
Expand All @@ -369,14 +371,17 @@ export default class AuthenticationModuleService<
@MedusaContext() sharedContext: Context = {}
): Promise<AuthenticationResponse> {
let registeredProvider
console.log("hello")
try {
await this.retrieveAuthProvider(provider, {})

console.log(registeredProvider)
registeredProvider = this.getRegisteredAuthenticationProvider(provider)

return await registeredProvider.authenticate(authenticationData)
} catch (error) {
console.log(JSON.stringify(error, null, 2))
return { success: false, error: error.message }
}

return await registeredProvider.authenticate(authenticationData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AuthUser } from "@models"

export type CreateAuthUserDTO = {
provider_id: string
entity_id: string
provider_metadata?: Record<string, unknown>
user_metadata?: Record<string, unknown>
app_metadata?: Record<string, unknown>
Expand Down
2 changes: 2 additions & 0 deletions packages/authentication/src/types/services/auth-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { AuthProviderDTO } from "./auth-provider"
export type AuthUserDTO = {
id: string
provider_id: string
entity_id: string
provider: AuthProviderDTO
provider_metadata?: Record<string, unknown>
user_metadata: Record<string, unknown>
app_metadata: Record<string, unknown>
}

export type CreateAuthUserDTO = {
entity_id: string
provider_id: string
provider_metadata?: Record<string, unknown>
user_metadata?: Record<string, unknown>
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/authentication/common/auth-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AuthProviderDTO } from "./auth-provider"
export type AuthUserDTO = {
id: string
provider_id: string
entity_id: string
provider: AuthProviderDTO
provider_metadata?: Record<string, unknown>
user_metadata: Record<string, unknown>
Expand All @@ -12,6 +13,7 @@ export type AuthUserDTO = {

export type CreateAuthUserDTO = {
provider_id: string
entity_id: string
provider_metadata?: Record<string, unknown>
user_metadata?: Record<string, unknown>
app_metadata?: Record<string, unknown>
Expand Down

0 comments on commit ae8a5a1

Please sign in to comment.