Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/node_modules


.env
71 changes: 71 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const express = require('express');
const { StatusCodes } = require('http-status-codes');
const app = express();
require('dotenv').config()
const port = process.env.PORT || 3004
const cors = require('cors')
const connectionDB = require('./connection/connection')
const authroute = require('./routes/auth')




// other middlewares



app.use(cors())
app.use(express.json())
app.use('/api/auth', authroute)






// error middleware


app.use((err, req, res, next)=>{

const errorstatus = err.status || StatusCodes.INTERNAL_SERVER_ERROR
const errormessage = err.message || 'something went wrong'
return res.status(errorstatus).json({

success:false,
status:errorstatus,
message:errormessage,
stack:err.stack
})



})




// connection to DB


const DB = async()=>{

try{

await connectionDB(process.env.mongo_url)

app.listen({port}, ()=>{
console.log(`Server is running on port ${port}`)
})
}

catch(error){


console.log(error)
}
}

DB()


12 changes: 12 additions & 0 deletions backend/connection/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require('mongoose')

mongoose.set('strictQuery', false);

const connectionDB = (url)=>{

return mongoose.connect(url)

}


module.exports = connectionDB
83 changes: 83 additions & 0 deletions backend/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const touruser = require('../models/User')
const error = require('../utils/error')
const { StatusCodes } = require('http-status-codes');
// const jwt = require('jsonwebtoken')



const register = async(req, res, next)=>{

try{
const user = await touruser.create({...req.body})


res.status(201).json({msg: 'User has been created successfully'})
}

catch(err){

next(err)
}
}


const login = async(req, res, next)=>{

try{

let {email, password} = req.body

if(!email || !password){

return next(error(StatusCodes.UNAUTHORIZED, 'Please provide the email and the password!'))
}

const user = await touruser.findOne({email})

if(!user){
return next(error(StatusCodes.UNAUTHORIZED, 'The email cannot seem to be found!'))

}

// const {isAdmin, ...otherDetails} = user._doc

// const userObj = user.toObject(); // convert the Mongoose document to a plain object
// delete userObj.password; // remove the password field
// delete userObj.isAdmin

const correctpassword = await user.checkpwd(password)

if(!correctpassword){
return next(error(StatusCodes.UNAUTHORIZED, 'The password is incorrect!'))
}

// const token = jwt.sign({id:user._id, isAdmin:user._isAdmin}, process.env.jwt_secret)


// res.cookie('access_token', token, {
// httpOnly:true,
// }).status(StatusCodes.OK).json(userObj)

// login page


if(email && password){

return res.status(StatusCodes.OK).json({msg: 'You have been logged in successfully'})
}

}

catch(err){


next(err)
}



}



module.exports = {register, login}
74 changes: 59 additions & 15 deletions backend/models/User.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,76 @@
import mongoose from "mongoose";
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs')

const userSchema = new mongoose.Schema(



const userschema = new mongoose.Schema(
{

username: {

type: String,
required: true,
required:[true, 'name of the user must be provided'],
minlength: 3,
maxlength: 25,
unique: true,

},

email: {

type: String,
required: true,
unique: true,
required:[true, 'email must be provided'],
match:[
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Please provide a valid email',

],

unique:true


},


password: {
type: String,
required: true,
},

photo: {
type: String,
},
required:[true, 'password must be provided'],
minlength:5
}

role: {
type: String,
default: "user",
},
},

{ timestamps: true }
);

export default mongoose.model("User", userSchema);


userschema.pre('save', async function(){

const salt = await bcrypt.genSalt(10)
this.password = await bcrypt.hash(this.password, salt)
})

userschema.methods.checkpwd = async function(candidatePassword){

const ismatch = await bcrypt.compare(candidatePassword, this.password)

return ismatch


}








module.exports = mongoose.model('user', userschema);





Loading