Skip to content

Commit

Permalink
feat: Added unauthenticated user model (#9)
Browse files Browse the repository at this point in the history
Changes
1) Have made a shell for the backend of the project with routes, controllers and models
2) Have added a user model, route and controller. This only adds a user for now. Still it's not authenticated, and meant as a test

Additional content
1) Next we will authenticate the user and add several endpoints for signing up, logging in and loggin out
  • Loading branch information
kiblindh committed Apr 13, 2024
1 parent 382b628 commit 8224737
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 17 deletions.
20 changes: 20 additions & 0 deletions recruitment-web/backend/src/controllers/userController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { userModel } from "../models/userModel";
import { Response, Request } from "express";

async function signupUser(req: Request, res: Response) {
const { username, password, email, first_name, last_name } = req.body;
try {
const user = await userModel.create({
username,
password,
email,
first_name,
last_name,
});
return res.status(200).json(user);
} catch (error) {
res.status(400).json({ error: "Could not create user" });
}
}

export { signupUser };
28 changes: 28 additions & 0 deletions recruitment-web/backend/src/models/userModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import mongoose from "mongoose"

const Schema = mongoose.Schema;

interface IUser {
username: String;
password: String;
email: String;
first_name: String;
last_name: String;
}

const userSchema = new Schema<IUser>(
{
username: {type: String, required: true,unique: true,},
password: {type: String,required: true,},
email: {type: String, required: true,unique: true,},
first_name: { type: String,required: true,},
last_name: {type: String,required: true,},
},
{ timestamps: true }
);

const userModel = mongoose.model<IUser>("User", userSchema);

export{userModel}


11 changes: 11 additions & 0 deletions recruitment-web/backend/src/routes/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import express from "express"
import {signupUser} from "../controllers/userController"

const userRouter = express.Router();


userRouter.post('/signup', signupUser);


export{userRouter}
34 changes: 18 additions & 16 deletions recruitment-web/backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express, { Express, Request, Response } from 'express';
import cors from 'cors';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import express, { Express, Request, Response } from "express";
import cors from "cors";
import mongoose from "mongoose";
import dotenv from "dotenv";
import { userRouter } from "./routes/user";

dotenv.config();

Expand All @@ -11,24 +12,25 @@ app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const uri: string =
process.env.MONGODB_URI || 'mongodb://localhost:27017';
app.use("/user", userRouter);

const uri: string = process.env.MONGODB_URI || "mongodb://localhost:27017";

(async () => {
try {
await mongoose.connect(uri);
console.log('Connected to the database');
} catch {
console.log('Error connecting to the database');
}
try {
await mongoose.connect(uri);
console.log("Connected to the database");
} catch {
console.log("Error connecting to the database");
}
})();

app.get('/health', (_req: Request, res: Response) => {
res.status(200).send('Server is running');
app.get("/health", (_req: Request, res: Response) => {
res.status(200).send("Server is running");
});

const PORT: string | number = process.env.PORT || 3000;

app.listen(PORT, () => {
console.log(`Server is running on PORT: ${PORT}`);
});
console.log(`Server is running on PORT: ${PORT}`);
});
217 changes: 217 additions & 0 deletions recruitment-web/package-lock.json

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

Loading

0 comments on commit 8224737

Please sign in to comment.