Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
dd867bf
feat: Add initial project files and configurations
relusion Sep 7, 2024
eea2457
refactor: Remove unused code and imports in backend files
relusion Sep 8, 2024
0558ca1
refactor: Update frontend dependencies
relusion Sep 8, 2024
58dd766
feat: Add role-based authorization to KeystoneJS Azure Static Web App…
relusion Sep 8, 2024
e7987fb
refactor: Update backend session implementation for role-based author…
relusion Sep 8, 2024
1d11416
refactor: Update README.md for Azure Static Web Apps example
relusion Sep 8, 2024
f7312a7
refactor: Update KeystoneJS backend to include role-based authorization
relusion Sep 8, 2024
b5f3880
feat: Add role-based authorization tests to React frontend
relusion Sep 8, 2024
3ac17c6
refactor: Update frontend package.json dependencies
relusion Sep 8, 2024
65b1966
refactor: Update frontend package.json dependencies
relusion Sep 8, 2024
3bfc17c
Remove package-lock.json as requested
relusion Sep 8, 2024
efd5cac
Update examples/custom-session-az-swa/frontend/package.json
relusion Sep 9, 2024
bebd539
Update examples/custom-session-az-swa/backend/package.json
relusion Sep 9, 2024
3ffc04c
Remove unused files: reportWebVitals.js and setupTests.js
relusion Sep 9, 2024
234dc65
Refactor package.json files in examples/custom-session-az-swa
relusion Sep 9, 2024
b8b128a
Merge branch 'main' into feature/az-swa-session-implementation
relusion Sep 9, 2024
a04c9a7
updated pnpm-lock.yaml
relusion Sep 9, 2024
57b6ce5
Merge branch 'main' into feature/az-swa-session-implementation
relusion Sep 29, 2024
fc5e775
pnpm lock refresh
relusion Sep 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions examples/custom-session-az-swa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# KeystoneJS [Azure Static Web Apps](https://learn.microsoft.com/en-us/azure/static-web-apps/overview) Example

This project demonstrates how to integrate a KeystoneJS backend with a React frontend, deployed on Azure Static Web Apps. It showcases a custom session implementation using Azure's authentication headers. Additionally, it demonstrates how to use roles for authorization on the backend.

## Features

- React frontend with login/logout functionality, and role-based authorization tests
- KeystoneJS backend with authorization based on roles
- Custom session implementation using `x-ms-client-principal` header

## Prerequisites

- Node.js (v14 or later)
- npm or yarn
- An Azure account with an active subscription
- Azure CLI (optional, for deployment)

## Project Structure

```
custom-session-az-swa/
├── frontend/
│ ├── src/
│ │ └── App.js
│ └── package.json
│ └── staticwebapp.config.json
├── backend/
│ ├── keystone.ts
│ ├── schema.ts
│ ├── session.ts
│ └── package.json
├── staticwebapp.config.json
└── README.md
└── swa-cli.config.json
```

## Setup and Installation
1. Install Azure Static Web Apps CLI https://azure.github.io/static-web-apps-cli/docs/use/install. It's required for local execution and testing.
2. Clone the repository:
```
git clone https://github.com/keystonejs/keystone.git
cd examples/custom-session-az-swa
```

3. Install dependencies:
```
cd frontend && npm install
cd ../backend && npm install
```

4. Start the backend:
```
cd backend
npm run dev
```

5. In a new terminal, start the frontend:
```
swa start
```

7. Visit `http://localhost:4280` to see the application running locally.

## Custom Session Implementation

The custom session implementation in `backend/session.ts` uses the `x-ms-client-principal` header provided by Azure Static Web Apps. This header contains encoded user information when a user is authenticated.

## Roles and Authorization
**NOTE: This is a simple example to demonstrate how to use roles for authorization. In a production application, you should use a more secure method to manage roles and permissions.**

The following roles are defined in `backend/session.ts`:
- `blogReader`: Can only read posts
- `blogContributor`: Can read, create, update, and delete posts
- `admin`: Can perform all operations

- Roles are used for authorization on the **Post** list.
```
operation: {
query: ({ session}) => isBlogReader({ session }) || isBlogContributor({ session }),
create: isBlogContributor,
update: isBlogContributor,
delete: isBlogContributor,
},
```

## Important Notes

- Ensure all API routes are prefixed with `/api` in your frontend requests.
- Because backend is "hiden" under a reverse proxy under /api sub path, the Admin dashboard is moved to /api/admin,

- The local development environment may not fully replicate the Azure Static Web Apps authentication. Always test in a staging environment before deploying to production.

23 changes: 23 additions & 0 deletions examples/custom-session-az-swa/backend/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { config } from '@keystone-6/core'
import { lists } from './schema'
import type { TypeInfo } from '.keystone/types'
import { AzStaticWebAppAuthSessionStrategy } from './session'

export default config<TypeInfo>({
server:
{
port: process.env.PORT ? parseInt(process.env.PORT, 10) : 3001,
},
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL ?? 'file:./keystone-example.db',

// WARNING: this is only needed for our monorepo examples, dont do this
prismaClientPath: 'node_modules/myprisma',
},
lists,
session: AzStaticWebAppAuthSessionStrategy,
ui: {
basePath: '/api/admin', // move admin ui under api path
}
})
Loading