diff --git a/src/entities/User.ts b/src/entities/User.ts index e7c2ee5..a2f0ba4 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -6,7 +6,7 @@ export interface IUser extends Document { username: string; pwdHash: string; email: string; - trackers: ITracker | ITracker[]; + trackers: ITracker[]; role: 'User' | 'Admin'; } @@ -28,10 +28,10 @@ const userSchema: Schema = new Schema( type: String, required: [true, 'Email is required'], }, - trackers: { + trackers: [{ type: Schema.Types.ObjectId, ref: 'Tracker', - }, + }], role: { type: String, required: [true, 'Role is required'] @@ -43,36 +43,4 @@ const userSchema: Schema = new Schema( } ); -export default model('User', userSchema); - -// export class User implements IUser { - -// public id: number; -// public name: string; -// public email: string; -// public role: UserRoles; -// public pwdHash: string; - - -// constructor( -// nameOrUser?: string | IUser, -// email?: string, -// role?: UserRoles, -// pwdHash?: string, -// id?: number, -// ) { -// if (typeof nameOrUser === 'string' || typeof nameOrUser === 'undefined') { -// this.name = nameOrUser || ''; -// this.email = email || ''; -// this.role = role || UserRoles.Standard; -// this.pwdHash = pwdHash || ''; -// this.id = id || -1; -// } else { -// this.name = nameOrUser.name; -// this.email = nameOrUser.email; -// this.role = nameOrUser.role; -// this.pwdHash = nameOrUser.pwdHash; -// this.id = nameOrUser.id; -// } -// } -// } +export default model('User', userSchema); \ No newline at end of file diff --git a/src/routes/Auth.ts b/src/routes/Auth.ts index 3b4ffc7..941c72a 100644 --- a/src/routes/Auth.ts +++ b/src/routes/Auth.ts @@ -31,14 +31,18 @@ router.post('/login', async (req: Request, res: Response) => { // Fetch user let user: IUser | null; try { - user = await User.findOne({ username: username }); + user = await User + .findOne({ username: username }) + .populate([{path: 'trackers', model: 'Tracker'}]) + .exec(); if (user == null) { return res.status(UNAUTHORIZED).json({ error: loginFailedErr }); } } catch (err) { return res.status(500).json({ message: err.message }); } - + // user = await user.execPopulate([{path: 'trackers', model: 'Tracker'}]); + console.log('user', user); // Check password const pwdPassed = await bcrypt.compare(password, user.pwdHash); if (!pwdPassed) { @@ -60,6 +64,7 @@ router.post('/login', async (req: Request, res: Response) => { id: user._id, username: user.username, }, + trackers: user.trackers }); }); diff --git a/src/routes/Users.ts b/src/routes/Users.ts index 28e1718..658eeeb 100644 --- a/src/routes/Users.ts +++ b/src/routes/Users.ts @@ -26,17 +26,26 @@ router.get('/all', adminMW, async (req: Request, res: Response) => { }); /****************************************************************************** - * Get One User - "GET /api/users/:id" + * Get A User - "GET /api/users/:id" ******************************************************************************/ router.get('/:id', async (req: Request, res: Response) => { - let user: IUser | null; + // let user: IUser | null; try { - user = await User.findById(req.params.id); - if (user == null) { - return res.status(404).json({ message: 'Cannot find user' }); - } - return res.status(OK).json({ user }); + User + .findById(req.params.id) + .populate([{path: 'trackers', model: 'Tracker'}]) + .exec(function (err, user) { + if (user == null) { + return res.status(404).json({ message: 'Cannot find user' }); + } + return res.status(OK).json({ user }); + }); + // user = await User.findById(req.params.id); + // if (user == null) { + // return res.status(404).json({ message: 'Cannot find user' }); + // } + // return res.status(OK).json({ user }); } catch (err) { return res.status(500).json({ message: err.message }); }