Skip to content

Commit

Permalink
Merge branch 'develop' into react-18
Browse files Browse the repository at this point in the history
  • Loading branch information
Gervwyk committed May 30, 2022
2 parents 331284e + c581fb0 commit c1c1ade
Show file tree
Hide file tree
Showing 43 changed files with 446 additions and 82 deletions.
16 changes: 1 addition & 15 deletions packages/api/src/context/errors.js
Expand Up @@ -16,13 +16,6 @@

/* eslint-disable max-classes-per-file */

class AuthenticationError extends Error {
constructor(message) {
super(message);
this.name = 'AuthenticationError';
}
}

class ConfigurationError extends Error {
constructor(message) {
super(message);
Expand All @@ -44,11 +37,4 @@ class ServerError extends Error {
}
}

class TokenExpiredError extends Error {
constructor(message) {
super(message);
this.name = 'TokenExpiredError';
}
}

export { AuthenticationError, ConfigurationError, RequestError, ServerError, TokenExpiredError };
export { ConfigurationError, RequestError, ServerError };
12 changes: 2 additions & 10 deletions packages/api/src/index.js
Expand Up @@ -17,20 +17,13 @@
import callRequest from './routes/request/callRequest.js';
import createApiContext from './context/createApiContext.js';
import getHomeAndMenus from './routes/rootConfig/getHomeAndMenus.js';
import getNextAuthConfig from './auth/getNextAuthConfig.js';
import getNextAuthConfig from './routes/auth/getNextAuthConfig.js';
import getPageConfig from './routes/page/getPageConfig.js';
import getRootConfig from './routes/rootConfig/getRootConfig.js';

import {
AuthenticationError,
ConfigurationError,
RequestError,
ServerError,
TokenExpiredError,
} from './context/errors.js';
import { ConfigurationError, RequestError, ServerError } from './context/errors.js';

export {
AuthenticationError,
callRequest,
ConfigurationError,
createApiContext,
Expand All @@ -40,5 +33,4 @@ export {
getRootConfig,
RequestError,
ServerError,
TokenExpiredError,
};
Expand Up @@ -23,9 +23,55 @@ function createJWTCallback({ authConfig, plugins }) {
type: 'jwt',
});

if (jwtCallbackPlugins.length === 0) return undefined;

async function jwtCallback({ token, user, account, profile, isNewUser }) {
if (profile) {
const {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile: profile_claim,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
} = profile;
token = {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile: profile_claim,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
...token,
};
}

for (const plugin of jwtCallbackPlugins) {
token = await plugin.fn({
properties: plugin.properties ?? {},
Expand Down
Expand Up @@ -24,8 +24,53 @@ function createSessionCallback({ authConfig, plugins }) {
});

async function sessionCallback({ session, token, user }) {
// console.log({ session, token, user });
if (token) {
session.user.sub = token.sub;
const {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
} = token;
session.user = {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
...session.user,
};
}

for (const plugin of sessionCallbackPlugins) {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions packages/build/src/build/buildAuth/buildAuth.js
Expand Up @@ -16,12 +16,16 @@
limitations under the License.
*/

import { type } from '@lowdefy/helpers';
import buildAuthPlugins from './buildAuthPlugins.js';
import buildPageAuth from './buildPageAuth.js';
import validateAuthConfig from './validateAuthConfig.js';

function buildAuth({ components, context }) {
const configured = !type.isNone(components.auth);

validateAuthConfig({ components });
components.auth.configured = configured;
buildPageAuth({ components });
buildAuthPlugins({ components, context });

Expand Down
9 changes: 9 additions & 0 deletions packages/build/src/build/buildAuth/buildAuth.test.js
Expand Up @@ -31,6 +31,7 @@ test('buildAuth default', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: false,
events: [],
pages: {
roles: {},
Expand All @@ -53,6 +54,7 @@ test('buildAuth no pages', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: false,
events: [],
pages: {
roles: {},
Expand Down Expand Up @@ -82,6 +84,7 @@ test('buildAuth all protected, some public', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: true,
events: [],
pages: {
public: ['a', 'b'],
Expand Down Expand Up @@ -117,6 +120,7 @@ test('buildAuth all public, some protected', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: true,
events: [],
pages: {
protected: ['a', 'b'],
Expand Down Expand Up @@ -152,6 +156,7 @@ test('buildAuth all public', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: true,
events: [],
pages: {
public: true,
Expand Down Expand Up @@ -188,6 +193,7 @@ test('buildAuth all protected', async () => {
auth: {
callbacks: [],
events: [],
configured: true,
pages: {
protected: true,
roles: {},
Expand Down Expand Up @@ -224,6 +230,7 @@ test('buildAuth roles', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: true,
events: [],
pages: {
roles: {
Expand Down Expand Up @@ -276,6 +283,7 @@ test('buildAuth roles and protected pages array', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: true,
events: [],
pages: {
roles: {
Expand Down Expand Up @@ -307,6 +315,7 @@ test('buildAuth roles and protected true', async () => {
expect(res).toEqual({
auth: {
callbacks: [],
configured: true,
events: [],
pages: {
roles: {
Expand Down
2 changes: 0 additions & 2 deletions packages/client/src/Client.js
Expand Up @@ -30,7 +30,6 @@ const Client = ({
config,
resetContext = { reset: false, setReset: () => undefined },
router,
session,
stage,
types,
window,
Expand All @@ -40,7 +39,6 @@ const Client = ({
Components,
config,
router,
session,
stage,
types,
window,
Expand Down
8 changes: 5 additions & 3 deletions packages/client/src/auth/createAuthMethods.js
Expand Up @@ -34,12 +34,14 @@ function getCallbackUrl({ lowdefy, callbackUrl = {} }) {
return undefined;
}

function createAuthMethods(lowdefy, auth) {
function createAuthMethods({ lowdefy, auth }) {
// login and logout are Lowdefy function that handle action params
// signIn and signOut are the next-auth methods
function login({ providerId, callbackUrl, authUrl = {} } = {}) {
// TODO: if only one provider exists, pass provider here
// to link directly to provider
if (type.isNone(providerId) && auth.authConfig.providers.length === 1) {
providerId = auth.authConfig.providers[0].id;
}

auth.signIn(
providerId,
{ callbackUrl: getCallbackUrl({ lowdefy, callbackUrl }) },
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/initLowdefyContext.js
Expand Up @@ -44,7 +44,7 @@ const lowdefy = {
lowdefyGlobal: {},
};

function initLowdefyContext({ auth, Components, config, router, session, stage, types, window }) {
function initLowdefyContext({ auth, Components, config, router, stage, types, window }) {
if (stage === 'dev') {
window.lowdefy = lowdefy;
}
Expand All @@ -54,7 +54,7 @@ function initLowdefyContext({ auth, Components, config, router, session, stage,
lowdefy.menus = config.rootConfig.menus;
lowdefy.pageId = config.pageConfig.pageId;
lowdefy.urlQuery = urlQuery.parse(window.location.search.slice(1));
lowdefy.user = session?.user ?? null;
lowdefy.user = auth?.session?.user ?? null;

lowdefy._internal.window = window;
lowdefy._internal.document = window.document;
Expand All @@ -69,7 +69,7 @@ function initLowdefyContext({ auth, Components, config, router, session, stage,
lowdefy._internal.operators = types.operators;

// TODO: discuss not using object arguments
lowdefy._internal.auth = createAuthMethods(lowdefy, auth);
lowdefy._internal.auth = createAuthMethods({ lowdefy, auth });

return lowdefy;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/server-dev/lib/App.js
Expand Up @@ -31,7 +31,7 @@ import blocks from '../build/plugins/blocks.js';
import icons from '../build/plugins/icons.js';
import operators from '../build/plugins/operators/client.js';

const App = () => {
const App = ({ auth }) => {
const router = useRouter();
const { data: rootConfig } = useRootConfig(router.basePath);

Expand All @@ -44,6 +44,7 @@ const App = () => {
<Reload basePath={router.basePath}>
{(resetContext) => (
<Page
auth={auth}
Components={{ Head, Link }}
config={{
rootConfig,
Expand Down
11 changes: 2 additions & 9 deletions packages/server-dev/lib/Page.js
Expand Up @@ -15,20 +15,14 @@
*/

import React from 'react';
import { signIn, signOut, useSession } from 'next-auth/react';

import Client from '@lowdefy/client';

import RestartingPage from './RestartingPage.js';
import usePageConfig from './utils/usePageConfig.js';

const Page = ({ Components, config, pageId, resetContext, router, types }) => {
const { data: session, status } = useSession();
const Page = ({ auth, Components, config, pageId, resetContext, router, types }) => {
const { data: pageConfig } = usePageConfig(pageId, router.basePath);

if (status === 'loading') {
return '';
}
if (!pageConfig) {
router.replace(`/404`);
return '';
Expand All @@ -38,15 +32,14 @@ const Page = ({ Components, config, pageId, resetContext, router, types }) => {
}
return (
<Client
auth={{ signIn, signOut }}
auth={auth}
Components={Components}
config={{
...config,
pageConfig,
}}
resetContext={resetContext}
router={router}
session={session}
stage="dev"
types={types}
window={window}
Expand Down

0 comments on commit c1c1ade

Please sign in to comment.