chore: typescript version compatibilities and improvements
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Response } from 'express';
|
||||
import { Result } from '../login';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { TypeWithID } from '../../../collections/config/types';
|
||||
|
||||
export type Options = {
|
||||
collection: string
|
||||
@@ -17,7 +18,7 @@ export type Options = {
|
||||
showHiddenFields?: boolean
|
||||
}
|
||||
|
||||
async function login(options: Options): Promise<Result> {
|
||||
async function login<T extends TypeWithID = any>(options: Options): Promise<Result & { user: T}> {
|
||||
const {
|
||||
collection: collectionSlug,
|
||||
req = {},
|
||||
|
||||
@@ -8,6 +8,7 @@ import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import { Field, fieldHasSubFields, fieldAffectsData } from '../../fields/config/types';
|
||||
import { User } from '../types';
|
||||
import { Collection } from '../../collections/config/types';
|
||||
import { Payload } from '../..';
|
||||
|
||||
export type Result = {
|
||||
user?: User,
|
||||
@@ -28,7 +29,7 @@ export type Arguments = {
|
||||
showHiddenFields?: boolean
|
||||
}
|
||||
|
||||
async function login(incomingArgs: Arguments): Promise<Result> {
|
||||
async function login(this: Payload, incomingArgs: Arguments): Promise<Result> {
|
||||
const { config, operations, secret } = this;
|
||||
|
||||
let args = incomingArgs;
|
||||
@@ -176,7 +177,7 @@ async function login(incomingArgs: Arguments): Promise<Result> {
|
||||
id: user.id,
|
||||
data: user,
|
||||
hook: 'afterRead',
|
||||
operation: 'login',
|
||||
operation: 'read',
|
||||
overrideAccess,
|
||||
flattenLocales: true,
|
||||
showHiddenFields,
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { Document } from '../../types';
|
||||
import { Forbidden } from '../../errors';
|
||||
import { Payload } from '../..';
|
||||
import { Collection } from '../../collections/config/types';
|
||||
import { PayloadRequest } from '../../express/types';
|
||||
import { Collection, TypeWithID } from '../../collections/config/types';
|
||||
|
||||
export type Arguments = {
|
||||
collection: Collection
|
||||
data: {
|
||||
email: string
|
||||
password: string
|
||||
}
|
||||
req: PayloadRequest
|
||||
}
|
||||
|
||||
export type Result = {
|
||||
@@ -23,6 +29,11 @@ async function registerFirstUser(this: Payload, args: Arguments): Promise<Result
|
||||
},
|
||||
},
|
||||
},
|
||||
req: {
|
||||
payload,
|
||||
},
|
||||
req,
|
||||
data,
|
||||
} = args;
|
||||
|
||||
const count = await Model.countDocuments({});
|
||||
@@ -33,14 +44,16 @@ async function registerFirstUser(this: Payload, args: Arguments): Promise<Result
|
||||
// Register first user
|
||||
// /////////////////////////////////////
|
||||
|
||||
let result = await this.operations.collections.create({
|
||||
...args,
|
||||
const result = await payload.create<TypeWithID>({
|
||||
req,
|
||||
collection: slug,
|
||||
data,
|
||||
overrideAccess: true,
|
||||
});
|
||||
|
||||
// auto-verify (if applicable)
|
||||
if (verify) {
|
||||
await this.update({
|
||||
await payload.update({
|
||||
id: result.id,
|
||||
collection: slug,
|
||||
data: {
|
||||
@@ -53,18 +66,19 @@ async function registerFirstUser(this: Payload, args: Arguments): Promise<Result
|
||||
// Log in new user
|
||||
// /////////////////////////////////////
|
||||
|
||||
const { token } = await this.operations.collections.auth.login({
|
||||
const { token } = await payload.login({
|
||||
...args,
|
||||
collection: slug,
|
||||
});
|
||||
|
||||
result = {
|
||||
const resultToReturn = {
|
||||
...result,
|
||||
token,
|
||||
};
|
||||
|
||||
return {
|
||||
message: 'Registered and logged in successfully. Welcome!',
|
||||
user: result,
|
||||
user: resultToReturn,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user