mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-03-13 14:56:54 +08:00
Merge branch 'add-logout-callback-url' of https://github.com/lowdefy/lowdefy into add-logout-callback-url
This commit is contained in:
commit
dda2382cff
@ -0,0 +1,23 @@
|
||||
/*
|
||||
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 addUserFieldsToSession(context, { session, token, authConfig }) {
|
||||
Object.keys(authConfig.userFields).forEach((fieldName) => {
|
||||
session.user[fieldName] = token[fieldName];
|
||||
});
|
||||
}
|
||||
|
||||
export default addUserFieldsToSession;
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
import { get } from '@lowdefy/helpers';
|
||||
|
||||
function addUserFieldsToToken(context, { account, authConfig, profile, token }) {
|
||||
// const { debug } = context.logger;
|
||||
const objects = { account, profile };
|
||||
// TODO: Add when debug is fixed.
|
||||
// debug('Adding userFields to user. Available provider data is:');
|
||||
// debug(objects);
|
||||
Object.entries(authConfig.userFields).forEach(([lowdefyFieldName, providerFieldName]) => {
|
||||
const value = get(objects, providerFieldName);
|
||||
// debug(
|
||||
// `Adding provider field "${providerFieldName}" with value ${JSON.stringify(
|
||||
// value
|
||||
// )} as "${lowdefyFieldName}"`
|
||||
// );
|
||||
token[lowdefyFieldName] = value;
|
||||
});
|
||||
}
|
||||
|
||||
export default addUserFieldsToToken;
|
@ -19,17 +19,17 @@ import createRedirectCallback from './createRedirectCallback.js';
|
||||
import createSessionCallback from './createSessionCallback.js';
|
||||
import createSignInCallback from './createSignInCallback.js';
|
||||
|
||||
function createCallbacks({ authConfig, plugins }) {
|
||||
function createCallbacks(context, { authConfig, plugins }) {
|
||||
const callbacks = {
|
||||
session: createSessionCallback({ authConfig, plugins }),
|
||||
session: createSessionCallback(context, { authConfig, plugins }),
|
||||
};
|
||||
const jwt = createJWTCallback({ authConfig, plugins });
|
||||
const jwt = createJWTCallback(context, { authConfig, plugins });
|
||||
if (jwt) callbacks.jwt = jwt;
|
||||
|
||||
const redirect = createRedirectCallback({ authConfig, plugins });
|
||||
const redirect = createRedirectCallback(context, { authConfig, plugins });
|
||||
if (redirect) callbacks.redirect = redirect;
|
||||
|
||||
const signIn = createSignInCallback({ authConfig, plugins });
|
||||
const signIn = createSignInCallback(context, { authConfig, plugins });
|
||||
if (signIn) callbacks.signIn = signIn;
|
||||
|
||||
return callbacks;
|
||||
|
@ -14,9 +14,10 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import addUserFieldsToToken from './addUserFieldsToToken.js';
|
||||
import createCallbackPlugins from './createCallbackPlugins.js';
|
||||
|
||||
function createJWTCallback({ authConfig, plugins }) {
|
||||
function createJWTCallback(context, { authConfig, plugins }) {
|
||||
const jwtCallbackPlugins = createCallbackPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
@ -70,6 +71,9 @@ function createJWTCallback({ authConfig, plugins }) {
|
||||
updated_at,
|
||||
...token,
|
||||
};
|
||||
if (authConfig.userFields) {
|
||||
addUserFieldsToToken(context, { authConfig, account, profile, token });
|
||||
}
|
||||
}
|
||||
|
||||
for (const plugin of jwtCallbackPlugins) {
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createCallbackPlugins from './createCallbackPlugins.js';
|
||||
|
||||
function createRedirectCallback({ authConfig, plugins }) {
|
||||
function createRedirectCallback(context, { authConfig, plugins }) {
|
||||
const redirectCallbackPlugins = createCallbackPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -14,9 +14,10 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import addUserFieldsToSession from './addUserFieldsToSession.js';
|
||||
import createCallbackPlugins from './createCallbackPlugins.js';
|
||||
|
||||
function createSessionCallback({ authConfig, plugins }) {
|
||||
function createSessionCallback(context, { authConfig, plugins }) {
|
||||
const sessionCallbackPlugins = createCallbackPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
@ -70,6 +71,9 @@ function createSessionCallback({ authConfig, plugins }) {
|
||||
updated_at,
|
||||
...session.user,
|
||||
};
|
||||
if (authConfig.userFields) {
|
||||
addUserFieldsToSession(context, { authConfig, session, token });
|
||||
}
|
||||
}
|
||||
|
||||
for (const plugin of sessionCallbackPlugins) {
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createCallbackPlugins from './createCallbackPlugins.js';
|
||||
|
||||
function createSignInCallback({ authConfig, plugins }) {
|
||||
function createSignInCallback(context, { authConfig, plugins }) {
|
||||
const signInCallbackPlugins = createCallbackPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -19,7 +19,7 @@
|
||||
// This depends on providerId, which might cause some issues if users copy an example and change the id.
|
||||
// We need to allow users to configure ids, since they might have more than one of the same type.
|
||||
|
||||
function createProviders({ authConfig, plugins }) {
|
||||
function createProviders(context, { authConfig, plugins }) {
|
||||
return authConfig.providers.map((providerConfig) =>
|
||||
plugins.providers[providerConfig.type]({
|
||||
...providerConfig.properties,
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createEventPlugins from './createEventPlugins.js';
|
||||
|
||||
function createCreateUserEvent({ authConfig, plugins }) {
|
||||
function createCreateUserEvent(context, { authConfig, plugins }) {
|
||||
const createUserPlugins = createEventPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -21,25 +21,25 @@ import createSignInEvent from './createSignInEvent.js';
|
||||
import createSignOutEvent from './createSignOutEvent.js';
|
||||
import createUpdateUserEvent from './createUpdateUserEvent.js';
|
||||
|
||||
function createEvents({ authConfig, plugins }) {
|
||||
function createEvents(context, { authConfig, plugins }) {
|
||||
const events = {};
|
||||
|
||||
const createUser = createCreateUserEvent({ authConfig, plugins });
|
||||
const createUser = createCreateUserEvent(context, { authConfig, plugins });
|
||||
if (createUser) events.createUser = createUser;
|
||||
|
||||
const linkAccount = createLinkAccountEvent({ authConfig, plugins });
|
||||
const linkAccount = createLinkAccountEvent(context, { authConfig, plugins });
|
||||
if (linkAccount) events.linkAccount = linkAccount;
|
||||
|
||||
const session = createSessionEvent({ authConfig, plugins });
|
||||
const session = createSessionEvent(context, { authConfig, plugins });
|
||||
if (session) events.session = session;
|
||||
|
||||
const signIn = createSignInEvent({ authConfig, plugins });
|
||||
const signIn = createSignInEvent(context, { authConfig, plugins });
|
||||
if (signIn) events.signIn = signIn;
|
||||
|
||||
const signOut = createSignOutEvent({ authConfig, plugins });
|
||||
const signOut = createSignOutEvent(context, { authConfig, plugins });
|
||||
if (signOut) events.signOut = signOut;
|
||||
|
||||
const updateUser = createUpdateUserEvent({ authConfig, plugins });
|
||||
const updateUser = createUpdateUserEvent(context, { authConfig, plugins });
|
||||
if (updateUser) events.updateUser = updateUser;
|
||||
|
||||
return events;
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createEventPlugins from './createEventPlugins.js';
|
||||
|
||||
function createLinkAccountEvent({ authConfig, plugins }) {
|
||||
function createLinkAccountEvent(context, { authConfig, plugins }) {
|
||||
const linkAccountPlugins = createEventPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createEventPlugins from './createEventPlugins.js';
|
||||
|
||||
function createSessionEvent({ authConfig, plugins }) {
|
||||
function createSessionEvent(context, { authConfig, plugins }) {
|
||||
const sessionPlugins = createEventPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createEventPlugins from './createEventPlugins.js';
|
||||
|
||||
function createSignInEvent({ authConfig, plugins }) {
|
||||
function createSignInEvent(context, { authConfig, plugins }) {
|
||||
const signInPlugins = createEventPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createEventPlugins from './createEventPlugins.js';
|
||||
|
||||
function createSignOutEvent({ authConfig, plugins }) {
|
||||
function createSignOutEvent(context, { authConfig, plugins }) {
|
||||
const signInPlugins = createEventPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import createEventPlugins from './createEventPlugins.js';
|
||||
|
||||
function createUpdateUserEvent({ authConfig, plugins }) {
|
||||
function createUpdateUserEvent(context, { authConfig, plugins }) {
|
||||
const updateUserPlugins = createEventPlugins({
|
||||
authConfig,
|
||||
plugins,
|
||||
|
@ -25,10 +25,11 @@ import createProviders from './createProviders.js';
|
||||
const nextAuthConfig = {};
|
||||
let initialized = false;
|
||||
|
||||
function getNextAuthConfig({ authJson, plugins }) {
|
||||
function getNextAuthConfig(context, { authJson, plugins }) {
|
||||
if (initialized) return nextAuthConfig;
|
||||
const secrets = getSecretsFromEnv();
|
||||
|
||||
// TODO: Add logger
|
||||
const operatorsParser = new NodeParser({
|
||||
operators: { _secret },
|
||||
payload: {},
|
||||
@ -45,9 +46,9 @@ function getNextAuthConfig({ authJson, plugins }) {
|
||||
throw new Error(operatorErrors[0]);
|
||||
}
|
||||
|
||||
nextAuthConfig.callbacks = createCallbacks({ authConfig, plugins });
|
||||
nextAuthConfig.events = createEvents({ authConfig, plugins });
|
||||
nextAuthConfig.providers = createProviders({ authConfig, plugins });
|
||||
nextAuthConfig.callbacks = createCallbacks(context, { authConfig, plugins });
|
||||
nextAuthConfig.events = createEvents(context, { authConfig, plugins });
|
||||
nextAuthConfig.providers = createProviders(context, { authConfig, plugins });
|
||||
|
||||
nextAuthConfig.session = authConfig.session;
|
||||
nextAuthConfig.theme = authConfig.theme;
|
||||
|
@ -225,6 +225,9 @@ export default {
|
||||
theme: {
|
||||
type: 'object',
|
||||
},
|
||||
userFields: {
|
||||
type: 'object',
|
||||
},
|
||||
},
|
||||
},
|
||||
block: {
|
||||
|
@ -22,4 +22,20 @@ import callbacks from '../../../build/plugins/auth/callbacks.js';
|
||||
import events from '../../../build/plugins/auth/events.js';
|
||||
import providers from '../../../build/plugins/auth/providers.js';
|
||||
|
||||
export default NextAuth(getNextAuthConfig({ authJson, plugins: { callbacks, events, providers } }));
|
||||
// TODO: make createApiContext synchronous
|
||||
export default async function auth(req, res) {
|
||||
if (authJson.configured === true) {
|
||||
return await NextAuth(
|
||||
req,
|
||||
res,
|
||||
getNextAuthConfig(
|
||||
{ logger: console },
|
||||
{ authJson, plugins: { callbacks, events, providers } }
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return res.status(404).json({
|
||||
message: 'Auth not configured',
|
||||
});
|
||||
}
|
||||
|
@ -27,7 +27,10 @@ export default async function auth(req, res) {
|
||||
return await NextAuth(
|
||||
req,
|
||||
res,
|
||||
getNextAuthConfig({ authJson, plugins: { callbacks, events, providers } })
|
||||
getNextAuthConfig(
|
||||
{ logger: console },
|
||||
{ authJson, plugins: { callbacks, events, providers } }
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user