Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move auth to ms2 #196

Merged
merged 23 commits into from
Jan 2, 2024
Merged

Move auth to ms2 #196

merged 23 commits into from
Jan 2, 2024

Conversation

FelipeTrost
Copy link
Contributor

Details

  • Replaced Redis for lru-cache
  • Changed small ability mechanics for conditions: use null instead of undefined
  • Changed legacy data store to be used in rsc
  • Implemented server actions for roles and role-mappings
  • Changed roles pages to rsc

TODO: users site still needs the old ms, since nextauth doesn't implement auth0's api

Comment on lines 15 to 20
} catch (error) {
errors.push({ roleId, error: error as Error });
}
}

return errors;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change this to an early return as soon as you hit the first error.
The reason is that later with a proper database, these batch operations are usually atomic. So any error would fail the whole operation and not complete some and not others. So to not have to refactor the client side handlers, we should just return one error already.

For reference, you can look at the return userError() lines in lib/data/processes.tsx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in one of my last commits

Comment on lines +22 to +27
} catch (error) {
errors.push({ roleId, error: error as Error });
}
}

return errors;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in one of my last commits

Comment on lines 216 to 219
console.log(
'leftkeys',
Object.keys(roleMetaObjects).map((r) => roleMetaObjects[r].name),
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it in one of my last commits

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general comment on throwing errors in server actions:

In production mode, React doesn't emit errors or rejected promises to the client. Instead a hash is sent representing the error. This hash can be used to associate multiple of the same errors together and associate the error with server logs. React replaces the error message with its own generic one.

https://nextjs.org/blog/security-nextjs-server-components-actions#error-handling

That is why for "expected" errors (like permissions, wrong id, etc.) I return a normal object with userError(). That way our UI can show the message and the user doesn't lose the state of the forms as would be the case by showing the error.tsx.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in one of my last commits

Comment on lines +34 to +44
const { id: roleId, name } = addRole(role, new Ability(adminRules()));

if (process.env.NODE_ENV === 'development') {
const roleMappings = developmentRoleMappingsMigrations
.filter((mapping) => mapping.roleName === name)
.map((mapping) => ({
roleId,
userId: mapping.userId,
}));

addRoleMappings(roleMappings, new Ability(adminRules()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it unproblematic that these are async functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to remove async form addRole. I fixed it in one of my last commits

);

export const dynamic = 'force-dynamic';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary as long as getCurrentUser() is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in one of my last commits

setSelectedRows([]);
router.refresh();
} catch (e) {
messageApi.error((e as Error).message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment. This will always be a generic message in production.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in one of my last commits

@OhKai OhKai merged commit 19e14b5 into main Jan 2, 2024
11 checks passed
@OhKai OhKai deleted the move-auth-to-ms2 branch January 2, 2024 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants