Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] add setToken auth resource in js sdk #7053

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions packages/medusa-js/src/resources/admin/auth.ts
Expand Up @@ -6,20 +6,20 @@ import BaseResource from "../base"
/**
* This class is used to send requests to [Admin Auth API Routes](https://docs.medusajs.com/api/admin#auth_getauth). All its method
* are available in the JS Client under the `medusa.admin.auth` property.
*
*
* The methods in this class allow admin users to manage their session, such as login or log out.
* You can send authenticated requests for an admin user either using the Cookie header, their API token, or the JWT Token.
* When you log the admin user in using the {@link createSession} method, the JS client will automatically attach the
* cookie header in all subsequent requests.
*
*
* Related Guide: [How to implement user profiles](https://docs.medusajs.com/modules/users/admin/manage-profile).
*/
class AdminAuthResource extends BaseResource {
/**
* Get the currently logged in user's details. Can also be used to check if there is an authenticated user.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<AdminAuthRes>} Resolves to the logged-in user's details.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -41,7 +41,7 @@ class AdminAuthResource extends BaseResource {
* the user is still authorized to perform admin functionalities in other API Routes.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<void>} Resolves when user is logged out successfully.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -60,7 +60,7 @@ class AdminAuthResource extends BaseResource {
* @param {AdminPostAuthReq} payload - The credentials of the user.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<AdminAuthRes>} Resolves to the user's details.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -85,7 +85,7 @@ class AdminAuthResource extends BaseResource {
* @param {AdminPostAuthReq} payload - The credentials of the user.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<AdminBearerAuthRes>} Resolves to the access token of the user, if they're authenticated successfully.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -105,10 +105,23 @@ class AdminAuthResource extends BaseResource {
return this.client.request("POST", path, payload, {}, customHeaders)
.then((res) => {
JwtTokenManager.registerJwt(res.access_token, "admin");

return res
});
}

/**
* Set a store JWT token to be sent with each request.
* @param access_token - The JWT token to set.
* @returns void
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* medusa.admin.auth.setToken("my-super-access-token")
*/
setToken(access_token: string): void {
JwtTokenManager.registerJwt(access_token, "admin");
}
}

export default AdminAuthResource
37 changes: 25 additions & 12 deletions packages/medusa-js/src/resources/auth.ts
Expand Up @@ -11,12 +11,12 @@ import BaseResource from "./base"
/**
* This class is used to send requests to [Store Auth API Routes](https://docs.medusajs.com/api/store#auth). All its method
* are available in the JS Client under the `medusa.auth` property.
*
*
* The methods in this class allows you to manage a customer's session, such as login or log out.
* You can send authenticated requests for a customer either using the Cookie header or using the JWT Token.
* When you log the customer in using the {@link authenticate} method, the JS client will automatically attach the
* cookie header in all subsequent requests.
*
*
* Related Guide: [How to implement customer profiles in your storefront](https://docs.medusajs.com/modules/customers/storefront/implement-customer-profiles).
*/
class AuthResource extends BaseResource {
Expand All @@ -25,7 +25,7 @@ class AuthResource extends BaseResource {
* @param {StorePostAuthReq} payload - The credentials of the customer to authenticate.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<StoreAuthRes>} Resolves to the customer's details.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -46,7 +46,7 @@ class AuthResource extends BaseResource {
* Log out the customer and remove their authentication session. This method requires {@link AuthResource.authenticate | customer authentication}.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<void>} Resolves when customer is logged out successfully.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -55,7 +55,7 @@ class AuthResource extends BaseResource {
* // customer logged out successfully
* })
*/
deleteSession(customHeaders: Record<string, any> = {}): ResponsePromise<void> {
deleteSession(customHeaders: Record<string, any> = {}): ResponsePromise<void> {
const path = `/store/auth`
return this.client.request("DELETE", path, {}, {}, customHeaders)
}
Expand All @@ -65,7 +65,7 @@ class AuthResource extends BaseResource {
* This method requires {@link AuthResource.authenticate | customer authentication}.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<StoreAuthRes>} Resolves to the customer's details.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -85,7 +85,7 @@ class AuthResource extends BaseResource {
* @param {string} email - The email to check.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<StoreGetAuthEmailRes>} Resolves to the result of the check.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -101,7 +101,7 @@ class AuthResource extends BaseResource {
* @param {AdminPostAuthReq} payload - The credentials of the customer to authenticate.
* @param {Record<string, any>} customHeaders - Custom headers to attach to the request.
* @returns {ResponsePromise<StoreBearerAuthRes>} Resolves to the access token of the customer, if they're authenticated successfully.
*
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
Expand All @@ -115,15 +115,28 @@ class AuthResource extends BaseResource {
*/
getToken(
payload: StorePostAuthReq,
customHeaders: Record<string, any> = {}
customHeaders: Record<string, any> = {},
): ResponsePromise<StoreBearerAuthRes> {
const path = `/store/auth/token`
return this.client.request("POST", path, payload, {}, customHeaders)
.then((res) => {
JwtTokenManager.registerJwt(res.access_token, "store");

JwtTokenManager.registerJwt(res.access_token, "store")
return res
});
})
}

/**
* Set a store JWT token to be sent with each request.
* @param access_token - The JWT token to set.
* @returns void
*
* @example
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* medusa.auth.setToken("my-super-access-token")
*/
setToken(access_token: string): void {
JwtTokenManager.registerJwt(access_token, "store")
}
}

Expand Down