@driveflux/auth 4.0.60 → 4.0.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/AuthProvider.js +76 -59
  2. package/dist/authorization/constants.js +45 -24
  3. package/dist/authorization/define.js +57 -28
  4. package/dist/authorization/fields/index.js +4 -7
  5. package/dist/authorization/helpers.js +10 -8
  6. package/dist/authorization/index.js +6 -6
  7. package/dist/authorization/permissions-list.js +5 -7
  8. package/dist/authorization/quick.js +1 -1
  9. package/dist/authorization/roles/admin/business-development-executive.js +20 -7
  10. package/dist/authorization/roles/admin/ceo.js +4 -2
  11. package/dist/authorization/roles/admin/common.js +4 -2
  12. package/dist/authorization/roles/admin/concierge.js +35 -10
  13. package/dist/authorization/roles/admin/customer-success-executive.js +40 -10
  14. package/dist/authorization/roles/admin/data-analyst.js +7 -4
  15. package/dist/authorization/roles/admin/designer.js +7 -4
  16. package/dist/authorization/roles/admin/engineer.js +7 -4
  17. package/dist/authorization/roles/admin/finance-executive.js +11 -4
  18. package/dist/authorization/roles/admin/head-of-business-development.js +14 -4
  19. package/dist/authorization/roles/admin/head-of-data-analytics.js +14 -4
  20. package/dist/authorization/roles/admin/head-of-engineering.js +17 -6
  21. package/dist/authorization/roles/admin/head-of-finance.js +8 -3
  22. package/dist/authorization/roles/admin/head-of-human-resources.js +13 -5
  23. package/dist/authorization/roles/admin/head-of-marketing.js +17 -5
  24. package/dist/authorization/roles/admin/head-of-operations.js +8 -3
  25. package/dist/authorization/roles/admin/head-of-product.js +17 -6
  26. package/dist/authorization/roles/admin/head-of-sales.js +17 -5
  27. package/dist/authorization/roles/admin/human-resources-executive.js +12 -5
  28. package/dist/authorization/roles/admin/marketing-executive.js +7 -4
  29. package/dist/authorization/roles/admin/product-manager.js +7 -4
  30. package/dist/authorization/roles/admin/sales-executive.js +24 -8
  31. package/dist/authorization/roles/consumer/business-admin.js +19 -6
  32. package/dist/authorization/roles/consumer/business-user.js +18 -6
  33. package/dist/authorization/roles/consumer/member.js +16 -6
  34. package/dist/authorization/types.js +1 -1
  35. package/dist/authorization/update-user-permissions.js +22 -15
  36. package/dist/authorization/utils.js +26 -11
  37. package/dist/context.js +8 -9
  38. package/dist/default.js +1 -1
  39. package/dist/server/authenticate-user.js +11 -7
  40. package/dist/server/cors.js +23 -12
  41. package/dist/server/credentials-provider.js +2 -2
  42. package/dist/server/next-auth.js +104 -109
  43. package/dist/server/prisma-adapter.js +88 -52
  44. package/dist/server/verfiy-token.js +39 -24
  45. package/dist/translations.js +4 -4
  46. package/dist/use-auth.js +1 -1
  47. package/dist/use-session.js +1 -1
  48. package/package.json +3 -3
