Skip to content

Commit

Permalink
feat: allow flatten provider options
Browse files Browse the repository at this point in the history
ref: #1
  • Loading branch information
pi0 committed Jul 7, 2021
1 parent 1a26c00 commit bd9d3e4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 23 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const app = express()

app.use(createAuthMiddleware({
provider: 'basic',
providerOptions: { username: 'test', password: 'test' }
username: 'test',
password: 'test'
}))

app.use((_req, res) => { res.end(`Welcome ${req.auth.session.user}!`) })
Expand Down Expand Up @@ -42,10 +43,8 @@ app.listen(3000)
```js
app.use(createAuthMiddleware({
provider: 'basic',
providerOptions: {
username: 'test',
password: 'test'
}
username: 'test',
password: 'test'
}))
```

Expand All @@ -62,10 +61,8 @@ app.use(createAuthMiddleware({
app.use(createAuthMiddleware({
provider: 'github',
sessionSecret: '...',
providerOptions: {
clientId: '...',
clientSecret: '...',
}
clientId: '...',
clientSecret: '...',
}))
```

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const noop = () => { }

export function createAuth (opts: CreateAuthOptions) {
const providerCtor = providers[opts.provider || 'basic'] as AuthProvider
const provider = providerCtor(opts.providerOptions)
const provider = providerCtor(opts.providerOptions || opts)

return {
provider
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export interface CreateAuthOptions {
bypass?: boolean
onAuthorize?: (authReses: AuthorizeResult, req: IncomingMessage) => void | Promise<void>
unauthorizedTemplate?: string
[key: string]: any
}
6 changes: 2 additions & 4 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ const app = createApp()
app.useAsync(createAuthMiddleware({
provider: 'basic',
sessionSecret: 'secret',
providerOptions: {
username: 'test',
password: 'test'
}
username: 'test',
password: 'test'
}))

app.useAsync(req => `Welcome ${req.auth.session.user}!`)
Expand Down
8 changes: 3 additions & 5 deletions test/bypass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ const app = createApp()
app.useAsync(createAuthMiddleware({
bypass: true,
provider: 'basic',
providerOptions: {
username: 'test',
password: 'test'
}
username: 'test',
password: 'test'
}))

app.useAsync(req => `Welcome ${req.auth}!`)
app.useAsync(req => `Welcome ${req.auth.session ? 'user' : 'guest'}!`)

listen(app)
6 changes: 2 additions & 4 deletions test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ const app = createApp()
app.useAsync(createAuthMiddleware({
provider: 'github',
sessionSecret: 'secret',
providerOptions: {
clientId: 'e41eb9a028ccaae6358a',
clientSecret: '16a88f9887fa7b24bc7290d8986b2c204f7878a9'
}
clientId: 'e41eb9a028ccaae6358a',
clientSecret: '16a88f9887fa7b24bc7290d8986b2c204f7878a9'
}))

app.useAsync(req => `Welcome ${req.auth.session.user}!`)
Expand Down

0 comments on commit bd9d3e4

Please sign in to comment.