Skip to content

Commit

Permalink
Merge pull request #29 from CMU-17-356/login
Browse files Browse the repository at this point in the history
added backend adapter functions and user routes for user login
  • Loading branch information
gdevanshi authored May 3, 2023
2 parents 9a731cc + da1b4c3 commit 9a64f66
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 6 deletions.
3 changes: 2 additions & 1 deletion backend/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from "dotenv";
import express from "express";
import mongoose from 'mongoose';
import { TaskRoutes} from './src/routes/index';
import { TaskRoutes, UserRoutes } from './src/routes/index';
// import cors from 'cors';

// Allow the use of environment variables
Expand All @@ -26,6 +26,7 @@ app.use(function(req, res, next) {

// Create appropriate API endpoints
app.use("/api/tasks", TaskRoutes)
app.use("/api/users", UserRoutes)

var server = app.listen(port, () => {
console.log('Server is running on port ' + port + '!');
Expand Down
8 changes: 5 additions & 3 deletions backend/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import mongoose from 'mongoose'
const { Schema } = mongoose

export interface IUser {
name: string
email_id: string
first_name: string
last_name: string
username: string
password: string
}

const UserSchema = new Schema({
Expand All @@ -16,7 +18,7 @@ const UserSchema = new Schema({
required: true
},
username: {
type: String, // Positive Int
type: String,
required: true
},
password: {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file used for importing all routes at once

export { TaskRoutes } from './task';

export { UserRoutes } from './user';
50 changes: 50 additions & 0 deletions backend/src/routes/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import express from "express";
import { User } from '../models/user';

const router = express.Router();

// GET all Users
router.get("/", (req, res) => {
User.find({}, (err: any, result: any) => {
if (err) {
res.json(err);
} else {
res.json(result);
}
});
});

// GET a single User
router.get("/:username", (req, res) => {
User.find({ username: req.params.username}, (err: any, result: any) => {
if (err) {
res.json(err);
} else {
res.json(result);
}
});
});

// CREATE a new User
router.post("/", async (req, res) => {
console.log('post request')
const user = req.body;
const newUser = new User(user);
await newUser.save();

res.json(newUser);
});


// DELETE a User
router.delete("/:username", async (req, res) => {
User.findOneAndDelete({ username: req.params.username }, (err: any, result: any) => {
if (err) {
res.json(err);
} else {
res.json(result);
}
});
});

export const UserRoutes = router
2 changes: 0 additions & 2 deletions backend/tests/photo.test.ts

This file was deleted.

12 changes: 12 additions & 0 deletions backend/tests/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import { type ITask } from '../src/models/task'
import express, { Request, Response } from "express";
import { expect } from '@jest/globals'

_id: string
name: string
description: string
label: string
priority?: string
dueDate?: Date | null
recurring?: string
day?: string | null
completed: {date: Date, photo: string}[]
createdAt: Date
user: string

describe('task', () => {
it('should create a new Task with the appropriate fields', () => {
const task: ITask = {
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/backend-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,24 @@ export async function deleteTask(id: string) {
await instance.delete("tasks/" + id)
}

export async function addUser(user) {
// Check for duplicates
const response = await instance.get("users/" + user.username);
if (response.data.length == 0) {
await instance.post("users/", user)
} else {
throw new Error("Username already exists.")
}
}

export async function authenticateUser(username: string, password: string) {
const response = await instance.get("users/" + username)
if (response.data.length == 0) {
throw new Error("Username doesn't exist.")
}
const user = response.data
if (user.password === password) {
return true
}
return false
}
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"@jest/globals": "^29.5.0",
"axios": "^1.4.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"mongoose": "^7.1.0",
Expand Down

0 comments on commit 9a64f66

Please sign in to comment.