Skip to content

Commit 66e2379

Browse files
committed
current user route created and tested
1 parent a3d4c83 commit 66e2379

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

examples_new/microservices/auth/src/__test__/current-user.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ it('responds with the correct user if cookie exists and is valid', async () => {
3030
// 2) responds with null if no valid cookie
3131
// -- send request to /current-user
3232
// -- expect null as resposne (response.body.currentUser)
33-
it('responds with the correct user if cookie exists and is valid', async () => {
33+
it('responds with currentUser of null when no cookie included in request', async () => {
3434
const response = await request(app)
3535
.get('/api/auth/current-user')
3636
.send({

examples_new/microservices/auth/src/controllers/auth-controller.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BadRequestError } from '@chronosrx/common';
1+
import { BadRequestError, CurrentUserRequest } from '@chronosrx/common';
22
import { Request, Response } from 'express';
33
import { User } from '../models/user';
44
import { attachCookie } from '../util/attachCookie';
@@ -88,6 +88,16 @@ export const logout = async (req: Request, res: Response) => {
8888
res.status(200).send({ message: 'success' });
8989
};
9090

91-
export const getCurrentUser = async (req: Request, res: Response) => {
92-
res.send({});
91+
export const getCurrentUser = async (req: CurrentUserRequest, res: Response) => {
92+
// check request object for currentUser property
93+
if (!req.currentUser) {
94+
// if it doesn't exist send back status 200 with object with currentUser property set to null
95+
res.status(200).send({ currentUser: null });
96+
}
97+
98+
// if it does exist - use req.currentUser to find user in database by id
99+
const user = await User.findById(req.currentUser);
100+
// send back 200 with object with property currentUser set to the user from the database
101+
res.status(200).send({currentUser: user});
102+
93103
};

0 commit comments

Comments
 (0)