Skip to content

Commit

Permalink
Merge pull request #207 from aiji42/v5.0.0
Browse files Browse the repository at this point in the history
V5.0.0
  • Loading branch information
aiji42 authored Jul 11, 2022
2 parents 8c8eecd + a36ecc9 commit ca2b1f1
Show file tree
Hide file tree
Showing 30 changed files with 2,828 additions and 9,067 deletions.
7 changes: 0 additions & 7 deletions .eslintrc

This file was deleted.

6 changes: 0 additions & 6 deletions .github/dependabot.yml

This file was deleted.

56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CI
on:
push:
branches:
- main
- beta
pull_request:
branches:
- "*"

jobs:
Test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 16.x
registry-url: https://registry.npmjs.org
- uses: actions/cache@v2
with:
path: "**/node_modules"
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: Install dependencies 💿
run: yarn install --frozen-lockfile
- name: Run Tests 🧪
run: yarn test:coverage
- name: Report coverage (client) 📏
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}

Publish:
runs-on: ubuntu-latest
needs:
- Test
if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/beta' }}
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 16.x
registry-url: https://registry.npmjs.org
- uses: actions/cache@v2
with:
path: "**/node_modules"
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: Install dependencies 💿
run: yarn install --frozen-lockfile
- name: Publish 🚀
run: yarn run semantic-release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23 changes: 0 additions & 23 deletions .github/workflows/main.yml

This file was deleted.

25 changes: 0 additions & 25 deletions .github/workflows/release.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ npm-debug.log*
coverage
build
dist
/*.d.ts

# log
*.log
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn commitlint --edit
yarn commitlint --edit
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn format && yarn lint --max-warnings=0
npx lint-staged
58 changes: 43 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type Middleware = (request: NextRequest, event?: NextFetchEvent) => Response | u
[example](https://next-fortress.vercel.app/ip)

```ts
// /pages/admin/_middleware.ts
import { makeIPInspector } from 'next-fortress'
// middleware.ts
import { makeIPInspector } from 'next-fortress/ip'

/*
type IPs = string | Array<string>
Expand All @@ -58,6 +58,10 @@ export const middleware = makeIPInspector('123.123.123.123/32', {
type: 'redirect',
destination: '/'
})

export const config = {
matcher: ['/admin/:path*'],
}
```

### Control by Firebase
Expand All @@ -66,8 +70,8 @@ export const middleware = makeIPInspector('123.123.123.123/32', {


```ts
// /pages/mypage/_middleware.ts
import { makeFirebaseInspector } from 'next-fortress'
// middleware.ts
import { makeFirebaseInspector } from 'next-fortress/firebase'

/*
type makeFirebaseInspector = (
Expand All @@ -78,12 +82,16 @@ import { makeFirebaseInspector } from 'next-fortress'
export const middleware = makeFirebaseInspector(
{ type: 'redirect', destination: '/signin' }
)

export const config = {
matcher: ['/mypage/:path*'],
}
```

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'
import { FIREBASE_COOKIE_KEY } from 'next-fortress/constants'

firebase.auth().onAuthStateChanged(function (user) {
if (user) {
Expand All @@ -103,14 +111,18 @@ 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'
// middleware.ts
import { makeFirebaseInspector } from 'next-fortress/firebase'

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

export const config = {
matcher: ['/mypage/:path*'],
}
```

**NOTE**
Expand All @@ -122,8 +134,8 @@ export const middleware = makeFirebaseInspector(
[example](https://next-fortress.vercel.app/cognito)

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

/*
type UserPoolParams = {
Expand All @@ -146,6 +158,10 @@ export const middleware = makeCognitoInspector(
userPoolWebClientId: process.env.COGNITO_USER_POOL_WEB_CLIENT_ID,
}
)

export const config = {
matcher: ['/mypage/:path*'],
}
```

Add `ssr: true` option to `Amplify.configure` to handle the Cognito cookies on the edge.
Expand All @@ -163,8 +179,8 @@ Amplify.configure({
For the 3rd 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'
// middleware.ts
import { makeCognitoInspector } from 'next-fortress/cognito'

// Fallback if the email address is not verified.
export const middleware = makeCognitoInspector(
Expand All @@ -176,15 +192,19 @@ export const middleware = makeCognitoInspector(
},
(payload) => payload.email_verified
)

export const config = {
matcher: ['/mypage/:path*'],
}
```

### Control by Auth0

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

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

/*
type makeAuth0Inspector = (
Expand All @@ -197,6 +217,10 @@ export const middleware = makeAuth0Inspector(
{ type: 'redirect', destination: '/singin' },
'/api/auth/me' // api endpoint for auth0 profile
)

export const config = {
matcher: ['/mypage/:path*'],
}
```

To use Auth0, the api root must have an endpoint. [@auth0/nextjs-auth0](https://github.com/auth0/nextjs-auth0#basic-setup)
Expand All @@ -210,15 +234,19 @@ 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'
// middleware.ts
import { makeAuth0Inspector } from 'next-fortress/auth0'

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

export const config = {
matcher: ['/mypage/:path*'],
}
```

## Contributing
Expand Down
6 changes: 0 additions & 6 deletions babel.config.cjs

This file was deleted.

24 changes: 24 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { buildSync } from 'esbuild'
import * as fs from 'fs'

const readdirRecursively = (dir, files = []) => {
const entries = fs.readdirSync(dir, { withFileTypes: true })
const dirs = []
for (const dirent of entries) {
if (dirent.isDirectory()) dirs.push(`${dir}/${dirent.name}`)
if (dirent.isFile()) files.push(`${dir}/${dirent.name}`)
}
for (const d of dirs) {
files = readdirRecursively(d, files)
}
return files
}

buildSync({
entryPoints: readdirRecursively('./src').filter(
(n) => n.match(/\.(ts|tsx)$/) && !n.includes('__tests__')
),
outdir: './dist',
format: 'esm',
target: 'esnext'
})
24 changes: 12 additions & 12 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
"lint": "next lint"
},
"dependencies": {
"@auth0/nextjs-auth0": "^1.7.0",
"@auth0/nextjs-auth0": "^1.9.1",
"@geist-ui/react": "^2.2.5",
"@geist-ui/react-icons": "^1.0.1",
"aws-amplify": "^4.3.20",
"firebase": "^9.7.0",
"firebase-admin": "^10.1.0",
"aws-amplify": "^4.3.27",
"firebase": "^9.9.0",
"firebase-admin": "^11.0.0",
"js-cookie": "^3.0.1",
"next": "12.1.5",
"next-fortress": "4.0.1-beta.1",
"next": "12.2.2",
"next-fortress": "5.0.0-beta.1",
"nookies": "^2.5.2",
"react": "^18.1.0",
"react-dom": "18.1.0"
"react": "^18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/js-cookie": "^3.0.2",
"@types/react": "18.0.8",
"eslint": "8.14.0",
"eslint-config-next": "12.1.5",
"typescript": "4.6.4"
"@types/react": "18.0.15",
"eslint": "8.19.0",
"eslint-config-next": "12.2.2",
"typescript": "4.7.4"
}
}
Loading

1 comment on commit ca2b1f1

@vercel
Copy link

@vercel vercel bot commented on ca2b1f1 Jul 11, 2022

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:

next-fortress – ./

next-fortress-aiji42.vercel.app
next-fortress.vercel.app
next-fortress-git-main-aiji42.vercel.app

Please sign in to comment.