-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added unauthenticated user model (#9)
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
Showing
6 changed files
with
298 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.