-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add save token, add auth and anonym client
- Loading branch information
Showing
10 changed files
with
323 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,8 +74,22 @@ describe('Checking Customer Model', () => { | |
expect(customer).toHaveProperty('version'); | ||
}); | ||
|
||
it('should authenticate the customer', async () => { | ||
auth = await customerModel.authCustomer(user); | ||
expect(typeof auth).toBe('object'); | ||
expect(auth).toHaveProperty('addresses'); | ||
expect(auth).toHaveProperty('defaultBillingAddressId'); | ||
expect(auth).toHaveProperty('defaultShippingAddressId'); | ||
expect(auth).toHaveProperty('email'); | ||
expect(auth).toHaveProperty('firstName'); | ||
expect(auth).toHaveProperty('id'); | ||
expect(auth).toHaveProperty('lastName'); | ||
expect(auth).toHaveProperty('password'); | ||
expect(auth).toHaveProperty('version'); | ||
}); | ||
|
||
it('should edit the customer', async () => { | ||
if (customer) { | ||
if (auth) { | ||
editCustomer = await customerModel.editCustomer( | ||
[ | ||
CustomerModel.actionEditFirstName('John'), | ||
|
@@ -84,7 +98,7 @@ describe('Checking Customer Model', () => { | |
CustomerModel.actionAddAddress(address), | ||
CustomerModel.actionEditDateOfBirth('1990-01-01'), | ||
], | ||
customer, | ||
auth, | ||
); | ||
expect(typeof editCustomer).toBe('object'); | ||
expect(editCustomer).toHaveProperty('addresses'); | ||
|
@@ -128,39 +142,9 @@ describe('Checking Customer Model', () => { | |
} | ||
}); | ||
|
||
it('should found the customer by id', async () => { | ||
if (customer) { | ||
const getCustomer = await customerModel.getCustomerByID(customer.id); | ||
expect(typeof getCustomer).toBe('object'); | ||
expect(getCustomer).toHaveProperty('addresses'); | ||
expect(getCustomer).toHaveProperty('defaultBillingAddressId'); | ||
expect(getCustomer).toHaveProperty('defaultShippingAddressId'); | ||
expect(getCustomer).toHaveProperty('email'); | ||
expect(getCustomer).toHaveProperty('firstName'); | ||
expect(getCustomer).toHaveProperty('id'); | ||
expect(getCustomer).toHaveProperty('lastName'); | ||
expect(getCustomer).toHaveProperty('password'); | ||
expect(getCustomer).toHaveProperty('version'); | ||
} | ||
}); | ||
|
||
it('should authenticate the customer', async () => { | ||
auth = await customerModel.authCustomer({ email: '[email protected]', password: 'Qqq11' }); | ||
expect(typeof auth).toBe('object'); | ||
expect(auth).toHaveProperty('addresses'); | ||
expect(auth).toHaveProperty('defaultBillingAddressId'); | ||
expect(auth).toHaveProperty('defaultShippingAddressId'); | ||
expect(auth).toHaveProperty('email'); | ||
expect(auth).toHaveProperty('firstName'); | ||
expect(auth).toHaveProperty('id'); | ||
expect(auth).toHaveProperty('lastName'); | ||
expect(auth).toHaveProperty('password'); | ||
expect(auth).toHaveProperty('version'); | ||
}); | ||
|
||
it('should edit the customer password', async () => { | ||
if (auth) { | ||
editPassword = await customerModel.editPassword(auth, 'Qqq11', 'Qqq11'); | ||
if (editAddress) { | ||
editPassword = await customerModel.editPassword(editAddress, 'Qqq11', 'Qqq11'); | ||
expect(typeof editPassword).toBe('object'); | ||
expect(editPassword).toHaveProperty('addresses'); | ||
expect(editPassword).toHaveProperty('defaultBillingAddressId'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,167 @@ | ||
import type { UserCredentials } from '@/shared/types/user'; | ||
|
||
import { type ByProjectKeyRequestBuilder, createApiBuilderFromCtpClient } from '@commercetools/platform-sdk'; | ||
import { | ||
type AuthMiddlewareOptions, | ||
type Client, | ||
ClientBuilder, | ||
type HttpMiddlewareOptions, | ||
type Middleware, | ||
type PasswordAuthMiddlewareOptions, | ||
type RefreshAuthMiddlewareOptions, | ||
createAuthForAnonymousSessionFlow, | ||
createAuthForClientCredentialsFlow, | ||
createAuthForPasswordFlow, | ||
} from '@commercetools/sdk-client-v2'; | ||
|
||
import { TokenType } from '../types/type.ts'; | ||
import getTokenCache from './token-cache/token-cache.ts'; | ||
|
||
const URL_AUTH = 'https://auth.europe-west1.gcp.commercetools.com'; | ||
const URL_HTTP = 'https://api.europe-west1.gcp.commercetools.com'; | ||
const USE_SAVE_TOKEN = false; | ||
|
||
const httpMiddlewareOptions: HttpMiddlewareOptions = { | ||
fetch, | ||
host: URL_HTTP, | ||
}; | ||
|
||
export default function client(projectKey: string, clientID: string, clientSecret: string, scopes: string): Client { | ||
const scopesArr = scopes.split(','); | ||
const authMiddlewareOptions: AuthMiddlewareOptions = { | ||
credentials: { | ||
clientId: clientID, | ||
clientSecret, | ||
}, | ||
fetch, | ||
host: URL_AUTH, | ||
projectKey, | ||
scopes: scopesArr, | ||
}; | ||
|
||
return new ClientBuilder() | ||
.withProjectKey(projectKey) | ||
.withHttpMiddleware(httpMiddlewareOptions) | ||
.withClientCredentialsFlow(authMiddlewareOptions) | ||
.build(); | ||
export default class ApiClient { | ||
private adminConnection: ByProjectKeyRequestBuilder; | ||
|
||
private anonymConnection: ByProjectKeyRequestBuilder | null = null; | ||
|
||
private authConnection: ByProjectKeyRequestBuilder | null = null; | ||
|
||
private clientID: string; | ||
|
||
private clientSecret: string; | ||
|
||
private isAuth = false; | ||
|
||
private projectKey: string; | ||
|
||
private scopes: string[]; | ||
|
||
constructor(projectKey: string, clientID: string, clientSecret: string, scopes: string) { | ||
this.projectKey = projectKey; | ||
this.clientID = clientID; | ||
this.clientSecret = clientSecret; | ||
this.scopes = scopes.split(','); | ||
|
||
this.anonymConnection = this.createAnonymConnection(); | ||
|
||
const adminOptions = createAuthForClientCredentialsFlow({ | ||
credentials: { | ||
clientId: this.clientID, | ||
clientSecret: this.clientSecret, | ||
}, | ||
fetch, | ||
host: URL_AUTH, | ||
projectKey: this.projectKey, | ||
scopes: this.scopes, | ||
}); | ||
|
||
const adminClient = this.getAdminClient(adminOptions); | ||
|
||
this.adminConnection = this.getConnection(adminClient); | ||
} | ||
|
||
private getAdminClient(middleware: Middleware): Client { | ||
return new ClientBuilder() | ||
.withProjectKey(this.projectKey) | ||
.withHttpMiddleware(httpMiddlewareOptions) | ||
.withMiddleware(middleware) | ||
.build(); | ||
} | ||
|
||
private getAuthOption(credentials: UserCredentials): Middleware { | ||
const { email, password } = credentials || { email: '', password: '' }; | ||
const defaultOptions = this.getDefaultOptions(TokenType.AUTH); | ||
const authOptions: PasswordAuthMiddlewareOptions = { | ||
...defaultOptions, | ||
credentials: { | ||
...defaultOptions.credentials, | ||
user: { | ||
password, | ||
username: email, | ||
}, | ||
}, | ||
}; | ||
return createAuthForPasswordFlow(authOptions); | ||
} | ||
|
||
private getClient(middleware: Middleware, token: TokenType): Client { | ||
const defaultOptions = this.getDefaultOptions(token); | ||
const opt: RefreshAuthMiddlewareOptions = { | ||
...defaultOptions, | ||
refreshToken: token, | ||
}; | ||
return new ClientBuilder() | ||
.withProjectKey(this.projectKey) | ||
.withHttpMiddleware(httpMiddlewareOptions) | ||
.withMiddleware(middleware) | ||
.withRefreshTokenFlow(opt) | ||
.build(); | ||
} | ||
|
||
private getConnection(client: Client): ByProjectKeyRequestBuilder { | ||
return createApiBuilderFromCtpClient(client).withProjectKey({ projectKey: this.projectKey }); | ||
} | ||
|
||
private getDefaultOptions(tokenType: TokenType): AuthMiddlewareOptions { | ||
return { | ||
credentials: { | ||
clientId: this.clientID, | ||
clientSecret: this.clientSecret, | ||
}, | ||
fetch, | ||
host: URL_AUTH, | ||
projectKey: this.projectKey, | ||
scopes: this.scopes, | ||
tokenCache: USE_SAVE_TOKEN ? getTokenCache(tokenType) : undefined, | ||
}; | ||
} | ||
|
||
public adminRoot(): ByProjectKeyRequestBuilder { | ||
return this.adminConnection; | ||
} | ||
|
||
public apiRoot(): ByProjectKeyRequestBuilder { | ||
let client = this.authConnection && this.isAuth ? this.authConnection : this.anonymConnection; | ||
if (!client) { | ||
client = this.createAnonymConnection(); | ||
} | ||
return client; | ||
} | ||
|
||
public approveAuth(): boolean { | ||
this.isAuth = true; | ||
return this.isAuth; | ||
} | ||
|
||
public createAnonymConnection(): ByProjectKeyRequestBuilder { | ||
const defaultOptions = this.getDefaultOptions(TokenType.ANONYM); | ||
const anonymOptions = createAuthForAnonymousSessionFlow(defaultOptions); | ||
const anonymClient = this.getClient(anonymOptions, TokenType.ANONYM); | ||
this.anonymConnection = this.getConnection(anonymClient); | ||
|
||
return this.anonymConnection; | ||
} | ||
|
||
public createAuthConnection(credentials: UserCredentials): ByProjectKeyRequestBuilder { | ||
if (!this.authConnection || (this.authConnection && !this.isAuth)) { | ||
const authOptions = this.getAuthOption(credentials); | ||
const authClient = this.getClient(authOptions, TokenType.AUTH); | ||
this.authConnection = this.getConnection(authClient); | ||
} | ||
return this.authConnection; | ||
} | ||
|
||
public deleteAuthConnection(): boolean { | ||
this.authConnection = null; | ||
this.isAuth = false; | ||
this.anonymConnection = this.createAnonymConnection(); | ||
return this.authConnection === null; | ||
} | ||
} |
Oops, something went wrong.