Files
medusa-store/www/apps/docs/content/references/js-client/classes/AdminAuthResource.mdx
Shahed Nasser c51dce164d docs: general fixes to references (#5653)
* fixed typedoc plugin's escape strategy

* move props comments to the associated property

* regenerate references
2023-11-17 19:36:58 +02:00

502 lines
15 KiB
Plaintext

---
displayed_sidebar: jsClientSidebar
slug: /references/js-client/AdminAuthResource
---
import ParameterTypes from "@site/src/components/ParameterTypes"
# AdminAuthResource
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 [createSession](AdminAuthResource.mdx#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).
## Methods
### createSession
Log a User in using their credentials. If the user is authenticated successfully, the cookie is automatically attached to subsequent requests sent with the JS Client.
#### Example
```ts
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
medusa.admin.AdminAuthResource.createSession({
email: "user@example.com",
password: "supersecret",
}).then(({ user }) => {
console.log(user.id)
})
```
#### Parameters
<ParameterTypes parameters={[
{
"name": "payload",
"type": "[AdminPostAuthReq](../internal/classes/internal.AdminPostAuthReq.mdx)",
"description": "The credentials of the user.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": [
{
"name": "email",
"type": "`string`",
"description": "The user's email.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "password",
"type": "`string`",
"description": "The user's password.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]
},
{
"name": "customHeaders",
"type": "`Record<string, any>`",
"description": "Custom headers to attach to the request.",
"optional": false,
"defaultValue": "{}",
"expandable": false,
"children": []
}
]} />
#### Returns
<ParameterTypes parameters={[
{
"name": "ResponsePromise",
"type": "[ResponsePromise](../internal/types/internal.ResponsePromise.mdx)&#60;[AdminAuthRes](../internal/types/internal.AdminAuthRes.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "Resolves to the user's details.",
"expandable": false,
"children": [
{
"name": "AdminAuthRes",
"type": "`object`",
"description": "The user's details.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": [
{
"name": "user",
"type": "[Omit](../internal/types/internal.Omit.mdx)&#60;[User](../internal/classes/internal.User.mdx), \"password_hash\"&#62;",
"description": "User details.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": [
{
"name": "api_token",
"type": "`string`",
"description": "An API token associated with the user.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "created_at",
"type": "`Date`",
"description": "The date with timezone at which the resource was created.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "deleted_at",
"type": "`null` \\| `Date`",
"description": "The date with timezone at which the resource was deleted.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "email",
"type": "`string`",
"description": "The email of the User",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "first_name",
"type": "`string`",
"description": "The first name of the User",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "id",
"type": "`string`",
"description": "The user's ID",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "last_name",
"type": "`string`",
"description": "The last name of the User",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "metadata",
"type": "`Record<string, unknown>`",
"description": "An optional key-value map with additional details",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "role",
"type": "[UserRoles](../internal/enums/internal.internal.UserRoles.mdx)",
"description": "The user's role. These roles don't provide any different privileges.",
"optional": false,
"defaultValue": "member",
"expandable": false,
"children": []
},
{
"name": "updated_at",
"type": "`Date`",
"description": "The date with timezone at which the resource was updated.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]
}
]
}
]
}
]} />
___
### deleteSession
Log out the user and remove their authentication session. This will only work if you're using Cookie session for authentication. If the API token is still passed in the header,
the user is still authorized to perform admin functionalities in other API Routes.
#### Example
```ts
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in
medusa.admin.auth.deleteSession()
```
#### Parameters
<ParameterTypes parameters={[
{
"name": "customHeaders",
"type": "`Record<string, any>`",
"description": "Custom headers to attach to the request.",
"optional": false,
"defaultValue": "{}",
"expandable": false,
"children": []
}
]} />
#### Returns
<ParameterTypes parameters={[
{
"name": "ResponsePromise",
"type": "[ResponsePromise](../internal/types/internal.ResponsePromise.mdx)&#60;void&#62;",
"optional": false,
"defaultValue": "",
"description": "Resolves when user is logged out successfully.",
"expandable": false,
"children": []
}
]} />
___
### getSession
Get the currently logged in user's details. Can also be used to check if there is an authenticated user.
#### Example
```ts
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.auth.getSession().then(({ user }) => {
console.log(user.id)
})
```
#### Parameters
<ParameterTypes parameters={[
{
"name": "customHeaders",
"type": "`Record<string, any>`",
"description": "Custom headers to attach to the request.",
"optional": false,
"defaultValue": "{}",
"expandable": false,
"children": []
}
]} />
#### Returns
<ParameterTypes parameters={[
{
"name": "ResponsePromise",
"type": "[ResponsePromise](../internal/types/internal.ResponsePromise.mdx)&#60;[AdminAuthRes](../internal/types/internal.AdminAuthRes.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "Resolves to the logged-in user's details.",
"expandable": false,
"children": [
{
"name": "AdminAuthRes",
"type": "`object`",
"description": "The user's details.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": [
{
"name": "user",
"type": "[Omit](../internal/types/internal.Omit.mdx)&#60;[User](../internal/classes/internal.User.mdx), \"password_hash\"&#62;",
"description": "User details.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": [
{
"name": "api_token",
"type": "`string`",
"description": "An API token associated with the user.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "created_at",
"type": "`Date`",
"description": "The date with timezone at which the resource was created.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "deleted_at",
"type": "`null` \\| `Date`",
"description": "The date with timezone at which the resource was deleted.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "email",
"type": "`string`",
"description": "The email of the User",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "first_name",
"type": "`string`",
"description": "The first name of the User",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "id",
"type": "`string`",
"description": "The user's ID",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "last_name",
"type": "`string`",
"description": "The last name of the User",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "metadata",
"type": "`Record<string, unknown>`",
"description": "An optional key-value map with additional details",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "role",
"type": "[UserRoles](../internal/enums/internal.internal.UserRoles.mdx)",
"description": "The user's role. These roles don't provide any different privileges.",
"optional": false,
"defaultValue": "member",
"expandable": false,
"children": []
},
{
"name": "updated_at",
"type": "`Date`",
"description": "The date with timezone at which the resource was updated.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]
}
]
}
]
}
]} />
___
### getToken
Authenticate the user and retrieve a JWT token to use for subsequent authenticated requests.
#### Example
```ts
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
medusa.admin.auth
.getToken({
email: "user@example.com",
password: "supersecret",
})
.then(({ access_token }) => {
console.log(access_token)
})
```
#### Parameters
<ParameterTypes parameters={[
{
"name": "payload",
"type": "[AdminPostAuthReq](../internal/classes/internal.AdminPostAuthReq.mdx)",
"description": "The credentials of the user.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": [
{
"name": "email",
"type": "`string`",
"description": "The user's email.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "password",
"type": "`string`",
"description": "The user's password.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]
},
{
"name": "customHeaders",
"type": "`Record<string, any>`",
"description": "Custom headers to attach to the request.",
"optional": false,
"defaultValue": "{}",
"expandable": false,
"children": []
}
]} />
#### Returns
<ParameterTypes parameters={[
{
"name": "ResponsePromise",
"type": "[ResponsePromise](../internal/types/internal.ResponsePromise.mdx)&#60;[AdminBearerAuthRes](../internal/types/internal.AdminBearerAuthRes.mdx)&#62;",
"optional": false,
"defaultValue": "",
"description": "Resolves to the access token of the user, if they're authenticated successfully.",
"expandable": false,
"children": [
{
"name": "AdminBearerAuthRes",
"type": "`object`",
"description": "The access token of the user, if they're authenticated successfully.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": [
{
"name": "access_token",
"type": "`string`",
"description": "Access token that can be used to send authenticated requests.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]
}
]
}
]} />