Skip to content

Commit

Permalink
docs: auth examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Mar 28, 2023
1 parent 4c26708 commit a69d1f4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/content/2.features/1.useMedusaClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ Authenticating users with `authenticate` method:
```vue
<script lang="ts" setup>
const client = useMedusaClient();
await client.auth.authenticate({ email: '[email protected]', password: 'test1234' });
</script>
```

This will create a new cookie called `connect.sid` that will have a `httpOnly` flag set to true (which means that it is not accessible from the frontend JS code).

You can now fetch the data about currently logged in user like following:

```vue
<script lang="ts" setup>
const client = useMedusaClient();
const { customer } = await client.auth.getSession()
</script>
```
18 changes: 18 additions & 0 deletions docs/content/2.features/2.serverMedusaClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,21 @@ Finally, let's fetch this data in our Vue components by using `$fetch` like foll
const products = data.value.products
</script>
```

## Fetching data as authenticated user

To fetch the data as authenticated user (for example to get the data about currently logged in user) on the server you need to pass the cookies that are send from the browser as custom headers to the method like following:

```ts
import { serverMedusaClient } from '#medusa/server'

export default eventHandler(async (event) => {
const client = serverMedusaClient(event)

const { customer } = await client.auth.getSession({
Cookie: event.node.req.headers.cookie,
});

return customer
})
```
11 changes: 11 additions & 0 deletions playground/server/api/customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { serverMedusaClient } from '#medusa/server'

export default eventHandler(async (event) => {
const client = serverMedusaClient(event)

const { customer } = await client.auth.getSession({
Cookie: event.node.req.headers.cookie,
});

return customer
})

1 comment on commit a69d1f4

@vercel
Copy link

@vercel vercel bot commented on a69d1f4 Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nuxt-medusa – ./

nuxt-medusa-git-main-baroshem.vercel.app
nuxt-medusa-baroshem.vercel.app
nuxt-medusa.vercel.app

Please sign in to comment.