hapi-nextauth: update packages, fix linter and typescript
This commit is contained in:
parent
7aa1ec74eb
commit
fbe33c5c7b
6 changed files with 595 additions and 3870 deletions
4209
package-lock.json
generated
4209
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -1,18 +0,0 @@
|
|||
.PHONY: fmt test yarn
|
||||
.npmrc:
|
||||
echo '@guardianproject-ops:registry=https://gitlab.com/api/v4/packages/npm/' > .npmrc
|
||||
echo '//gitlab.com/api/v4/packages/npm/:_authToken=${CI_JOB_TOKEN}' >> .npmrc
|
||||
echo '//gitlab.com/api/v4/projects/:_authToken=${CI_JOB_TOKEN}' >> .npmrc
|
||||
echo '//gitlab.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}' >> .npmrc
|
||||
|
||||
yarn:
|
||||
yarn
|
||||
|
||||
test: yarn
|
||||
mkdir -p coverage
|
||||
yarn lint
|
||||
yarn test
|
||||
|
||||
publish: test .npmrc
|
||||
npm publish
|
||||
|
||||
|
|
@ -8,28 +8,26 @@
|
|||
"private": false,
|
||||
"devDependencies": {
|
||||
"@types/jest": "^27.0.2",
|
||||
"@hapi/basic": "^6.0.0",
|
||||
"@hapi/basic": "^7.0.1",
|
||||
"tsconfig-link": "*",
|
||||
"eslint-config-link": "*",
|
||||
"jest-config-link": "*"
|
||||
"jest-config-link": "*",
|
||||
"babel-preset-link": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"@hapi/hapi": "^20.2.0",
|
||||
"@hapi/hoek": "^9.2.1",
|
||||
"joi": "^17.4.2",
|
||||
"@hapipal/toys": "^3.1.0",
|
||||
"@hapi/hapi": "^21.3.0",
|
||||
"@hapi/hoek": "^11.0.1",
|
||||
"joi": "^17.6.1",
|
||||
"next-auth": "3.29.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"fix:lint": "eslint src --ext .ts --fix",
|
||||
"fix:prettier": "prettier \"src/**/*.ts\" --write",
|
||||
"test": "yarn test:jest && yarn test:lint && yarn test:prettier",
|
||||
"test:lint": "eslint src --ext .ts",
|
||||
"test:prettier": "prettier \"src/**/*.ts\" --list-different",
|
||||
"test:jest": "jest --coverage --forceExit --detectOpenHandles --reporters=default --reporters=jest-junit",
|
||||
"doc": "yarn run doc:html",
|
||||
"doc:html": "typedoc src/ --exclude '**/*.test.ts' --exclude '**/*.spec.ts' --name $npm_package_name --readme README.md --target es2019 --mode file --out build/docs",
|
||||
"fmt": "prettier \"src/**/*.ts\" --write",
|
||||
"test": "jest --coverage --forceExit --detectOpenHandles --reporters=default --reporters=jest-junit",
|
||||
"lint": "eslint src --ext .ts",
|
||||
"lint-fmt": "prettier \"src/**/*.ts\" --list-different",
|
||||
"doc": "typedoc src/ --exclude '**/*.test.ts' --exclude '**/*.spec.ts' --name $npm_package_name --readme README.md --target es2019 --mode file --out build/docs",
|
||||
"watch:build": "tsc -p tsconfig.json -w"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,15 +62,12 @@ const register = async <TUser, TProfile, TSession>(
|
|||
server: Hapi.Server,
|
||||
pluginOpts?: NextAuthPluginOptions<TUser, TProfile, TSession>
|
||||
): Promise<void> => {
|
||||
const options: NextAuthPluginOptions<
|
||||
TUser,
|
||||
TProfile,
|
||||
TSession
|
||||
> = Hoek.applyToDefaults(
|
||||
// a little type gymnastics here to workaround poor typing
|
||||
defaultOptions as unknown,
|
||||
pluginOpts
|
||||
) as NextAuthPluginOptions<TUser, TProfile, TSession>;
|
||||
const options: NextAuthPluginOptions<TUser, TProfile, TSession> =
|
||||
Hoek.applyToDefaults(
|
||||
// a little type gymnastics here to workaround poor typing
|
||||
defaultOptions as unknown,
|
||||
pluginOpts
|
||||
) as NextAuthPluginOptions<TUser, TProfile, TSession>;
|
||||
|
||||
if (!options.nextAuthAdapterFactory) {
|
||||
throw new Error(
|
||||
|
|
|
|||
|
|
@ -1,138 +1,155 @@
|
|||
/* eslint-disable unicorn/no-null */
|
||||
import Toys from "@hapipal/toys";
|
||||
import * as Joi from "joi";
|
||||
import * as Hapi from "@hapi/hapi";
|
||||
import { NextAuthPluginOptions } from "./types";
|
||||
import { ResponseToolkit, ResponseObject } from "@hapi/hapi";
|
||||
|
||||
export interface LinkAccountPayload {
|
||||
userId: string;
|
||||
providerType: string;
|
||||
providerId: string;
|
||||
providerAccountId: string;
|
||||
refreshToken: string;
|
||||
accessToken: string;
|
||||
accessTokenExpires?: null;
|
||||
}
|
||||
|
||||
export const register = async <TUser, TProfile, TSession>(
|
||||
server: Hapi.Server,
|
||||
opts: NextAuthPluginOptions<TUser, TProfile, TSession>,
|
||||
auth?: string
|
||||
): Promise<void> => {
|
||||
const withDefaults = Toys.withRouteDefaults({
|
||||
options: {
|
||||
auth,
|
||||
tags: opts.tags,
|
||||
},
|
||||
});
|
||||
|
||||
const { tags, basePath, validators } = opts;
|
||||
const { session, user, userId, profile } = validators;
|
||||
server.route([
|
||||
withDefaults({
|
||||
{
|
||||
method: "POST",
|
||||
path: `${opts.basePath}/createUser`,
|
||||
path: `${basePath}/createUser`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
payload: opts.validators.profile,
|
||||
payload: profile,
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TUser> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<Hapi.ResponseObject> {
|
||||
const payload: TProfile = request.payload as TProfile;
|
||||
const r = await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.createUser(payload);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Create a user from a profile",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
path: `${opts.basePath}/getUser/{userId}`,
|
||||
path: `${basePath}/getUser/{userId}`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
params: {
|
||||
userId: opts.validators.userId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TUser | null> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const id = request.params.userId;
|
||||
const r = await opts.nextAuthAdapterFactory(request).getUser(id);
|
||||
if (!r) return h.response().code(404);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Get a user by id",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
path: `${opts.basePath}/getUserByEmail/{userEmail}`,
|
||||
path: `${basePath}/getUserByEmail/{userEmail}`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
params: {
|
||||
userEmail: Joi.string().email(),
|
||||
},
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TUser | null> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const email = request.params.userEmail;
|
||||
const r = await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.getUserByEmail(email);
|
||||
if (!r) return h.response().code(404);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Get a user by email",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
path: `${opts.basePath}/getUserByProviderAccountId/{providerId}/{providerAccountId}`,
|
||||
path: `${basePath}/getUserByProviderAccountId/{providerId}/{providerAccountId}`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
params: {
|
||||
providerId: Joi.string(),
|
||||
providerAccountId: Joi.string(),
|
||||
},
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TUser | null> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const { providerId, providerAccountId } = request.params;
|
||||
const r = await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.getUserByProviderAccountId(providerId, providerAccountId);
|
||||
if (!r) return h.response().code(404);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Get a user by provider id and provider account id",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "PUT",
|
||||
path: `${opts.basePath}/updateUser`,
|
||||
path: `${basePath}/updateUser`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
payload: opts.validators.user,
|
||||
payload: user,
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TUser> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const payload: TUser = request.payload as TUser;
|
||||
const r = await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.updateUser(payload);
|
||||
if (!r) return h.response().code(404);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Update a user's data",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "PUT",
|
||||
path: `${opts.basePath}/linkAccount`,
|
||||
path: `${basePath}/linkAccount`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
payload: Joi.object({
|
||||
userId: opts.validators.userId,
|
||||
userId,
|
||||
providerId: Joi.string(),
|
||||
providerType: Joi.string(),
|
||||
providerAccountId: Joi.string(),
|
||||
|
|
@ -141,10 +158,10 @@ export const register = async <TUser, TProfile, TSession>(
|
|||
accessTokenExpires: Joi.number().optional().allow(null),
|
||||
}).options({ presence: "required" }),
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<void> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const {
|
||||
userId,
|
||||
providerId,
|
||||
|
|
@ -153,7 +170,7 @@ export const register = async <TUser, TProfile, TSession>(
|
|||
refreshToken,
|
||||
accessToken,
|
||||
accessTokenExpires,
|
||||
} = request.payload;
|
||||
} = request.payload as LinkAccountPayload;
|
||||
await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.linkAccount(
|
||||
|
|
@ -169,94 +186,104 @@ export const register = async <TUser, TProfile, TSession>(
|
|||
},
|
||||
description: "Link a provider account with a user",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
path: `${opts.basePath}/createSession`,
|
||||
path: `${basePath}/createSession`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
payload: opts.validators.user,
|
||||
payload: user,
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TSession> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const payload: TUser = request.payload as TUser;
|
||||
const r = await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.createSession(payload);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Create a new session for a user",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
path: `${opts.basePath}/getSession/{sessionToken}`,
|
||||
path: `${basePath}/getSession/{sessionToken}`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
params: {
|
||||
sessionToken: Joi.string(),
|
||||
},
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TSession | null> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const token = request.params.sessionToken;
|
||||
const r = await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.getSession(token);
|
||||
if (!r) return h.response().code(404);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Get a session by its token",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "PUT",
|
||||
path: `${opts.basePath}/updateSession`,
|
||||
path: `${basePath}/updateSession`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
payload: opts.validators.session,
|
||||
payload: session,
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<TSession> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const inputPayload = request.payload as any;
|
||||
const { expires } = inputPayload;
|
||||
const payload = {
|
||||
...request.payload,
|
||||
expires: new Date(request.payload.expires),
|
||||
...inputPayload,
|
||||
expires: new Date(expires),
|
||||
};
|
||||
const force = Boolean(request.query.force);
|
||||
const r = await opts
|
||||
.nextAuthAdapterFactory(request)
|
||||
.updateSession(payload, force);
|
||||
if (!r) return h.response().code(204);
|
||||
return h.response(r);
|
||||
return h.response(r as object);
|
||||
},
|
||||
description: "Update a session for a user",
|
||||
},
|
||||
}),
|
||||
withDefaults({
|
||||
},
|
||||
{
|
||||
method: "DELETE",
|
||||
path: `${opts.basePath}/deleteSession/{sessionToken}`,
|
||||
path: `${basePath}/deleteSession/{sessionToken}`,
|
||||
options: {
|
||||
auth,
|
||||
tags,
|
||||
validate: {
|
||||
params: {
|
||||
sessionToken: Joi.string(),
|
||||
},
|
||||
},
|
||||
handler: async (
|
||||
async handler(
|
||||
request: Hapi.Request,
|
||||
h: Hapi.Toolkit
|
||||
): Promise<void> => {
|
||||
h: ResponseToolkit
|
||||
): Promise<ResponseObject> {
|
||||
const token = request.params.sessionToken;
|
||||
await opts.nextAuthAdapterFactory(request).deleteSession(token);
|
||||
return h.response().code(204);
|
||||
},
|
||||
description: "Delete a user's session",
|
||||
},
|
||||
}),
|
||||
},
|
||||
]);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"extends": "tsconfig",
|
||||
"extends": "tsconfig-link",
|
||||
"compilerOptions": {
|
||||
"incremental": true,
|
||||
"outDir": "build/main",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue