Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zett-8/express-prisma-auth0

Secure API with Express, Prisma and Auth0
https://github.com/zett-8/express-prisma-auth0

auth0 authentication express node prisma rest-api

Last synced: about 1 month ago
JSON representation

Secure API with Express, Prisma and Auth0

Awesome Lists containing this project

README

        

# Express + Prisma + Auth0

## [💻 Demo](https://express-prisma-auth0.herokuapp.com/)

## Requirements

[Express](https://expressjs.com/)
[Prisma](https://www.prisma.io/)
[Auth0](https://auth0.com/)

## Protect endpoints
Tricky part is here

```js
import jwt from 'express-jwt'
import jwksRsa from 'jwks-rsa'

const auth0Config = {
issuer: process.env.AUTH0_ISSUER,
audience: process.env.AUTH0_AUDIENCE,
algorithms: ['RS256'],
}

export const jwtCheck = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 15,
jwksUri: `${auth0Config.issuer}.well-known/jwks.json`,
}),
...auth0Config,
})
```

Then use it for endpoints where you want to protect.
Only Authenticated users can implement the endpoint with Bearer Token.

```js
import { jwtCheck } from './jwtCheck'

app.get('/sample', jwtCheck, (req, res) => {
res.sendStatus(200)
})
```

All required environment variables are the following,

```text
AUTH0_DOMAIN=*******.us.auth0.com
AUTH0_CLIENT_ID=*******
AUTH0_ISSUER=https://*******.us.auth0.com/
AUTH0_AUDIENCE=https://*******.us.auth0.com/api/v2/
```

You can find them on Auth0 dashboard,
```text
DOMAIN: Auth0 dashboard -> Applications -> Settings -> Domain
CLIENT_ID: Auth0 dashboard -> Applications -> Settings -> Client ID
ISSUER: https:///
AUDIENCE: Auth0 dashboard -> APIs -> API Audience
```

*Do not forget to put trailing slash at the end of ISSUER and AUDIENCE.
Don't know why but without trailing slash, it didn't work well in my case.

## Prisma commands

### Migrate in Dev environment
```shell
prisma migrate dev
```
### Create migrations
```shell
prisma migrate dev --create-only
```

### Apply all migration files to prod
```shell
prisma migrate deploy
```

### Seed DB
add to package.json
```json
{
"prisma": {
"seed": "ts-node ./prisma/seed.ts"
}
}
```

```shell
prisma db seed
```

### Reset DB
*This command should be run only on localhost
*Delete all data and populate DB with seed data
```shell
prisma migrate reset
```

### Generate prisma client
*need to run this command after editing schema
```shell
prisma generate
```