Skip to content

Commit 07e9721

Browse files
author
DylanBulmer
committed
update types; disallow disabled accounts to login.
1 parent 7944ef9 commit 07e9721

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codrjs/core",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"description": "An open-sourced customizable annotation tool",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",

src/models/User.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ interface User {
2828
isDisabled: boolean;
2929
isAnonymous: boolean;
3030
};
31+
createdAt: string;
32+
updatedAt: string;
3133
}
3234

3335
export type IUserSchema = User & Document;
34-
export type IUser = User & { _id: ObjectId }
36+
export type IUser = User & {
37+
_id: ObjectId;
38+
};
3539

3640
const UserSchema = new Schema<User>(
3741
{
@@ -70,5 +74,8 @@ const UserSchema = new Schema<User>(
7074
// exports User model.
7175
UserSchema.plugin(accessibleFieldsPlugin);
7276
UserSchema.plugin(accessibleRecordsPlugin);
73-
const User = model<IUserSchema, AccessibleModel<IUserSchema>>("User", UserSchema);
77+
const User = model<IUserSchema, AccessibleModel<IUserSchema>>(
78+
"User",
79+
UserSchema,
80+
);
7481
export default User;

src/services/admin.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Administration {
2828
email: string;
2929
role: UserRoleType;
3030
name?: IUser["name"];
31-
isAnonymous: boolean;
31+
flags?: IUser["flags"];
3232
},
3333
) {
3434
if (this.app.mongoIsConnected) {
@@ -88,7 +88,12 @@ class Administration {
8888
*/
8989
async addUsers(
9090
user: UserToken,
91-
newUsers: { email: string; role: UserRoleType; isAnonymous: boolean }[],
91+
newUsers: {
92+
email: string;
93+
role: UserRoleType;
94+
name?: IUser["name"];
95+
flags?: IUser["flags"];
96+
}[],
9297
) {
9398
const users: IUser[] = [];
9499
const errors: Error<

src/services/auth.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,41 @@ class Authentication {
5555
"It appears you do not have an account using this email, please contact your Codr admin to gain access.",
5656
});
5757
} else if (!token) {
58-
try {
59-
// init access token
60-
const uuid = uuidv4();
61-
const accessToken = new AccessToken(uuid);
62-
await user.updateOne({
63-
accessToken: accessToken.encode(),
64-
});
65-
66-
// send email with access code/token
67-
const link =
68-
`${process.env.HOST}${process.env.API_PATH}` +
69-
"/auth/email/verify?token=" +
70-
encrypt(JSON.stringify({ email: email, token: uuid }));
71-
const template = new SigninTemplate();
72-
await Mail.send(await template.html({ link }), {
73-
...template.config,
74-
to: email,
75-
});
76-
return new Response({
77-
message: "An email has been sent to your inbox.",
78-
});
79-
} catch (e: any) {
58+
if (user.flags.isDisabled) {
8059
throw new Error({
8160
status: 500,
82-
message: e?.message || "An unknown error occured",
83-
details: e,
61+
message:
62+
"It appears that your account has been disabled or deleted. Please contact your administrator if you feel like this is a mistake.",
8463
});
64+
} else {
65+
try {
66+
// init access token
67+
const uuid = uuidv4();
68+
const accessToken = new AccessToken(uuid);
69+
await user.updateOne({
70+
accessToken: accessToken.encode(),
71+
});
72+
73+
// send email with access code/token
74+
const link =
75+
`${process.env.HOST}${process.env.API_PATH}` +
76+
"/auth/email/verify?token=" +
77+
encrypt(JSON.stringify({ email: email, token: uuid }));
78+
const template = new SigninTemplate();
79+
await Mail.send(await template.html({ link }), {
80+
...template.config,
81+
to: email,
82+
});
83+
return new Response({
84+
message: "An email has been sent to your inbox.",
85+
});
86+
} catch (e: any) {
87+
throw new Error({
88+
status: 500,
89+
message: e?.message || "An unknown error occured",
90+
details: e,
91+
});
92+
}
8593
}
8694
} else if (user.accessToken) {
8795
// decrypt the stored access code
@@ -116,7 +124,7 @@ class Authentication {
116124
// send response
117125
return new Response<{ user: IUser }>({
118126
message: `Login successful.`,
119-
details: { user: {...user.toObject(), ...update} },
127+
details: { user: { ...user.toObject(), ...update } },
120128
});
121129
} catch (e: any) {
122130
throw new Error({

0 commit comments

Comments
 (0)