Skip to content

Commit

Permalink
docs: add code samples to session checking
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 25, 2020
1 parent 9ad73b8 commit eba8eda
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/docs/guides/login-session.mdx
Expand Up @@ -3,6 +3,9 @@ id: login-session
title: Configuring And Checking for Login Sessions
---

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'

A login session is created when a user signs in. The session is stored as a
cookie or as a token, depending on the interaction type.

Expand Down Expand Up @@ -94,6 +97,41 @@ A typical session payload will look like this:
}
```

#### Code Examples

<Tabs
defaultValue="nodejs"
values={[
{label: 'ExpressJS', value: 'nodejs'},
]}>
<TabItem value="nodejs">

```js
import { PublicApi } from '@oryd/kratos-client'

const publicEndpoint = new PublicApi(config.kratos.public)
const protect = (req: Request, res: Response, next: NextFunction) => {
req.headers['host'] = config.kratos.public.split('/')[2]
publicEndpoint
.whoami(req)
.then(({ body, response }) => {
req.user = { session: body }
next()
})
.catch(() => {
// Redirect to login if not logged in
res.redirect('/auth/login')
})

// const app = expres()
// ...

app.get('/', protect, dashboard)
```
</TabItem>
</Tabs>
### API Client
API clients receive and use ORY Kratos Session Tokens which can be checked by calling the `/sessions/whoami` endpoint
Expand Down

0 comments on commit eba8eda

Please sign in to comment.