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
|
|
@ -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",
|
||||
},
|
||||
}),
|
||||
},
|
||||
]);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue