Skip to content

Commit 5772094

Browse files
authoredNov 21, 2023
feat: add support for auth providers (SoftwareBrothers#62)
* feat: add support for auth providers
1 parent b852dbd commit 5772094

9 files changed

+45
-83
lines changed
 

‎package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@
2525
"peerDependencies": {
2626
"@nestjs/common": ">=9.0.5",
2727
"@nestjs/core": ">=9.0.5",
28-
"adminjs": "^7.0.0",
28+
"adminjs": "^7.4.0",
2929
"rxjs": "^7.1.0"
3030
},
3131
"devDependencies": {
32-
"@adminjs/express": "^6.0.0",
32+
"@adminjs/express": "^6.1.0",
3333
"@commitlint/cli": "^17.4.4",
3434
"@commitlint/config-conventional": "^17.4.4",
3535
"@nestjs/common": ">=9.3.10",
3636
"@nestjs/core": ">=9.3.10",
3737
"@semantic-release/git": "^10.0.1",
3838
"@typescript-eslint/eslint-plugin": "^5.56.0",
3939
"@typescript-eslint/parser": "^5.56.0",
40-
"adminjs": "^7.0.0",
40+
"adminjs": "^7.4.0",
4141
"eslint": "^8.36.0",
4242
"eslint-config-airbnb": "^19.0.4",
4343
"eslint-plugin-import": "^2.27.5",

‎src/admin-resource.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DynamicModule, Module } from '@nestjs/common';
2-
import { ResourceWithOptions } from 'adminjs';
2+
import { type ResourceWithOptions } from 'adminjs';
33

44
import AdminResourceService from './admin-resource.service.js';
55

‎src/admin-resource.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2-
import { ResourceWithOptions } from 'adminjs';
2+
import { type ResourceWithOptions } from 'adminjs';
33

44
@Injectable()
55
class AdminResourceService {

‎src/admin.module.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { DynamicModule, Inject, Module, OnModuleInit } from '@nestjs/common'
22
import { HttpAdapterHost } from '@nestjs/core'
3-
import AdminJS from 'adminjs'
43

54
import AdminResourceService from './admin-resource.service.js'
65
import { AdminModuleFactory } from './interfaces/admin-module-factory.interface.js'
@@ -122,11 +121,13 @@ export class AdminModule implements OnModuleInit {
122121
/**
123122
* Applies given options to AdminJS and initializes it
124123
*/
125-
public onModuleInit() {
124+
public async onModuleInit() {
126125
if ('shouldBeInitialized' in this.adminModuleOptions && !this.adminModuleOptions.shouldBeInitialized) {
127126
return;
128127
}
129128

129+
const { default: AdminJS } = await import('adminjs');
130+
130131
const forFeatureResources = AdminResourceService.getResources()
131132

132133
const adminJSOptions = forFeatureResources.length > 0

‎src/interfaces/admin-module-options.interface.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AdminJSOptions, CurrentAdmin } from 'adminjs';
1+
import type { AdminJSOptions, BaseAuthProvider, CurrentAdmin } from 'adminjs';
22
import { SessionOptions } from 'express-session';
33

44
import { ExpressFormidableOptions } from './express-formidable-options.interface.js';
@@ -21,9 +21,10 @@ export type AdminModuleOptions = {
2121
/**
2222
* verifies if given credentials are valid, therefore if user has access to Admin Panel
2323
*/
24-
authenticate: (email: string, password: string) => Promise<CurrentAdmin | null>,
24+
authenticate?: (email: string, password: string, ctx?: any) => Promise<CurrentAdmin | null>,
2525
cookiePassword: string,
2626
cookieName: string,
27+
provider?: BaseAuthProvider,
2728
}
2829
/**
2930
* Options passed to express formidable (used only by AdminJS express module)

‎src/loaders/abstract.loader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { AbstractHttpAdapter } from '@nestjs/core';
3-
import AdminJS from 'adminjs';
3+
import type AdminJS from 'adminjs';
44

55
import { AdminModuleOptions } from '../interfaces/admin-module-options.interface.js';
66

‎src/loaders/express.loader.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Injectable } from '@nestjs/common';
33
import { loadPackage } from '@nestjs/common/utils/load-package.util.js';
44
import { AbstractHttpAdapter } from '@nestjs/core';
5-
import AdminJS from 'adminjs';
5+
import type AdminJS from 'adminjs';
66

77
import { AdminModuleOptions } from '../interfaces/admin-module-options.interface.js';
88

@@ -26,13 +26,21 @@ export class ExpressLoader extends AbstractLoader {
2626
if (options.auth) {
2727
loadPackage('express-session', '@adminjs/nestjs');
2828
router = adminJsExpressjs.default.buildAuthenticatedRouter(
29-
admin, options.auth, undefined, options.sessionOptions, options.formidableOptions,
29+
admin,
30+
options.auth,
31+
undefined,
32+
options.sessionOptions as any,
33+
options.formidableOptions,
3034
);
3135
} else {
32-
router = adminJsExpressjs.default.buildRouter(admin, undefined, options.formidableOptions);
36+
router = adminJsExpressjs.default.buildRouter(
37+
admin,
38+
undefined,
39+
options.formidableOptions,
40+
);
3341
}
3442

35-
// This named function is there on purpose.
43+
// This named function is there on purpose.
3644
// It names layer in main router with the name of the function, which helps localize
3745
// admin layer in reorderRoutes() step.
3846
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
@@ -69,20 +77,20 @@ export class ExpressLoader extends AbstractLoader {
6977
(layer: { name: string }) => layer.name === 'admin',
7078
);
7179
if (adminIndex >= 0) {
72-
admin = app._router.stack.splice(adminIndex, 1)
80+
admin = app._router.stack.splice(adminIndex, 1);
7381
}
7482

7583
// if adminjs-nestjs didn't reorder the middleware
7684
// the body parser would have come after corsMiddleware
7785
const corsIndex = app._router.stack.findIndex(
7886
(layer: { name: string }) => layer.name === 'corsMiddleware',
79-
)
87+
);
8088

8189
// in other case if there is no corsIndex we go after expressInit, because right after that
8290
// there are nest endpoints.
8391
const expressInitIndex = app._router.stack.findIndex(
8492
(layer: { name: string }) => layer.name === 'expressInit',
85-
)
93+
);
8694

8795
const initIndex = (corsIndex >= 0 ? corsIndex : expressInitIndex) + 1;
8896

@@ -92,7 +100,7 @@ export class ExpressLoader extends AbstractLoader {
92100
...admin,
93101
...jsonParser,
94102
...urlencodedParser,
95-
)
103+
);
96104
}
97105
}
98106
}

‎src/loaders/noop.loader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable @typescript-eslint/no-empty-function */
33
import { Injectable } from '@nestjs/common';
44
import { AbstractHttpAdapter } from '@nestjs/core';
5-
import AdminJS from 'adminjs';
5+
import type AdminJS from 'adminjs';
66

77
import { AdminModuleOptions } from '../interfaces/admin-module-options.interface.js';
88

‎yarn.lock

+16-64
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
styled-system "^5.1.5"
4343
text-mask-addons "^3.8.0"
4444

45-
"@adminjs/express@^6.0.0":
46-
version "6.0.0"
47-
resolved "https://registry.yarnpkg.com/@adminjs/express/-/express-6.0.0.tgz#9ad77dda79034682aee22f746f9b9460d5e5739a"
48-
integrity sha512-TIylSszG5yUp+uHFYWWCrlztViiR05KM7suDo4SWd4UezZYc0mwNgfYPsnEmKkf5VFMEiCtYqhdCt38OxUPn/g==
45+
"@adminjs/express@^6.1.0":
46+
version "6.1.0"
47+
resolved "https://registry.yarnpkg.com/@adminjs/express/-/express-6.1.0.tgz#9f5035e3d6de0b00b9ea741dde354b97c6faed6f"
48+
integrity sha512-B3yoWGSp8xeqS/geBIKoSnMx46tbYYrbb/EmKD0SqqlVfxtBwgyoTWa6pkxqRc2T4iTMOl84VkMT0axnooAmZA==
4949

5050
"@ampproject/remapping@^2.2.0":
5151
version "2.2.0"
@@ -2662,51 +2662,6 @@
26622662
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.3.tgz#472eaab5f15c1ffdd7f8628bd4c4f753995ec79e"
26632663
integrity sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==
26642664

2665-
"@types/babel-core@^6.25.7":
2666-
version "6.25.7"
2667-
resolved "https://registry.npmjs.org/@types/babel-core/-/babel-core-6.25.7.tgz"
2668-
integrity sha512-WPnyzNFVRo6bxpr7bcL27qXtNKNQ3iToziNBpibaXHyKGWQA0+tTLt73QQxC/5zzbM544ih6Ni5L5xrck6rGwg==
2669-
dependencies:
2670-
"@types/babel-generator" "*"
2671-
"@types/babel-template" "*"
2672-
"@types/babel-traverse" "*"
2673-
"@types/babel-types" "*"
2674-
"@types/babylon" "*"
2675-
2676-
"@types/babel-generator@*":
2677-
version "6.25.5"
2678-
resolved "https://registry.npmjs.org/@types/babel-generator/-/babel-generator-6.25.5.tgz"
2679-
integrity sha512-lhbwMlAy5rfWG+R6l8aPtJdEFX/kcv6LMFIuvUb0i89ehqgD24je9YcB+0fRspQhgJGlEsUImxpw4pQeKS/+8Q==
2680-
dependencies:
2681-
"@types/babel-types" "*"
2682-
2683-
"@types/babel-template@*":
2684-
version "6.25.2"
2685-
resolved "https://registry.npmjs.org/@types/babel-template/-/babel-template-6.25.2.tgz"
2686-
integrity sha512-QKtDQRJmAz3Y1HSxfMl0syIHebMc/NnOeH/8qeD0zjgU2juD0uyC922biMxCy5xjTNvHinigML2l8kxE8eEBmw==
2687-
dependencies:
2688-
"@types/babel-types" "*"
2689-
"@types/babylon" "*"
2690-
2691-
"@types/babel-traverse@*":
2692-
version "6.25.7"
2693-
resolved "https://registry.npmjs.org/@types/babel-traverse/-/babel-traverse-6.25.7.tgz"
2694-
integrity sha512-BeQiEGLnVzypzBdsexEpZAHUx+WucOMXW6srEWDkl4SegBlaCy+iBvRO+4vz6EZ+BNQg22G4MCdDdvZxf+jW5A==
2695-
dependencies:
2696-
"@types/babel-types" "*"
2697-
2698-
"@types/babel-types@*":
2699-
version "7.0.11"
2700-
resolved "https://registry.npmjs.org/@types/babel-types/-/babel-types-7.0.11.tgz"
2701-
integrity sha512-pkPtJUUY+Vwv6B1inAz55rQvivClHJxc9aVEPPmaq2cbyeMLCiDpbKpcKyX4LAwpNGi+SHBv0tHv6+0gXv0P2A==
2702-
2703-
"@types/babylon@*":
2704-
version "6.16.6"
2705-
resolved "https://registry.npmjs.org/@types/babylon/-/babylon-6.16.6.tgz"
2706-
integrity sha512-G4yqdVlhr6YhzLXFKy5F7HtRBU8Y23+iWy7UKthMq/OSQnL1hbsoeXESQ2LY8zEDlknipDG3nRGhUC9tkwvy/w==
2707-
dependencies:
2708-
"@types/babel-types" "*"
2709-
27102665
"@types/body-parser@*":
27112666
version "1.19.1"
27122667
resolved "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.1.tgz"
@@ -2849,15 +2804,6 @@
28492804
"@types/scheduler" "*"
28502805
csstype "^3.0.2"
28512806

2852-
"@types/react@^18.0.28":
2853-
version "18.0.28"
2854-
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.0.28.tgz#accaeb8b86f4908057ad629a26635fe641480065"
2855-
integrity sha512-RD0ivG1kEztNBdoAK7lekI9M+azSnitIn85h4iOiaLjaTrMjzslhaqCGaI4IyCJ1RljWiLCEu4jyrLLgqxBTew==
2856-
dependencies:
2857-
"@types/prop-types" "*"
2858-
"@types/scheduler" "*"
2859-
csstype "^3.0.2"
2860-
28612807
"@types/resolve@1.20.2":
28622808
version "1.20.2"
28632809
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.20.2.tgz#97d26e00cd4a0423b4af620abecf3e6f442b7975"
@@ -3013,10 +2959,10 @@ acorn@^8.4.1, acorn@^8.5.0, acorn@^8.8.0:
30132959
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.2.tgz#1b2f25db02af965399b9776b0c2c391276d37c4a"
30142960
integrity sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==
30152961

3016-
adminjs@^7.0.0:
3017-
version "7.0.0"
3018-
resolved "https://registry.yarnpkg.com/adminjs/-/adminjs-7.0.0.tgz#5dad16fcdd91dfe9fd84402b3e109f9fdbb74534"
3019-
integrity sha512-6cvr04yhPpoqpK9lfy5ohxHMUI+J9lDZbRScyqzmpPTZ4P8E68unZekixx7nAGXFBmhixP5+CumLNpCNzcUeGA==
2962+
adminjs@^7.4.0:
2963+
version "7.4.0"
2964+
resolved "https://registry.yarnpkg.com/adminjs/-/adminjs-7.4.0.tgz#9551c79ac1b6047f1cc86ac1525e01660fea954a"
2965+
integrity sha512-GKot4WNEe5aQN2MLkSR216N0oE9KrpJ+COwPrYhRlF42wUMiQucwQbq36VfMb/ZsiEpF3SfBdSa9Qi6EApR0WQ==
30202966
dependencies:
30212967
"@adminjs/design-system" "^4.0.0"
30222968
"@babel/core" "^7.21.0"
@@ -3035,8 +2981,6 @@ adminjs@^7.0.0:
30352981
"@rollup/plugin-node-resolve" "^15.0.1"
30362982
"@rollup/plugin-replace" "^5.0.2"
30372983
"@rollup/plugin-terser" "^0.4.0"
3038-
"@types/babel-core" "^6.25.7"
3039-
"@types/react" "^18.0.28"
30402984
axios "^1.3.4"
30412985
commander "^10.0.0"
30422986
flat "^5.0.2"
@@ -3047,6 +2991,7 @@ adminjs@^7.0.0:
30472991
ora "^6.2.0"
30482992
prop-types "^15.8.1"
30492993
punycode "^2.3.0"
2994+
qs "^6.11.1"
30502995
react "^18.2.0"
30512996
react-dom "^18.2.0"
30522997
react-feather "^2.0.10"
@@ -7806,6 +7751,13 @@ qrcode-terminal@^0.12.0:
78067751
resolved "https://registry.npmjs.org/qrcode-terminal/-/qrcode-terminal-0.12.0.tgz"
78077752
integrity sha512-EXtzRZmC+YGmGlDFbXKxQiMZNwCLEO6BANKXG4iCtSIM0yqc/pappSx3RIKr4r0uh5JsBckOXeKrB3Iz7mdQpQ==
78087753

7754+
qs@^6.11.1:
7755+
version "6.11.2"
7756+
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.2.tgz#64bea51f12c1f5da1bc01496f48ffcff7c69d7d9"
7757+
integrity sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==
7758+
dependencies:
7759+
side-channel "^1.0.4"
7760+
78097761
queue-microtask@^1.2.2:
78107762
version "1.2.3"
78117763
resolved "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz"

0 commit comments

Comments
 (0)
Please sign in to comment.