Skip to content

Commit

Permalink
docs: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
aiji42 committed Nov 22, 2021
1 parent a0b6afc commit 1ba727c
Showing 1 changed file with 70 additions and 35 deletions.
105 changes: 70 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ type Middleware = (request: NextRequest, event?: NextFetchEvent) => Response | u
import { makeIPInspector } from 'next-fortress'
import { NextRequest } from 'next/server'

export const middleware = (req: NextRequest) => {
// type IPs = string | Array<string>
// type makeIPInspector = (allowedIPs: IPs, fallback: Fallback) => Middleware
// IP can be specified in CIDR format. You can also specify multiple IPs in an array.
return makeIPInspector('123.123.123.123/32', {
type: 'redirect',
destination: '/'
})(req)
}
// type IPs = string | Array<string>
// type makeIPInspector = (allowedIPs: IPs, fallback: Fallback) => Middleware
// IP can be specified in CIDR format. You can also specify multiple IPs in an array.
export const middleware = makeIPInspector('123.123.123.123/32', {
type: 'redirect',
destination: '/'
})
```

### Control by Firebase
Expand All @@ -69,17 +67,14 @@ export const middleware = (req: NextRequest) => {
```ts
// /pages/mypage/_middleware.ts
import { makeFirebaseInspector } from 'next-fortress'
import { NextRequest } from 'next/server'

export const middleware = async (req: NextRequest) => {
// type makeFirebaseInspector = (fallback: Fallback) => AsyncMiddleware;
return makeFirebaseInspector(
{ type: 'redirect', destination: '/signin' }
)(req)
}
// type makeFirebaseInspector = (fallback: Fallback, customHandler?: (payload: any) => boolean) => AsyncMiddleware;
export const middleware = makeFirebaseInspector(
{ type: 'redirect', destination: '/signin' }
)
```

Put the Firebase user token into the cookie using the following example.
Put the Firebase user token into the cookie using the following example.
```ts
// cient side code (for example /pages/_app.tsx)
import { FIREBASE_COOKIE_KEY } from 'next-fortress/dist/constants'
Expand All @@ -99,24 +94,38 @@ firebase.auth().onAuthStateChanged(function (user) {
})
```

For the second argument of `makeFirebaseInspector`, you can pass a payload inspection function. This is useful, for example, if you want to ignore some authentication providers, or if you need to ensure that the email has been verified.
If this function returns false, it will enter the fallback case.
```ts
// /pages/mypage/_middleware.ts
import { makeFirebaseInspector } from 'next-fortress'

// Redirect for anonymous users.
export const middleware = makeFirebaseInspector(
{ type: 'redirect', destination: '/signin' },
(payload) => payload.firebase.sign_in_provider !== 'anonymous'
)
```

**NOTE**
- If you want to specify the cookie key, use the environment variable `FORTRESS_FIREBASE_COOKIE_KEY`.
- If you use [session cookies](https://firebase.google.com/docs/auth/admin/manage-cookies) to share authentication data with the server side, set the environment variable `FORTRESS_FIREBASE_MODE` to `session`.

### Control by Amazon Cognito

[example](https://next-fortress.vercel.app/cognito)

```ts
// /pages/mypage/_middleware.ts
import { makeCognitoInspector } from 'next-fortress'
import { NextRequest } from 'next/server'

export const middleware = async (req: NextRequest) => {
// type makeCognitoInspector =
// (fallback: Fallback, cognitoRegion: string, cognitoUserPoolId: string) => AsyncMiddleware;
return makeCognitoInspector(
{ type: 'redirect', destination: '/signin' },
process.env.COGNITO_REGION,
process.env.COGNITO_USER_POOL_ID
)(req)
}
// type makeCognitoInspector =
// (fallback: Fallback, cognitoRegion: string, cognitoUserPoolId: string, customHandler?: (payload: any) => boolean) => AsyncMiddleware;
export const middleware = makeCognitoInspector(
{ type: 'redirect', destination: '/signin' },
process.env.COGNITO_REGION,
process.env.COGNITO_USER_POOL_ID
)
```

Add `ssr: true` option to `Amplify.configure` to handle the Cognito cookies on the edge.
Expand All @@ -131,22 +140,34 @@ Amplify.configure({
})
```

For the 4th argument of `makeCognitoInspector`, you can pass a payload inspection function. This is useful, for example, if you want to ignore some authentication providers, or if you need to ensure that the email has been verified.
If this function returns false, it will enter the fallback case.
```ts
// /pages/mypage/_middleware.ts
import { makeCognitoInspector } from 'next-fortress'

// Fallback if the email address is not verified.
export const middleware = makeCognitoInspector(
{ type: 'redirect', destination: '/signin' },
process.env.COGNITO_REGION,
process.env.COGNITO_USER_POOL_ID,
(payload) => payload.email_verified
)
```

### Control by Auth0

[example](https://next-fortress.vercel.app/auth0)

```ts
// /pages/mypage/_middleware.ts
import { makeAuth0Inspector } from 'next-fortress'
import { NextRequest } from 'next/server'

export const middleware = async (req: NextRequest) => {
// type makeAuth0Inspector = (fallback: Fallback, apiEndpoint: string) => AsyncMiddleware;
return makeAuth0Inspector(
{ type: 'redirect', destination: '/singin' },
'/api/auth/me' // api endpoint for auth0 profile
)(req)
}
// type makeAuth0Inspector = (fallback: Fallback, apiEndpoint: string, customHandler?: (payload: any) => boolean) => AsyncMiddleware;
export const middleware = makeAuth0Inspector(
{ type: 'redirect', destination: '/singin' },
'/api/auth/me' // api endpoint for auth0 profile
)
```

To use Auth0, the api root must have an endpoint. [@auth0/nextjs-auth0](https://github.com/auth0/nextjs-auth0#basic-setup)
Expand All @@ -157,6 +178,20 @@ import { handleAuth } from '@auth0/nextjs-auth0'
export default handleAuth()
```

For the third argument of `makeAuth0Inspector`, you can pass a payload inspection function. This is useful, for example, if you need to ensure that the email has been verified.
If this function returns false, it will enter the fallback case.
```ts
// /pages/mypage/_middleware.ts
import { makeAuth0Inspector } from 'next-fortress'

// Fallback if the email address is not verified.
export const middleware = makeAuth0Inspector(
{ type: 'redirect', destination: '/singin' },
'/api/auth/me',
(payload) => payload.email_verified
)
```

## Contributing
Please read [CONTRIBUTING.md](https://github.com/aiji42/next-fortress/blob/main/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

Expand Down

0 comments on commit 1ba727c

Please sign in to comment.