Skip to content

Commit

Permalink
feat(*): added user login and register
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Aug 23, 2021
1 parent 2416121 commit e28d822
Show file tree
Hide file tree
Showing 17 changed files with 905 additions and 7 deletions.
101 changes: 101 additions & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const Users = require('../models/user');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');

const userCtrl = {
register,
refreshToken,
login
}

async function register(req, res) {
try {
const { name, email, password } = req.body;
console.log(req.body)
console.log(name)
console.log(email)
console.log(password)
const user = await Users.findOne({ email })
if (user) return res.status(400).json({ msg: "The email already exists" })

if (password.length < 6)
return res.status(400).json({ msg: "Password is at least 6 characters long" })

//Password Encryption
const passwordHash = await bcrypt.hash(password, 10)

//Create new user instance
const newUser = new Users({
name, email, password: passwordHash
})
// Save mongodb
await newUser.save()

//Create jsonwebtoken for authentication
const accesstoken = createAccessToken({ id: newUser._id })
const refreshtoken = createRefreshToken({ id: newUser._id })

res.cookie('refreshtoken', refreshtoken, {
httpOnly: true,
path: '/api/user/refresh_token'
})
// res.json({ password, passwordHash })
res.json({ accesstoken })
// res.json({ msg: "Register Successful" })

} catch (err) {
return res.status(500).json({ msg: err.message })
}
}

function refreshToken(req, res) {
try {
const rf_token = req.cookies.refreshtoken;
if (!rf_token) return res.status(400).json({ msg: "Please Login or Register" })

jwt.verify(rf_token, process.env.REFRESH_TOKEN_SECRET, (err, user) => {
if (err) return res.status(400).json({ msg: "Please Login or Register" })

const accesstoken = createAccessToken({ id: user.id })

res.json({ accesstoken })
})
res.json({ rf_token })
} catch (err) {
return res.status(500).json({ msg: err.message })
}
}

async function login(req, res) {
try {
const { email, password } = req.body

const user = await Users.findOne({ email })
if (!user) return res.status(400).json({ msh: "User does not exist." })

const isMatch = await bcrypt.compare(password, user.password)
if (!isMatch) return res.status(400).json({ msh: "Invalid password" })

const accesstoken = createAccessToken({ id: user._id })
const refreshtoken = createRefreshToken({ id: user._id })

res.cookie('refreshtoken', refreshtoken, {
httpOnly: true,
path: '/api/user/refresh_token'
})
res.json({ accesstoken })
// res.json({ msg: "Login successful" })

} catch (err) {
return res.status(500).json({ msg: err.message })
}
}

const createAccessToken = (user) => {
return jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1d' })
}
const createRefreshToken = (user) => {
return jwt.sign(user, process.env.REFRESH_TOKEN_SECRET, { expiresIn: '7d' })
}

module.exports = userCtrl
31 changes: 31 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true,
trim: true
},
role: {
type: Number,
default: 0
},
cart: {
type: Array,
default: []
}
}, {
timestamps: true
})

module.exports = mongoose.model('Users', userSchema)
Loading

0 comments on commit e28d822

Please sign in to comment.