Skip to content

Commit

Permalink
feat: Add support for Next-Auth adapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Jul 13, 2022
1 parent 5ae6e2b commit 337dbf4
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 35 deletions.
27 changes: 27 additions & 0 deletions packages/api/src/routes/auth/createAdapter.js
@@ -0,0 +1,27 @@
/*
Copyright 2020-2022 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

function createAdapter(context, { authConfig, plugins }) {
const adapterConfig = authConfig.adapter;

if (!adapterConfig) {
return undefined;
}

return plugins.adapters[adapterConfig.type]({ properties: adapterConfig.properties });
}

export default createAdapter;
2 changes: 2 additions & 0 deletions packages/api/src/routes/auth/getNextAuthConfig.js
Expand Up @@ -18,6 +18,7 @@ import { NodeParser } from '@lowdefy/operators';
import { getSecretsFromEnv } from '@lowdefy/node-utils';
import { _secret } from '@lowdefy/operators-js/operators/server';

import createAdapter from './createAdapter.js';
import createCallbacks from './callbacks/createCallbacks.js';
import createEvents from './events/createEvents.js';
import createProviders from './createProviders.js';
Expand Down Expand Up @@ -46,6 +47,7 @@ function getNextAuthConfig(context, { authJson, plugins }) {
throw new Error(operatorErrors[0]);
}

nextAuthConfig.adapter = createAdapter(context, { authConfig, plugins });
nextAuthConfig.callbacks = createCallbacks(context, { authConfig, plugins });
nextAuthConfig.events = createEvents(context, { authConfig, plugins });
nextAuthConfig.providers = createProviders(context, { authConfig, plugins });
Expand Down
27 changes: 22 additions & 5 deletions packages/build/src/build/buildAuth/buildAuthPlugins.js
Expand Up @@ -38,14 +38,31 @@ function buildAuthPlugin({ counter, pluginConfig, typeClass }) {
}
}

function buildAdapter({ components, context }) {
const { adapter } = components.auth;
if (type.isNone(adapter)) {
return;
}
if (type.isUndefined(adapter.id)) {
throw new Error(`Auth adapter id missing.`);
}
if (!type.isString(adapter.id)) {
throw new Error(`Auth adapter id is not a string. Received ${JSON.stringify(adapter.id)}.`);
}
if (!type.isString(adapter.type)) {
throw new Error(
`Auth adapter type is not a string at adapter "${adapter.id}". Received ${JSON.stringify(
adapter.type
)}.`
);
}
context.typeCounters.auth.adapters.increment(adapter.type);
}

function buildAuthPlugins({ components, context }) {
const counters = context.typeCounters.auth;
const authConfig = components.auth;
buildAuthPlugin({
counter: counters.adapters,
pluginConfig: authConfig.adapters,
typeClass: 'adapter',
});
buildAdapter({ components, context });
buildAuthPlugin({
counter: counters.callbacks,
pluginConfig: authConfig.callbacks,
Expand Down
3 changes: 0 additions & 3 deletions packages/build/src/build/buildAuth/validateAuthConfig.js
Expand Up @@ -33,9 +33,6 @@ async function validateAuthConfig({ components }) {
if (type.isNone(components.auth.pages.roles)) {
components.auth.pages.roles = {};
}
if (type.isNone(components.auth.adapters)) {
components.auth.adapters = [];
}
if (type.isNone(components.auth.callbacks)) {
components.auth.callbacks = [];
}
Expand Down
1 change: 1 addition & 0 deletions packages/build/src/build/buildImports/buildImportsDev.js
Expand Up @@ -55,6 +55,7 @@ function buildImportsDev({ components, context }) {
return {
actions: buildImportClassDev({ pluginPackages, map: context.typesMap.actions }),
auth: {
adapters: buildImportClassDev({ pluginPackages, map: context.typesMap.auth.adapters }),
callbacks: buildImportClassDev({ pluginPackages, map: context.typesMap.auth.callbacks }),
events: buildImportClassDev({ pluginPackages, map: context.typesMap.auth.events }),
providers: buildImportClassDev({ pluginPackages, map: context.typesMap.auth.providers }),
Expand Down
47 changes: 22 additions & 25 deletions packages/build/src/lowdefySchema.js
Expand Up @@ -80,35 +80,32 @@ export default {
},
},
},
adapters: {
type: 'array',
items: {
type: 'object',
required: ['id', 'type'],
properties: {
id: {
type: 'string',
errorMessage: {
type: 'Auth adapter "id" should be a string.',
},
},
type: {
type: 'string',
errorMessage: {
type: 'Auth adapter "type" should be a string.',
},
},
properties: {
type: 'object',
adapter: {
type: 'object',
required: ['id', 'type'],
properties: {
id: {
type: 'string',
errorMessage: {
type: 'Auth adapter "id" should be a string.',
},
},
errorMessage: {
type: 'Auth adapter should be an object.',
required: {
id: 'Auth adapter should have required property "id".',
type: 'Auth adapter should have required property "type".',
type: {
type: 'string',
errorMessage: {
type: 'Auth adapter "type" should be a string.',
},
},
properties: {
type: 'object',
},
},
errorMessage: {
type: 'Auth adapter should be an object.',
required: {
id: 'Auth adapter should have required property "id".',
type: 'Auth adapter should have required property "type".',
},
},
},
callbacks: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugins/plugin-next-auth/package.json
Expand Up @@ -28,7 +28,7 @@
"type": "module",
"exports": {
".": "./dist/index.js",
"./auth/*": "./dist/auth/*",
"./auth/providers": "./dist/auth/providers.js",
"./types": "./dist/types.js"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/server-dev/pages/api/auth/[...nextauth].js
Expand Up @@ -31,7 +31,7 @@ export default async function auth(req, res) {
res,
getNextAuthConfig(
{ logger: console },
{ authJson, plugins: { callbacks, events, providers } }
{ authJson, plugins: { adapters, callbacks, events, providers } }
)
);
}
Expand Down

0 comments on commit 337dbf4

Please sign in to comment.