Skip to content

Commit

Permalink
fix(react-formio): Fix user authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Feb 21, 2021
1 parent e47005b commit 4974ba8
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 108 deletions.
1 change: 1 addition & 0 deletions packages/react-formio/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/camelcase": 0,
"import/export": 0
}
}
112 changes: 4 additions & 108 deletions packages/react-formio/src/stores/auth/auth.actions.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,12 @@
import { createAction } from "@tsed/redux-utils";
import { Formio } from "formiojs";
import noop from "lodash/noop";

const name = "auth";
import { FormSchema, RoleSchema, Submission } from "../../interfaces";

export const requestUser = createAction();
export const receiveUser = createAction<{ user: any }>();
export const receiveUser = createAction<{ user: Submission }>();
export const failUser = createAction<{ error: Error }>();
export const logoutUser = createAction();
export const submissionAccessUser = createAction<{ submissionAccess: any }>();
export const formAccessUser = createAction<{ formAccess: any }>();
export const projectAccessUser = createAction<{ projectAccess: any }>();
export const userRoles = createAction<{ roles: any[] }>();
export const userForms = createAction<{ forms: any[] }>();

function transformSubmissionAccess(forms: any[]) {
return Object.values(forms).reduce(
(result, form) => ({
...result,
[form.name]: form.submissionAccess.reduce(
(formSubmissionAccess: any, access: any) => ({
...formSubmissionAccess,
[access.type]: access.roles
}),
{}
)
}),
{}
);
}

function transformFormAccess(forms: any[]) {
return Object.values(forms).reduce(
(result, form) => ({
...result,
[form.name]: form.access.reduce(
(formAccess: any, access: any) => ({
...formAccess,
[access.type]: access.roles
}),
{}
)
}),
{}
);
}

function transformProjectAccess(projectAccess: any) {
return projectAccess.reduce(
(result: any, access: any) => ({
...result,
[access.type]: access.roles
}),
{}
);
}

async function getAccess(dispatch: any) {
const projectUrl = Formio.getProjectUrl();

try {
const result = await Formio.makeStaticRequest(`${projectUrl}/access`);

const submissionAccess = transformSubmissionAccess(result.forms);
const formAccess = transformFormAccess(result.forms);

dispatch(submissionAccessUser(name, { submissionAccess }));
dispatch(formAccessUser(name, { formAccess }));
dispatch(userRoles(name, { roles: result.roles }));
dispatch(userForms(name, { forms: result.forms }));
} catch (err) {}
}

async function getProjectAccess(dispatch: any) {
const projectUrl = Formio.getProjectUrl();

try {
const project = await Formio.makeStaticRequest(projectUrl);
const projectAccess = transformProjectAccess(project.access);

dispatch(projectAccessUser(name, projectAccess));
} catch (er) {}
}

export const initAuth = (done = noop) => async (dispatch: any) => {
dispatch(requestUser(name));

try {
const [user] = await Promise.all([
Formio.currentUser(),
getAccess(dispatch),
getProjectAccess(dispatch)
]);

if (user) {
dispatch(receiveUser(name, user));
} else {
dispatch(logoutUser(name));
}
} catch (error) {
dispatch(failUser(name, { error }));
}

done();
};

export const setUser = (user: any) => (dispatch: any) => {
Formio.setUser(user);
dispatch(receiveUser(name, { user }));
};

export const logout = () => (dispatch: any) => {
Formio.logout();
dispatch(logoutUser(name));
};
export const userRoles = createAction<{ roles: Record<string, RoleSchema> }>();
export const userForms = createAction<{ forms: Record<string, FormSchema> }>();
1 change: 1 addition & 0 deletions packages/react-formio/src/stores/auth/auth.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const USER_AUTH = "auth";
Loading

0 comments on commit 4974ba8

Please sign in to comment.