Skip to content

Commit f094f4b

Browse files
fix: TypeError when omitting credentials.auth (#256)
* fix: TypeError when omitting credentials.auth #249 #246 * fix: proposed correction for expectAssignable<ModuleOptions<string>>(credentials) * style: simplify mock expectAssignable<ModuleOptions<string>>(credentials) * fix: revert version Signed-off-by: RodrigoDornelles <[email protected]> * feat: assertion options.discovery.issuer or credentials.auth is set * tests: not providing options.discovery.issuer and credentials.auth --------- Signed-off-by: RodrigoDornelles <[email protected]>
1 parent 1715f76 commit f094f4b

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ function fastifyOauth2 (fastify, options, next) {
9595
if (options.discovery && options.credentials.auth) {
9696
return next(new Error('when options.discovery.issuer is configured, credentials.auth should not be used'))
9797
}
98+
if (!options.discovery && !options.credentials.auth) {
99+
return next(new Error('options.discovery.issuer or credentials.auth have to be given'))
100+
}
98101
if (!fastify.hasReplyDecorator('cookie')) {
99102
fastify.register(require('@fastify/cookie'))
100103
}

test/index.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,26 @@ t.test('credentials.auth should not be provided when discovery is used', t => {
18781878
})
18791879
})
18801880

1881+
t.test('not providing options.discovery.issuer and credentials.auth', t => {
1882+
t.plan(1)
1883+
1884+
const fastify = createFastify({ logger: { level: 'silent' } })
1885+
1886+
fastify.register(fastifyOauth2, {
1887+
name: 'the-name',
1888+
credentials: {
1889+
client: {
1890+
id: 'my-client-id',
1891+
secret: 'my-secret'
1892+
}
1893+
},
1894+
callbackUri: '/callback'
1895+
})
1896+
.ready(err => {
1897+
t.strictSame(err.message, 'options.discovery.issuer or credentials.auth have to be given')
1898+
})
1899+
})
1900+
18811901
t.test('options.schema', t => {
18821902
const fastify = createFastify({ logger: { level: 'silent' }, exposeHeadRoutes: false })
18831903

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ declare namespace fastifyOauth2 {
107107
/** Parameter name used to send the client id. Default to client_id. */
108108
idParamName?: string | undefined;
109109
};
110-
auth: ProviderConfiguration;
110+
auth?: ProviderConfiguration;
111111
/**
112112
* Used to set global options to the internal http library (wreck).
113113
* All options except baseUrl are allowed

types/index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ expectType<string[]>(tags);
153153
expectType<Credentials>(credentials);
154154

155155
// Ensure duplicayed simple-oauth2 are compatible with simple-oauth2
156-
expectAssignable<ModuleOptions<string>>(credentials);
156+
expectAssignable<ModuleOptions<string>>({auth: {tokenHost: ''}, ...credentials });
157157
expectAssignable<ModuleOptions["auth"]>(auth);
158158
// Ensure published types of simple-oauth2 are accepted
159159
expectAssignable<Credentials>(simpleOauth2Options);

0 commit comments

Comments
 (0)