@@ -1,12 +1,25 @@
1
- export const defineRoleAbilitiesBusinessAdmin = async (can, user) => {
1
+ export const defineRoleAbilitiesBusinessAdmin = async (can, user)=>{
2
2
  if (!user.businessId) {
3
3
  return;
4
4
  }
5
- can(['read', 'update'], 'User', { businessId: user.businessId });
6
- can('read', 'Subscription', { businessId: user.businessId });
7
- can('update', 'Business', ['phoneNumber', 'mobileNumber', 'addresses'], {
8
- id: user.businessId,
5
+ can([
6
+ 'read',
7
+ 'update'
8
+ ], 'User', {
9
+ businessId: user.businessId
10
+ });
11
+ can('read', 'Subscription', {
12
+ businessId: user.businessId
13
+ });
14
+ can('update', 'Business', [
15
+ 'phoneNumber',
16
+ 'mobileNumber',
17
+ 'addresses'
18
+ ], {
19
+ id: user.businessId
20
+ });
21
+ can('read', 'Business', {
22
+ id: user.businessId
9
23
  });
10
- can('read', 'Business', { id: user.businessId });
11
24
  can('create', 'Mandate');
12
25
  };
@@ -1,12 +1,24 @@
1
- export const defineRoleAbilitiesBusinessUser = async (can, user) => {
1
+ export const defineRoleAbilitiesBusinessUser = async (can, user)=>{
2
2
  if (!user.businessId) {
3
3
  return;
4
4
  }
5
- can(['read'], 'User', { businessId: user.businessId });
6
- can('read', 'Subscription', { businessId: user.businessId });
7
- can('update', 'Business', ['phoneNumber', 'mobileNumber', 'addresses'], {
8
- id: user.businessId,
5
+ can([
6
+ 'read'
7
+ ], 'User', {
8
+ businessId: user.businessId
9
+ });
10
+ can('read', 'Subscription', {
11
+ businessId: user.businessId
12
+ });
13
+ can('update', 'Business', [
14
+ 'phoneNumber',
15
+ 'mobileNumber',
16
+ 'addresses'
17
+ ], {
18
+ id: user.businessId
19
+ });
20
+ can('read', 'Business', {
21
+ id: user.businessId
9
22
  });
10
- can('read', 'Business', { id: user.businessId });
11
23
  can('create', 'Mandate');
12
24
  };
@@ -1,4 +1,4 @@
1
- export const defineRoleAbilitiesMember = async (can, user) => {
1
+ export const defineRoleAbilitiesMember = async (can, user)=>{
2
2
  can('read', 'Cycle', [
3
3
  'id',
4
4
  'startDate',
@@ -6,13 +6,23 @@ export const defineRoleAbilitiesMember = async (can, user) => {
6
6
  'utilization.pricePerKm',
7
7
  'utilization.paid',
8
8
  'utilization.allowedMileage',
9
- 'utilization.mileage',
10
- ], { subscription: { userId: user.id } });
11
- can('read', 'Vehicle', { status: 'listed' });
9
+ 'utilization.mileage'
10
+ ], {
11
+ subscription: {
12
+ userId: user.id
13
+ }
14
+ });
15
+ can('read', 'Vehicle', {
16
+ status: 'listed'
17
+ });
12
18
  can('create', 'Business');
13
19
  if (user.businessId) {
14
- can('update', 'Business', { id: user.businessId });
20
+ can('update', 'Business', {
21
+ id: user.businessId
22
+ });
15
23
  }
16
- can('read', 'Subscription', { userId: user.id });
24
+ can('read', 'Subscription', {
25
+ userId: user.id
26
+ });
17
27
  can('read', 'Coupon');
18
28
  };
@@ -1 +1 @@
1
- export {};
1
+ export { };
@@ -2,32 +2,39 @@ import { prisma } from '@driveflux/db';
2
2
  import { makeProblem, PROBLEM_NOT_FOUND } from '@driveflux/problem';
3
3
  import { Err, Ok } from '@driveflux/result';
4
4
  import { defineAbilityFor } from './define.js';
5
- export const updateUserPermissions = async (userId, newGroups) => {
6
- const user = typeof userId === 'string'
7
- ? await prisma.user.findUnique({
8
- where: {
9
- id: userId,
10
- },
11
- })
12
- : userId;
5
+ export const updateUserPermissions = async (userId, newGroups)=>{
6
+ const user = typeof userId === 'string' ? await prisma.user.findUnique({
7
+ where: {
8
+ id: userId
9
+ }
10
+ }) : userId;
13
11
  if (!user) {
14
12
  return new Err(makeProblem(PROBLEM_NOT_FOUND, 'User not found when trying to update the permissions'));
15
13
  }
16
- const groups = newGroups ? [...newGroups] : [...user.groups];
14
+ const groups = newGroups ? [
15
+ ...newGroups
16
+ ] : [
17
+ ...user.groups
18
+ ];
17
19
  const ability = await defineAbilityFor({
18
20
  ...user,
19
- groups,
21
+ groups
20
22
  });
21
23
  await prisma.user.update({
22
24
  where: {
23
- id: user.id,
25
+ id: user.id
24
26
  },
25
27
  data: {
26
- groups: [...groups],
27
- permissions: ability.rules,
28
- },
28
+ groups: [
29
+ ...groups
30
+ ],
31
+ permissions: ability.rules
32
+ }
29
33
  });
30
34
  user.groups = groups;
31
35
  user.permissions = ability.rules;
32
- return new Ok({ user, ability });
36
+ return new Ok({
37
+ user,
38
+ ability
39
+ });
33
40
  };
@@ -1,25 +1,37 @@
1
1
  import { subject } from '@casl/ability';
2
2
  import { pascalCase } from 'change-case';
3
- export const detectSubjectType = (subject) => {
3
+ export const detectSubjectType = (subject)=>{
4
4
  if (!('object' in subject)) {
5
5
  throw new Error(`Trying to get an object from model ${subject} that is not a real model`);
6
6
  }
7
7
  return pascalCase(subject.object);
8
8
  };
9
9
  const documents = {
10
- identification: ['visa', 'passport', 'idFront', 'idBack', 'drivingLicense'],
11
- drivingHistory: ['pdrm', 'jpj', 'bgs'],
10
+ identification: [
11
+ 'visa',
12
+ 'passport',
13
+ 'idFront',
14
+ 'idBack',
15
+ 'drivingLicense'
16
+ ],
17
+ drivingHistory: [
18
+ 'pdrm',
19
+ 'jpj',
20
+ 'bgs'
21
+ ],
12
22
  financial: [
13
23
  'experianReport',
14
24
  'bankStatement',
15
25
  'bankStatement2',
16
26
  'bankStatement3',
17
- 'epfStatement',
27
+ 'epfStatement'
18
28
  ],
19
- offerLetter: ['offerLetter'],
29
+ offerLetter: [
30
+ 'offerLetter'
31
+ ]
20
32
  };
21
- export const getDocumentsFields = (documentsGroup) => documents[documentsGroup].map((d) => `documents.${d}`);
22
- export const getPricingFields = () => {
33
+ export const getDocumentsFields = (documentsGroup)=>documents[documentsGroup].map((d)=>`documents.${d}`);
34
+ export const getPricingFields = ()=>{
23
35
  return [
24
36
  'pricing',
25
37
  'basePrice',
@@ -27,14 +39,17 @@ export const getPricingFields = () => {
27
39
  'basePricePlan12',
28
40
  'basePricePlan24',
29
41
  'basePricePlan36',
30
- 'basePricePlan60',
42
+ 'basePricePlan60'
31
43
  ];
32
44
  };
33
45
  // we are creating a helper function to fake an object
34
- export const s = (modelName, key = 'id') => {
46
+ export const s = (modelName, key = 'id')=>{
35
47
  const objectName = getObjectName(modelName);
36
- return subject(modelName, { object: objectName, [key]: 'XXXXXXXXX' });
48
+ return subject(modelName, {
49
+ object: objectName,
50
+ [key]: 'XXXXXXXXX'
51
+ });
37
52
  };
38
- const getObjectName = (modelName) => {
53
+ const getObjectName = (modelName)=>{
39
54
  return modelName[0].toLowerCase() + modelName.slice(1);
40
55
  };
package/dist/context.js CHANGED
@@ -2,16 +2,15 @@ import { createContext } from 'react';
2
2
  const defaultContext = {
3
3
  user: undefined,
4
4
  isLoadingUser: false,
5
- signIn: async () => Promise.resolve(undefined),
6
- refresh: async () => Promise.resolve(undefined),
5
+ signIn: async ()=>Promise.resolve(undefined),
6
+ refresh: async ()=>Promise.resolve(undefined),
7
7
  /**
8
- *
9
- * @deprecated There's no need for access token anymore, we use cookies
10
- */
11
- getAccessToken: async () => Promise.resolve(undefined),
8
+ *
9
+ * @deprecated There's no need for access token anymore, we use cookies
10
+ */ getAccessToken: async ()=>Promise.resolve(undefined),
12
11
  // TODO
13
12
  // eslint-disable-next-line @typescript-eslint/no-empty-function -- initial state
14
- logout: () => { },
15
- status: 'loading',
13
+ logout: ()=>{},
14
+ status: 'loading'
16
15
  };
17
- export const AuthContext = createContext(defaultContext);
16
+ export const AuthContext = /*#__PURE__*/ createContext(defaultContext);
package/dist/default.js CHANGED
@@ -1,4 +1,4 @@
1
- export const extractDefault = (mod) => {
1
+ export const extractDefault = (mod)=>{
2
2
  if (typeof mod === 'object' && mod !== null && 'default' in mod) {
3
3
  return mod.default;
4
4
  }
@@ -5,25 +5,29 @@ import bcrypt from 'bcryptjs';
5
5
  import { PROBLEM_INVALID_LOGIN } from '../constants.js';
6
6
  import { translations } from '../translations.js';
7
7
  import { verifyToken } from './verfiy-token.js';
8
- export const authenticateUser = async ({ login, password, }) => {
8
+ export const authenticateUser = async ({ login, password })=>{
9
9
  const isEmail = login.includes('@');
10
10
  // Check if the user exists
11
11
  const user = await prisma.user.findFirst({
12
12
  where: {
13
- ...(isEmail
14
- ? { email: login.toLowerCase().trim() }
15
- : { phoneNumber: login.replace(/[\s-]/g, '') }),
16
- },
13
+ ...isEmail ? {
14
+ email: login.toLowerCase().trim()
15
+ } : {
16
+ phoneNumber: login.replace(/[\s-]/g, '')
17
+ }
18
+ }
17
19
  });
18
20
  if (!user) {
19
21
  return new Err(makeProblem(PROBLEM_INVALID_LOGIN, translations.wrongUsernameOrPassword));
20
22
  }
21
23
  // Check the user's password
22
- if (isEmail && !(await bcrypt.compare(password, user.password))) {
24
+ if (isEmail && !await bcrypt.compare(password, user.password)) {
23
25
  return new Err(makeProblem(PROBLEM_INVALID_LOGIN, 'The email / password combination is invalid.'));
24
26
  }
25
27
  if (!isEmail) {
26
- const tokenResult = await verifyToken(password, { scope: 'verify-phone' });
28
+ const tokenResult = await verifyToken(password, {
29
+ scope: 'verify-phone'
30
+ });
27
31
  if (!tokenResult.ok) {
28
32
  return new Err(makeProblem(PROBLEM_INVALID_LOGIN, 'The OTP is invalid.'));
29
33
  }
@@ -1,28 +1,39 @@
1
1
  import { config } from '@driveflux/config/backend';
2
2
  import Cors from 'cors';
3
3
  const corsOptions = {
4
- origin: (origin, callback) => {
5
- const allowedOrigins = [config.appUrl];
6
- if (config.appEnv === 'development' ||
7
- (origin && allowedOrigins.includes(origin))) {
4
+ origin: (origin, callback)=>{
5
+ const allowedOrigins = [
6
+ config.appUrl
7
+ ];
8
+ if (config.appEnv === 'development' || origin && allowedOrigins.includes(origin)) {
8
9
  callback(null, true);
9
- }
10
- else {
10
+ } else {
11
11
  callback(new Error('CORS not allowed'));
12
12
  }
13
13
  },
14
- allowedHeaders: ['content-type', 'x-correlation-id', 'authorization'],
14
+ allowedHeaders: [
15
+ 'content-type',
16
+ 'x-correlation-id',
17
+ 'authorization'
18
+ ],
15
19
  exposedHeaders: '*',
16
- methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'],
17
- credentials: true,
20
+ methods: [
21
+ 'GET',
22
+ 'HEAD',
23
+ 'PUT',
24
+ 'PATCH',
25
+ 'POST',
26
+ 'DELETE'
27
+ ],
28
+ credentials: true
18
29
  };
19
30
  // Initializing the cors middleware
20
31
  // I don't have time to debug cors now, I've got a million things to do.
21
32
  // TODO Implement this properly please
22
33
  const corsHandler = config.appEnv === 'development' ? Cors(corsOptions) : Cors();
23
- export const cors = (req, res) => {
24
- return new Promise((resolve, reject) => {
25
- corsHandler(req, res, (result) => {
34
+ export const cors = (req, res)=>{
35
+ return new Promise((resolve, reject)=>{
36
+ corsHandler(req, res, (result)=>{
26
37
  if (result instanceof Error) {
27
38
  return reject(result);
28
39
  }
@@ -4,7 +4,7 @@ export function Credentials(options) {
4
4
  name: 'Credentials',
5
5
  type: 'flux-credentials',
6
6
  credentials: {},
7
- authorize: () => null,
8
- options,
7
+ authorize: ()=>null,
8
+ options
9
9
  };
10
10
  }