Skip to content
Snippets Groups Projects
Commit 0bc27bb4 authored by abdullah Bin Zubair's avatar abdullah Bin Zubair
Browse files

Create profile and fetch details

parent 4a20f104
No related branches found
No related tags found
No related merge requests found
{
"mongooURI":"mongodb+srv://abz282:abz282_@devconnector.oiepthu.mongodb.net/?retryWrites=true&w=majority"
"mongooURI":"mongodb+srv://abz282:abz282_@devconnector.oiepthu.mongodb.net/?retryWrites=true&w=majority",
"jwtSecret":"mysecret"
}
\ No newline at end of file
const jwt = require('jsonwebtoken');
const config = require('config');
module.exports = function(req, res, next){
const token = req.header('x-auth-token');
if (!token){
return res.status(401).json({msg: 'No token, authorization denied'});
}
try{
const decoded = jwt.verify(token, config.get('jwtSecret'));
req.user = decoded.user;
next();
}catch(err) {
res.status(401).json({msg: 'Token is not valid '});
}
}
\ No newline at end of file
......@@ -9,6 +9,22 @@ const UserSchema = new mongoose.Schema({
type: String,
required: true
},
password: {
type: String,
required: true
},
university:{
type: String,
required: true
},
highestEducation: {
type: String,
required: true
},
country: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
......
const express = require('express');
const router = express.Router();
const auth = require('../../middleware/auth');
const User = require('../../models/User');
// route GET api/auth
router.get('/', (req, res) => res.send('Auth route'));
router.get('/', auth, async (req, res) => {
try{
const user = await User.findById(req.user.id).select('-password');
res.json(user);
} catch(err){
console.log(err);
res.status(500).send('Server Error');
}
});
module.exports = router;
\ No newline at end of file
const express = require('express');
const router = express.Router();
// route GET api/profile
router.get('/', (req, res) => res.send('Profile route'));
module.exports = router;
\ No newline at end of file
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const config = require('config')
const {check, validationResult } = require('express-validator');
// route GET api/users
router.get('/', (req, res) => res.send('User route'));
const User = require('../../models/User');
// route POST api/users
router.post('/', [
check('name', 'Name is required').not().isEmpty(),
check('email','Please enter a valid email').not().isEmpty(),
check('password','Enter a password with 5 or more characters').isLength({min:5}),
check('university', 'Enter univerity name').not().isEmpty(),
check('highestEducation', 'Enter highestEducation').not().isEmpty(),
check('country', 'Enter country').not().isEmpty(),
], async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()){
return res.status(400).json({ errors: errors.array()});
}
const {name, email, password, university,
highestEducation,
country} = req.body;
try{
let user = await User.findOne({email});
if (user){
res.send(400).json({error: [{msg: 'User already exist'}]});
}
user = new User({
name,
email,
password,
university,
highestEducation,
country
});
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(password, salt);
await user.save();
const payload = {
user:{
id: user.id
}
}
jwt.sign(payload, config.get('jwtSecret'), {expiresIn:300000},
(err, token) => {
if (err) throw err;
res.json(token);
}
);
}catch(err){
console.error(err.message);
res.status(500).send('Server Error');
}
});
module.exports = router;
\ No newline at end of file
......@@ -7,9 +7,10 @@ connectDB();
app.get('/', (req, res) => res.send('Api Running'));
//Define Routes
app.use(express.json({extended : false}));
//Defined Routes
app.use('/api/users', require('./routes/api/user'));
app.use('/api/profile',require('./routes/api/profile'));
app.use('/api/auth',require('./routes/api/auth'));
const PORT = 5000;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment