Skip to content

Commit

Permalink
fix: fix various typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Aug 3, 2022
1 parent 12baaf2 commit f6004f5
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 20 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"@hapi/hapi": "^20.2.1",
"@hapi/inert": "^6.0.5",
"@semantic-release/git": "^9.0.0",
"@types/hapi__cookie": "^10.1.4",
"@types/hapi__hapi": "^20.0.12",
"@types/hapi__inert": "^5.2.3",
"@types/node": "^16.11.26",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0",
Expand All @@ -69,5 +72,8 @@
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"dependencies": {
"tslib": "^2.4.0"
}
}
29 changes: 23 additions & 6 deletions src/extensions/session-auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { ExtendedAdminJSOptions } from './../plugin';
import AdminJS from 'adminjs';
import Hapi from '@hapi/hapi';
import HapiAuthCookie from '@hapi/cookie';
import { ExtendedAdminJSOptionsWithDefault } from './../plugin';

interface AuthRequestPayload {
email: string;
password: string;
}

/**
* Creates authentication logic for admin users
*/
const sessionAuth = async (server: Hapi, adminJs: AdminJS) => {
const options = adminJs.options as ExtendedAdminJSOptions;
const sessionAuth = async (server: Hapi.Server, adminJs: AdminJS) => {
const options = adminJs.options as ExtendedAdminJSOptionsWithDefault;
const { loginPath, logoutPath, rootPath } = options;
const { cookiePassword, authenticate, isSecure, defaultMessage, cookieName, strategy, ...other } = options.auth;
const {
cookiePassword,
authenticate,
isSecure,
defaultMessage,
cookieName,
strategy = 'simple',
...other
} = options.auth;

if (!authenticate) {
throw new Error('"authenticate" function must be provided for authenticated access');
}

// example authentication is based on the cookie store
await server.register(HapiAuthCookie);
Expand All @@ -35,8 +52,8 @@ const sessionAuth = async (server: Hapi, adminJs: AdminJS) => {
try {
let errorMessage = defaultMessage;
if (request.method === 'post') {
const { email, password } = request.payload;
const admin = await authenticate!(email, password);
const { email, password } = request.payload as AuthRequestPayload;
const admin = await authenticate(email, password);
if (admin) {
request.cookieAuth.set(admin);
return h.redirect(rootPath);
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
* Only thing you have to do is to define the following {@link module:@adminjs/hapi.register auth options}:
* _authenticate_, _cookiePassword_, _isSecure_, _cookieName_.
*/

import AdminJSHapi from './plugin';
export { ExtendedAdminJSOptions, AuthOptions } from './plugin';

Expand Down
3 changes: 1 addition & 2 deletions src/info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Sample info file, it gets updated during release by semantic-release
export default {
name: '@adminjs/hapi',
version: '5.0.0',
version: '6.0.0',
};
21 changes: 13 additions & 8 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path';
import Boom from '@hapi/boom';
import inert from '@hapi/inert';
import Hapi from '@hapi/hapi';
import AdminJS, { AdminJSOptions, Router as AdminRouter } from 'adminjs';
import Hapi, { Plugin, RouteOptions } from '@hapi/hapi';
import AdminJS, { AdminJSOptions, AdminJSOptionsWithDefault, Router as AdminRouter } from 'adminjs';
import sessionAuth from './extensions/session-auth';
import info from './info';

Expand All @@ -11,12 +11,13 @@ import info from './info';
* @private
*/

export type AuthenticateResult = Promise<Record<string, unknown> | null | false> | null | false;
export type AuthOptions = {
/**
* Function takes email and password as argument. Should return a logged-in user object or null/false.
* If provided, the strategy is set to 'session'.
*/
authenticate?: (email: string, password: string) => Promise<Record<string, any>> | null | false;
authenticate?: (email: string, password: string) => AuthenticateResult;
/**
* Auth strategy for Hapi routes.
*/
Expand All @@ -36,7 +37,7 @@ export type AuthOptions = {
/**
* Cookie options: https://github.com/hapijs/cookie
*/
[key: string]: any;
[key: string]: unknown;
};

export type ExtendedAdminJSOptions = AdminJSOptions & {
Expand All @@ -51,6 +52,8 @@ export type ExtendedAdminJSOptions = AdminJSOptions & {
auth: AuthOptions;
};

export type ExtendedAdminJSOptionsWithDefault = AdminJSOptionsWithDefault & ExtendedAdminJSOptions;

/**
* Actual method that Hapi uses under the hood when you call
* server.register(plugin, options) method.
Expand Down Expand Up @@ -97,7 +100,7 @@ export type ExtendedAdminJSOptions = AdminJSOptions & {
*
* start()
*/
const register = async (server: Hapi, options: ExtendedAdminJSOptions) => {
const register = async (server: Hapi.Server, options: ExtendedAdminJSOptions) => {
const { registerInert = true } = options;
const { routes, assets } = AdminRouter;

Expand All @@ -122,7 +125,7 @@ const register = async (server: Hapi, options: ExtendedAdminJSOptions) => {
}

routes.forEach((route) => {
const opts =
const opts: RouteOptions =
route.method === 'POST'
? {
auth: options.auth?.strategy,
Expand Down Expand Up @@ -180,10 +183,12 @@ const register = async (server: Hapi, options: ExtendedAdminJSOptions) => {
});
});

return admin;
// Note: Returning AdminJS as any because `register` is typed to return `Promise<void>` but
// we might need to access created AdminJS instance outside of the plugin
return admin as any;
};

const AdminJSHapi = {
const AdminJSHapi: Plugin<ExtendedAdminJSOptions> = {
name: info?.name ?? '@adminjs/hapi',
version: info?.version ?? 5,
register,
Expand Down
92 changes: 89 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@
dependencies:
"@hapi/hoek" "9.x.x"

"@hapi/[email protected]", "@hapi/boom@^9.1.0", "@hapi/boom@^9.1.4":
"@hapi/[email protected]", "@hapi/boom@^9.0.0", "@hapi/boom@^9.1.0", "@hapi/boom@^9.1.4":
version "9.1.4"
resolved "https://registry.yarnpkg.com/@hapi/boom/-/boom-9.1.4.tgz#1f9dad367c6a7da9f8def24b4a986fc5a7bd9db6"
integrity sha512-Ls1oH8jaN1vNsqcaHVYJrKmgMcKsC1wcp8bujvXrHaAqD2iDYq3HoOwsxwo09Cuda5R5nC0o0IxlrlTuvPuzSw==
Expand Down Expand Up @@ -1478,7 +1478,7 @@
"@hapi/validate" "1.x.x"
lru-cache "^6.0.0"

"@hapi/[email protected]":
"@hapi/[email protected]", "@hapi/iron@^6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@hapi/iron/-/iron-6.0.0.tgz#ca3f9136cda655bdd6028de0045da0de3d14436f"
integrity sha512-zvGvWDufiTGpTJPG1Y/McN8UqWBu0k/xs/7l++HVU535NLHXsHhy54cfEMdW7EjwKfbBfM9Xy25FmTiobb7Hvw==
Expand Down Expand Up @@ -1516,7 +1516,7 @@
"@hapi/hoek" "9.x.x"
"@hapi/nigel" "4.x.x"

"@hapi/[email protected]", "@hapi/podium@^4.1.1":
"@hapi/[email protected]", "@hapi/podium@^4.1.1", "@hapi/podium@^4.1.3":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@hapi/podium/-/podium-4.1.3.tgz#91e20838fc2b5437f511d664aabebbb393578a26"
integrity sha512-ljsKGQzLkFqnQxE7qeanvgGj4dejnciErYd30dbrYzUOF/FyS/DOF97qcrT3bhoVwCYmxa6PEMhxfCPlnUcD2g==
Expand Down Expand Up @@ -2082,6 +2082,23 @@
lodash "^4.17.4"
read-pkg-up "^7.0.0"

"@sideway/address@^4.1.3":
version "4.1.4"
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.4.tgz#03dccebc6ea47fdc226f7d3d1ad512955d4783f0"
integrity sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw==
dependencies:
"@hapi/hoek" "^9.0.0"

"@sideway/formula@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c"
integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg==

"@sideway/pinpoint@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df"
integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==

"@styled-system/background@^5.1.2":
version "5.1.2"
resolved "https://registry.yarnpkg.com/@styled-system/background/-/background-5.1.2.tgz#75c63d06b497ab372b70186c0bf608d62847a2ba"
Expand Down Expand Up @@ -2454,6 +2471,54 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/hapi__catbox@*":
version "10.2.4"
resolved "https://registry.yarnpkg.com/@types/hapi__catbox/-/hapi__catbox-10.2.4.tgz#4d0531a6c2d0e45024f724020d536041ef8ffe30"
integrity sha512-A6ivRrXD5glmnJna1UAGw87QNZRp/vdFO9U4GS+WhOMWzHnw+oTGkMvg0g6y1930CbeheGOCm7A1qHsqH7AXqg==

"@types/hapi__cookie@^10.1.4":
version "10.1.4"
resolved "https://registry.yarnpkg.com/@types/hapi__cookie/-/hapi__cookie-10.1.4.tgz#a9d83ab122022f7d73e67b9e67f0e7a625dd42e5"
integrity sha512-CESd2IRnTYAnr+gxHGTXIuLOvi5+M2dw4aG6efP+K6bVTdE385Cu63k/DNLO6iQX0hoXtxCYUKy1QyQEy/Xg5A==
dependencies:
"@types/hapi__hapi" "*"
joi "^17.3.0"

"@types/hapi__hapi@*", "@types/hapi__hapi@^20.0.12":
version "20.0.12"
resolved "https://registry.yarnpkg.com/@types/hapi__hapi/-/hapi__hapi-20.0.12.tgz#f88bdfac7f05406d32f2453fbea8ee915fdc89c5"
integrity sha512-B+0fceCzFvbIOVv5YWOZzbHtEff8BLlGH3etrkcOedyj7F0unC5FjzFfaaO5gwlhJDdX0cmmMeRg2pwRdMa2CQ==
dependencies:
"@hapi/boom" "^9.0.0"
"@hapi/iron" "^6.0.0"
"@hapi/podium" "^4.1.3"
"@types/hapi__catbox" "*"
"@types/hapi__mimos" "*"
"@types/hapi__shot" "*"
"@types/node" "*"
joi "^17.3.0"

"@types/hapi__inert@^5.2.3":
version "5.2.3"
resolved "https://registry.yarnpkg.com/@types/hapi__inert/-/hapi__inert-5.2.3.tgz#f586eb240d5997c9968d1b4e8b37679517045ca1"
integrity sha512-I1mWQrEc7oMqGtofT0rwBgRBCBurz0wNzbq8QZsHWR+aXM0bk1j9GA6zwyGIeO53PNl2C1c2kpXlc084xCV+Tg==
dependencies:
"@types/hapi__hapi" "*"

"@types/hapi__mimos@*":
version "4.1.4"
resolved "https://registry.yarnpkg.com/@types/hapi__mimos/-/hapi__mimos-4.1.4.tgz#4f8a1c58345fc468553708d3cb508724aa081bd9"
integrity sha512-i9hvJpFYTT/qzB5xKWvDYaSXrIiNqi4ephi+5Lo6+DoQdwqPXQgmVVOZR+s3MBiHoFqsCZCX9TmVWG3HczmTEQ==
dependencies:
"@types/mime-db" "*"

"@types/hapi__shot@*":
version "4.1.2"
resolved "https://registry.yarnpkg.com/@types/hapi__shot/-/hapi__shot-4.1.2.tgz#d4011999a91e8101030fece1462fe99769455855"
integrity sha512-8wWgLVP1TeGqgzZtCdt+F+k15DWQvLG1Yv6ZzPfb3D5WIo5/S+GGKtJBVo2uNEcqabP5Ifc71QnJTDnTmw1axA==
dependencies:
"@types/node" "*"

"@types/hoist-non-react-statics@^3.3.0":
version "3.3.1"
resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#1124aafe5118cb591977aeb1ceaaed1070eb039f"
Expand All @@ -2472,6 +2537,11 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/mime-db@*":
version "1.43.1"
resolved "https://registry.yarnpkg.com/@types/mime-db/-/mime-db-1.43.1.tgz#c2a0522453bb9b6e84ee48b7eef765d19bcd519e"
integrity sha512-kGZJY+R+WnR5Rk+RPHUMERtb2qBRViIHCBdtUrY+NmwuGb8pQdfTqQiCKPrxpdoycl8KWm2DLdkpoSdt479XoQ==

"@types/minimist@^1.2.0":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
Expand Down Expand Up @@ -5744,6 +5814,17 @@ jest-worker@^26.0.0:
merge-stream "^2.0.0"
supports-color "^7.0.0"

joi@^17.3.0:
version "17.6.0"
resolved "https://registry.yarnpkg.com/joi/-/joi-17.6.0.tgz#0bb54f2f006c09a96e75ce687957bd04290054b2"
integrity sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw==
dependencies:
"@hapi/hoek" "^9.0.0"
"@hapi/topo" "^5.0.0"
"@sideway/address" "^4.1.3"
"@sideway/formula" "^3.0.0"
"@sideway/pinpoint" "^2.0.0"

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down Expand Up @@ -9088,6 +9169,11 @@ tslib@^1.8.1:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==

tsutils@^3.21.0:
version "3.21.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"
Expand Down

0 comments on commit f6004f5

Please sign in to comment.