Skip to content

Commit 8454ec2

Browse files
committed
Add Admins collection and update user role checks in Categories and Tasks collections
1 parent 7bce248 commit 8454ec2

File tree

5 files changed

+100
-18
lines changed

5 files changed

+100
-18
lines changed

src/collections/Admins.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { CollectionConfig } from 'payload'
2+
3+
export const Admins: CollectionConfig = {
4+
slug: 'admins',
5+
admin: {
6+
useAsTitle: 'email',
7+
},
8+
auth: true,
9+
fields: [],
10+
}

src/collections/Categories.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ const Categories: CollectionConfig = {
1111
return false
1212
}
1313

14-
if (req.user.role === 'user') {
14+
if (req.user.collection === 'users' && req.user.role === 'user') {
1515
return {
1616
user: {
1717
equals: req.user.id,
1818
},
1919
}
2020
}
2121

22-
if (req.user.role === 'admin') {
22+
if (req.user.collection === 'admins') {
2323
return true
2424
}
2525

src/collections/Tasks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const Tasks: CollectionConfig = {
1212
return false
1313
}
1414

15-
if (req.user.role === 'user') {
15+
if (req.user.collection === 'users' && req.user.role === 'user') {
1616
return {
1717
user: {
1818
equals: req.user.id,
1919
},
2020
}
2121
}
2222

23-
if (req.user.role === 'admin') {
23+
if (req.user.collection === 'admins') {
2424
return true
2525
}
2626

@@ -35,7 +35,7 @@ const Tasks: CollectionConfig = {
3535
throw new Error('You must be logged in to view tasks')
3636
}
3737

38-
if (req.user.role === 'user') {
38+
if (req.user.collection === 'users' && req.user.role === 'user') {
3939
if (category) {
4040
query.where = {
4141
...query.where,

src/payload-types.ts

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
export interface Config {
1010
auth: {
11+
admins: AdminAuthOperations;
1112
users: UserAuthOperations;
1213
};
1314
collections: {
15+
admins: Admin;
1416
users: User;
1517
media: Media;
1618
categories: Category;
@@ -21,6 +23,7 @@ export interface Config {
2123
};
2224
collectionsJoins: {};
2325
collectionsSelect: {
26+
admins: AdminsSelect<false> | AdminsSelect<true>;
2427
users: UsersSelect<false> | UsersSelect<true>;
2528
media: MediaSelect<false> | MediaSelect<true>;
2629
categories: CategoriesSelect<false> | CategoriesSelect<true>;
@@ -35,14 +38,36 @@ export interface Config {
3538
globals: {};
3639
globalsSelect: {};
3740
locale: null;
38-
user: User & {
39-
collection: 'users';
40-
};
41+
user:
42+
| (Admin & {
43+
collection: 'admins';
44+
})
45+
| (User & {
46+
collection: 'users';
47+
});
4148
jobs: {
4249
tasks: unknown;
4350
workflows: unknown;
4451
};
4552
}
53+
export interface AdminAuthOperations {
54+
forgotPassword: {
55+
email: string;
56+
password: string;
57+
};
58+
login: {
59+
email: string;
60+
password: string;
61+
};
62+
registerFirstUser: {
63+
email: string;
64+
password: string;
65+
};
66+
unlock: {
67+
email: string;
68+
password: string;
69+
};
70+
}
4671
export interface UserAuthOperations {
4772
forgotPassword: {
4873
email: string;
@@ -61,6 +86,23 @@ export interface UserAuthOperations {
6186
password: string;
6287
};
6388
}
89+
/**
90+
* This interface was referenced by `Config`'s JSON-Schema
91+
* via the `definition` "admins".
92+
*/
93+
export interface Admin {
94+
id: string;
95+
updatedAt: string;
96+
createdAt: string;
97+
email: string;
98+
resetPasswordToken?: string | null;
99+
resetPasswordExpiration?: string | null;
100+
salt?: string | null;
101+
hash?: string | null;
102+
loginAttempts?: number | null;
103+
lockUntil?: string | null;
104+
password?: string | null;
105+
}
64106
/**
65107
* This interface was referenced by `Config`'s JSON-Schema
66108
* via the `definition` "users".
@@ -157,6 +199,10 @@ export interface Task {
157199
export interface PayloadLockedDocument {
158200
id: string;
159201
document?:
202+
| ({
203+
relationTo: 'admins';
204+
value: string | Admin;
205+
} | null)
160206
| ({
161207
relationTo: 'users';
162208
value: string | User;
@@ -174,10 +220,15 @@ export interface PayloadLockedDocument {
174220
value: string | Task;
175221
} | null);
176222
globalSlug?: string | null;
177-
user: {
178-
relationTo: 'users';
179-
value: string | User;
180-
};
223+
user:
224+
| {
225+
relationTo: 'admins';
226+
value: string | Admin;
227+
}
228+
| {
229+
relationTo: 'users';
230+
value: string | User;
231+
};
181232
updatedAt: string;
182233
createdAt: string;
183234
}
@@ -187,10 +238,15 @@ export interface PayloadLockedDocument {
187238
*/
188239
export interface PayloadPreference {
189240
id: string;
190-
user: {
191-
relationTo: 'users';
192-
value: string | User;
193-
};
241+
user:
242+
| {
243+
relationTo: 'admins';
244+
value: string | Admin;
245+
}
246+
| {
247+
relationTo: 'users';
248+
value: string | User;
249+
};
194250
key?: string | null;
195251
value?:
196252
| {
@@ -215,6 +271,21 @@ export interface PayloadMigration {
215271
updatedAt: string;
216272
createdAt: string;
217273
}
274+
/**
275+
* This interface was referenced by `Config`'s JSON-Schema
276+
* via the `definition` "admins_select".
277+
*/
278+
export interface AdminsSelect<T extends boolean = true> {
279+
updatedAt?: T;
280+
createdAt?: T;
281+
email?: T;
282+
resetPasswordToken?: T;
283+
resetPasswordExpiration?: T;
284+
salt?: T;
285+
hash?: T;
286+
loginAttempts?: T;
287+
lockUntil?: T;
288+
}
218289
/**
219290
* This interface was referenced by `Config`'s JSON-Schema
220291
* via the `definition` "users_select".

src/payload.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { buildConfig } from 'payload'
88
import { fileURLToPath } from 'url'
99
import sharp from 'sharp'
1010

11+
import { Admins } from './collections/Admins'
1112
import { Users } from './collections/Users'
1213
import { Media } from './collections/Media'
1314
import Categories from './collections/Categories'
@@ -23,12 +24,12 @@ export default buildConfig({
2324
apiKey: process.env.RESEND_API_KEY ?? '',
2425
}),
2526
admin: {
26-
user: Users.slug,
27+
user: Admins.slug,
2728
importMap: {
2829
baseDir: path.resolve(dirname),
2930
},
3031
},
31-
collections: [Users, Media, Categories, Tasks],
32+
collections: [Admins, Users, Media, Categories, Tasks],
3233
editor: lexicalEditor(),
3334
secret: process.env.PAYLOAD_SECRET || '',
3435
typescript: {

0 commit comments

Comments
 (0)