title: token is not working after login when i am trying to get method with token
body: <p>Que:After generating Authtoken when i am trying to use get method as a profile (which i mentioned in signin.js file) it not providing the user detail instead its showing user is not loged in.(middleware file)</p>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>one more question : do i have to store token in database or not? if yes,please explain in detail </p>
  
  <p>controller: signin.js code</p>
  
  <pre><code>var express=require('express');
  var signupuser=require('../model/singup')
  var router=express.Router();
  var bcrypt=require('bcryptjs');
  var validator=require('validator');
  var jwt=require('jsonwebtoken');
  var auth=require('../middleware/auth')
  router.post('/',async(req,res)=&gt;
  {
  const {email,password}=req.body
  const user=await signupuser.findByCredentials(email,password)
  if(!user){
      return res.status(401).send({error:"Login failed ! email or pasword wrong"})
  }
  const authtoken=await user.generateauthToken()
  res.send({user,authtoken})
  });
  
  router.get('/profile',auth,(req,res)=&gt;{  // using this method to get profile but it is not providing token 
      res.send(req.user)
  })
  module.exports=router;
  </code></pre>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>Middleware:auth.js code</p>
  
  <pre><code>var express=require('express');
  var signupuser=require('../model/singup')
  var jwt=require('jsonwebtoken');
  var router=express.Router();
  var auth= async function(req,res,next){
  const token =req.header("Authorization").replace('Bearer ','')
  const decode=jwt.verify(token,"mynameisbhupesh");
  try{
      const user= await signupuser.findOne({id:decode._id,'authtoken.token':token})
      if(!user){
          res.json("user not loged in" )
      }
      req.user=user
      req.token=token
      next();
   }catch(err){
  res.status(401).send({error:'no auth token'})
   }}
  
  
  module.exports=auth
  </code></pre>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>model:signup.js</p>
  
  <pre><code>var mongoose=require('mongoose');
  require('../db');
  var bcrypt=require('bcryptjs');
  var validator=require('validator');
  var jwt=require('jsonwebtoken');
  mongoose.Promise=require('bluebird');
  var schema= new mongoose.Schema({
      name:{type:String,required:true},
      email:{type:String,required:true,unique:true,validate:value=&gt;{
          if(!validator.isEmail(value)){
              throw new Error({error:'invalid email address'}) 
          }
      }},
      password:{type:String,required:true,minlength:5}, 
      //  authtoken:[{token:{type:String,required:true}}],  
  });
  
  
  schema.pre('save',async function(next){
      const user=this
      if(user.isModified('password')){
          user.password=await bcrypt.hash(user.password,8)
      }
      next()
  })
  
  schema.methods.generateauthToken=async function(){
      const user=this
      const token=jwt.sign({_id:user._id},"mynameisbhupesh",{
          expiresIn:10000000000000000000000000
      })
      // user.authtoken=user.authtoken.concat({token})
     await user.save()
      return token
  }
  schema.statics.findByCredentials =async (email,password)=&gt;{
      const user=await signupuser.findOne({email})
      if(!user){
          throw new Error ({error:'Invalid user'}) 
      }
      const ispassmatch=await bcrypt.compare(password,user.password) 
      if(!ispassmatch){
          throw new Error ({error:'Invalid user'})
      } 
      return user 
       }
  
  const signupuser=mongoose.model('signupuser',schema);
  module.exports=signupuser;
  </code></pre>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>controller:signup.js</p>
  
  <pre><code>var express=require('express');
  var signupuser=require('../model/singup')
  var router=express.Router();
  var bcrypt=require('bcryptjs');
  var validator=require('validator');
  var jwt=require('jsonwebtoken');
  router.post('/',async (req,res)=&gt;{
      try{
          const user=new signupuser(req.body)
          await user.save()
          const token=await user.generateauthToken()
          res.status(201).send({user,token})
      }
      catch(error){
          res.status(400).send(error)
      }
  })
  module.exports=router;
  </code></pre>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>database:db.js</p>
  
  <pre><code>var cookie=require('cookie-parser');
  var Mongostor=require('connect-mongo');
  var mongose=require('mongoose');
  var express=require('express');
  var session=require('express-session');
  var app=express()
  require('dotenv')
  mongose.set('useNewUrlParser', 'true');
  mongose.set('useCreateIndex', 'true');
  mongose.set('useFindAndModify', 'true');
  mongose.connect("mongodb://127.0.0.1:27017/website");
  
  var db=mongose.connection;
  db.on('error',console.error.bind(console,'connection error'));
  db.once('open',(req,res)=&gt;{
      console.log("connected with db!!!");
  })
  app.use(cookie());
  app.use(session({
      secret:'topsecret ',
      resave:false,
      cookies:{
          secrue:true
      },
  }))
  module.exports=mongose
  </code></pre>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>index.js file </p>
  
  <pre><code>require('./middleware/auth')
  var app=require('./server')
  var signup=require('./controller/signup');
  var signin=require('./controller/signin')
  var session=require('express-session')
  var db=require('./db');
  var bodyParser = require('body-parser');
  var cookie=require('cookie-parser');
  var Mongostor=require('connect-mongo');
  app.use(bodyParser.urlencoded({ extended: false }))
  app.use(bodyParser.json())
  app.set('viewengin','ejs')
  app.set('useNewUrlParser', 'true')
  app.set('useUnifiedTopology', 'true')
  app.use('/user/signup',signup);
  app.use('/user/signin',signin);
  
  
  app.all('*',(req,res)=&gt;{
      return res.json({status: 205,message: 'not found'});
  })
  
  var server=app.get('/',(req,res)=&gt;{
  req.end()
  });
  </code></pre>