Skip to content
Snippets Groups Projects
Commit c4b2bc10 authored by D'Abrantes, Felipe (UG - Comp Sci & Elec Eng)'s avatar D'Abrantes, Felipe (UG - Comp Sci & Elec Eng)
Browse files

Merge branch 'user-enpoints' into 'main'

Add additional GET endpoints to user service

See merge request !16
parents ab6091f0 73bf713a
No related branches found
No related tags found
1 merge request!16Add additional GET endpoints to user service
......@@ -70,7 +70,17 @@ export async function register(req,res){
// return save result as a response
user.save()
.then(result => res.status(201).send({ msg: "User Registered Successfully"}))
.then(async (result) => {
const user = await UserModel.findOne({username: username})
// create jwt token
const token = jwt.sign({
userId: user._id,
username : user.username
}, ENV.JWT_SECRET , { expiresIn : "24h"});
res.status(201).send({ msg: "User Registered Successfully", username: user.username, token })
})
.catch(error => res.status(500).send({error}))
}).catch(error => {
......@@ -195,3 +205,49 @@ export async function updateUser(req,res){
}
}
/**
* POST /userlist
* This DOES NOT return emails and passwords
* @param {*} req
* @param {*} res
* @returns
*
* body: {userIdList: []}
*/
export const GetUserList = async (req,res) => {
try {
const {userIdList} = req.body;
const users = await UserModel.find({ '_id': { $in: userIdList } }, {password: 0, email: 0});
return res.status(201).send({ userList: users});
} catch(error){
return res.status(401).send({ error });
}
}
/**
* GET /search
* This endpoint OMITS the password and email fields
* @param {*} req
* @param {*} res
* @returns
*
* query: {query: string}
*/
export const Search = async (req,res) => {
try {
const {searchQuery} = req.query;
console.log(searchQuery)
if(searchQuery === undefined || searchQuery.length === 0){
throw new Error("Please provide a valid query!")
}
const usersWithMatchingUsername = await UserModel.find({"username": {"$regex": `^${searchQuery}`}}, {password: 0, email: 0})
const usersWithMatchingName = await UserModel.find({$or: [{"firstName": {"$regex": `^${searchQuery}`,"$options": 'i'}}, {"lastName": {"$regex": `^${searchQuery}`,"$options": 'i'}}]}, {password: 0, email: 0})
return res.status(201).send({ usersByUsername: usersWithMatchingUsername, usersByName: usersWithMatchingName});
} catch(error){
return res.status(401).send({ error: error.message });
}
}
......@@ -8,9 +8,11 @@ const router = Router();
/** POST Methods */
router.route('/register').post(controller.register); // Register
router.route('/login').post(controller.verifyUser, controller.login); // Login
router.route('/userlist').post(controller.GetUserList) // Get user list
/** GET Methods */
router.route('/user/:username').get(controller.getUser); // GetUser
router.route("/search").get(controller.Search) // Search
/** PUT Methods */
......
......@@ -29,12 +29,14 @@ const Register = () => {
const response = await fetch(endpoint, options)
const result = await response.json()
console.log(result.error)
if(result.error !== undefined){
setError(result.error.error)
} else {
Router.push("/feed")
} else {
if(result.token !== undefined && result.username !== undefined){
sessionStorage.setItem('username', result.username)
sessionStorage.setItem('token', result.token)
Router.push("/feed")
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment