Skip to content

Commit 97caefc

Browse files
author
DylanBulmer
committed
simplify user model; adjust email auth output
1 parent ab72965 commit 97caefc

5 files changed

Lines changed: 39 additions & 72 deletions

File tree

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.10",
3+
"version": "1.0.11",
44
"description": "An open-sourced customizable annotation tool",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",

src/models/Profile.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import {
33
AccessibleModel,
44
accessibleRecordsPlugin,
55
} from "@casl/mongoose";
6-
import { Schema, model, Document } from "mongoose";
6+
import { Schema, model, Document, ObjectId } from "mongoose";
77

8-
export interface IProfile extends Document {
8+
export interface Profile {
99
avatarUrl?: string;
1010
username: string;
1111
user: Schema.Types.ObjectId;
1212
}
1313

14-
const ProfileSchema = new Schema<IProfile>(
14+
type IProfileSchema = IProfile & Document
15+
export type IProfile = Profile & { _id: ObjectId }
16+
17+
const ProfileSchema = new Schema<Profile>(
1518
{
1619
username: { type: String, required: true, unique: true },
1720
avatarUrl: { type: String },
@@ -29,7 +32,7 @@ const ProfileSchema = new Schema<IProfile>(
2932
// exports User model.
3033
ProfileSchema.plugin(accessibleFieldsPlugin);
3134
ProfileSchema.plugin(accessibleRecordsPlugin);
32-
const Profile = model<IProfile, AccessibleModel<IProfile>>(
35+
const Profile = model<IProfileSchema, AccessibleModel<IProfileSchema>>(
3336
"Profile",
3437
ProfileSchema,
3538
);

src/models/User.ts

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,34 @@ import {
44
AccessibleModel,
55
accessibleRecordsPlugin,
66
} from "@casl/mongoose";
7-
import { Schema, model, Document } from "mongoose";
7+
import { Schema, model, Document, ObjectId } from "mongoose";
88

99
type Role = "admin" | "researcher" | "annotator";
1010
export type UserRoleType = `codr:${Role}`;
1111

12-
interface IUserProvider {
13-
photo?: string;
14-
phone?: string;
15-
email: string;
16-
uid: string;
17-
}
18-
19-
export interface IUserName {
20-
first: string;
21-
last: string;
22-
preferred: string;
23-
}
24-
25-
export interface IUser extends Document {
26-
name?: IUserName;
12+
interface User {
13+
name?: {
14+
first: string;
15+
last: string;
16+
preferred?: string;
17+
};
2718
email: string;
2819
accessToken: string;
2920
refreshToken: string;
30-
providers?: IUserProvider;
31-
isAdmin: boolean;
3221
role: UserRoleType;
33-
disabled: boolean;
34-
anonymous: boolean;
22+
flags: {
23+
isDisabled: boolean;
24+
isAnonymous: boolean;
25+
};
3526
}
3627

37-
const UserProvider = new Schema<IUserProvider>({
38-
photo: { type: String },
39-
phone: { type: String },
40-
email: { type: String, required: true },
41-
uid: {
42-
type: String,
43-
required: [true, "Provider's unique identifier is required"],
44-
},
45-
});
46-
47-
const UserName = new Schema<IUserName>({
48-
first: { type: String },
49-
last: { type: String },
50-
preferred: { type: String },
51-
});
28+
type IUserSchema = User & Document;
29+
export type IUser = User & { _id: ObjectId }
5230

53-
const UserSchema = new Schema<IUser>(
31+
const UserSchema = new Schema<User>(
5432
{
5533
name: {
56-
type: UserName,
34+
type: Object,
5735
},
5836
email: {
5937
type: String,
@@ -69,33 +47,23 @@ const UserSchema = new Schema<IUser>(
6947
},
7048
accessToken: { type: String },
7149
refreshToken: { type: String },
72-
providers: {
73-
type: [UserProvider],
74-
},
7550
role: {
7651
type: String,
7752
required: [true, "Please specify the user's role."],
7853
},
79-
disabled: {
80-
type: Boolean,
81-
default: false,
82-
},
83-
anonymous: {
84-
type: Boolean,
85-
default: false,
54+
flags: {
55+
type: Object,
56+
required: true,
57+
default: { isAnonymous: false, isDisabled: false },
8658
},
8759
},
8860
{
8961
timestamps: true,
9062
},
9163
);
9264

93-
UserSchema.virtual("fullName").get(function get() {
94-
return this.name?.preferred + " " + this.name?.last;
95-
});
96-
9765
// exports User model.
9866
UserSchema.plugin(accessibleFieldsPlugin);
9967
UserSchema.plugin(accessibleRecordsPlugin);
100-
const User = model<IUser, AccessibleModel<IUser>>("User", UserSchema);
68+
const User = model<IUserSchema, AccessibleModel<IUserSchema>>("User", UserSchema);
10169
export default User;

src/services/admin.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
*/
44

55
import { MailTemplate, Response, Error } from "../classes/index.js";
6-
import { Types } from "mongoose";
76
import { UserToken } from "../classes/JWT.js";
8-
import User, { IUser, IUserName, UserRoleType } from "../models/User.js";
7+
import User, { IUser, UserRoleType } from "../models/User.js";
98
import App from "./app.js";
109
import Mail from "./mail/index.js";
1110

@@ -24,7 +23,7 @@ class Administration {
2423

2524
async addUser(
2625
user: UserToken,
27-
newUser: { email: string; role: UserRoleType; name?: IUserName },
26+
newUser: { email: string; role: UserRoleType; name?: IUser["name"], isAnonymous: boolean; },
2827
) {
2928
if (this.app.mongoIsConnected) {
3029
// check if user is admin; will throw an error if not.
@@ -83,9 +82,9 @@ class Administration {
8382
*/
8483
async addUsers(
8584
user: UserToken,
86-
newUsers: { email: string; role: UserRoleType }[],
85+
newUsers: { email: string; role: UserRoleType, isAnonymous: boolean }[],
8786
) {
88-
const users: (IUser & { _id: Types.ObjectId })[] = [];
87+
const users: (IUser)[] = [];
8988
const errors: Error<
9089
{ email: string; role: UserRoleType } | { connectionStatus: string }
9190
>[] = [];

src/services/auth.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Email from "../classes/Email.js";
99
import SigninTemplate from "../classes/MailTemplate/Signin.js";
1010
import User, { IUser } from "../models/User.js";
1111
import Response from "../classes/Response.js";
12-
import { generateToken, UserToken, verifyToken } from "../classes/JWT.js";
12+
import * as JWT from "../classes/JWT.js";
1313
import Error from "../classes/Error.js";
1414
import AccessToken from "../classes/AccessToken.js";
1515
import { decrypt, encrypt } from "../utils/AccessToken";
@@ -32,7 +32,7 @@ class Authentication {
3232

3333
async signinWithEmail(
3434
accessCode: string,
35-
): Promise<Response<undefined | { token: string }>> {
35+
): Promise<Response<undefined | { user: IUser }>> {
3636
const { email, token } = decrypt<IAccessCode>(accessCode);
3737
if (!email) {
3838
throw new Error({
@@ -113,13 +113,10 @@ class Authentication {
113113
}
114114

115115
try {
116-
// generate JWT token
117-
const token = generateToken({ ...user.toJSON(), ...update } as IUser);
118-
119116
// send response
120-
return new Response<{ token: string }>({
117+
return new Response<{ user: IUser }>({
121118
message: `Login successful.`,
122-
details: { token },
119+
details: { user: {...user.toObject(), ...update} },
123120
});
124121
} catch (e: any) {
125122
throw new Error({
@@ -145,10 +142,10 @@ class Authentication {
145142
}
146143

147144
updateJWT(oldJWT: string, payload: IUser) {
148-
verifyToken(oldJWT);
145+
JWT.verifyToken(oldJWT);
149146

150-
const jwt = generateToken(payload);
151-
return { jwt, user: decode(jwt) as UserToken };
147+
const jwt = JWT.generateToken(payload);
148+
return { jwt, user: decode(jwt) as JWT.UserToken };
152149
}
153150
}
154151

0 commit comments

Comments
 (0)