daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest May 16th, 2018 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     INDEX
  3. */
  4.  
  5. const express = require('express'),
  6.     app = express(),
  7.     bodyParser = require('body-parser'),
  8.     jwt = require('jsonwebtoken'),
  9.     mongoose = require('mongoose');
  10.     user = require('./routes/user.route');
  11.  
  12. // SETTINGS
  13.  
  14. app.use(bodyParser.urlencoded({ extended: true }))
  15. app.use(bodyParser.json())
  16. app.use(function (req, res, next) {
  17.     res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
  18.     res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  19.     res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  20.     res.setHeader('Access-Control-Allow-Credentials', true);
  21.     next();
  22. });
  23.  
  24. // DB
  25.  
  26. mongoose.connect('mongodb://127.0.0.1/chat');
  27.  
  28. // ROUTES
  29.  
  30. app.get('/', (req, res) => {
  31.     res.json({success: 'TAK'});
  32. })
  33.  
  34. app.use('/user', user);
  35.  
  36. app.listen(4000, () => {
  37.     console.log('Server running on http://localhost:4000')
  38. })
  39.  
  40.  
  41.  
  42.  
  43.  
  44. /*
  45.     USER ROUTES
  46. */
  47.  
  48. const express = require('express'),
  49.     router = express.Router(),
  50.     bcrypt = require('bcrypt'),
  51.     mongoose = require('mongoose'),
  52.     User = require('../models/user.model'),
  53.     jwt = require('jsonwebtoken');
  54.  
  55.  
  56. // REGISTER
  57.  
  58. router.post('/register', (req, res) => {
  59.     User.register.count({email: req.body.email}, (err, count) => {
  60.         if(count > 0) {
  61.             res.json({success: false, message: 'E-mail jest już zajęty!'});
  62.         } else {
  63.             bcrypt.hash(req.body.password, 1, (err, hash) => {
  64.                 if(err) {
  65.                     res.json({error: true});
  66.                 } else {
  67.                     const user = new User.register({
  68.                         _id: new mongoose.Types.ObjectId(),
  69.                         email: req.body.email,
  70.                         password: hash,
  71.                         firstName: req.body.firstName,
  72.                         lastName: req.body.lastName,
  73.                         birthDay: req.body.birthDay,
  74.                         place: req.body.place,
  75.                         age: req.body.age,
  76.                         male: req.body.male,
  77.                         female: req.body.female
  78.                     });
  79.  
  80.                     user.save().then(result => {
  81.                         res.json({success: true, message: 'Rejestracja powiodła się pomyślnie!'});
  82.                     }).catch(err => {
  83.                         res.json({
  84.                             error: true
  85.                         })
  86.                     })
  87.                 }
  88.             })
  89.         }
  90.     })
  91. })
  92.  
  93.  
  94. // LOGIN
  95.  
  96. router.post('/login', (req, res) => {
  97.     User.login.findOne({email: req.body.email}).exec().then((user) => {
  98.         console.log(user);
  99.         if(user!=null && user!=undefined) {
  100.             bcrypt.compare(req.body.password, user.password, (err, result) => {
  101.                 if(err) {
  102.                     return res.json({success: false, message: 'Wystąpił gdfgbłąd. Spróbuj ponownie.'});
  103.                 } else if(result) {
  104.                     const token = jwt.sign({
  105.                         email: user.email,
  106.                         _id: user._id
  107.                     },
  108.                     'secret',
  109.                     {
  110.                         expiresIn: 60*2
  111.                     });
  112.                     return res.json({
  113.                         success: true,
  114.                         token: token
  115.                     })
  116.                 } else {
  117.                     return res.json({success: false, message: 'Niepoprawny email lub hasło!'});
  118.                 }
  119.             })
  120.         } else {
  121.             return res.json({success: false, message: 'Niepoprawny email lub hasło!'});
  122.         }
  123.     }).catch(err => {
  124.         res.json({
  125.             success: false,
  126.             message: 'Wystąpił błąd. Spróbuj ponownie.'
  127.         })
  128.     })
  129. })
  130.  
  131. // CHECK TOKEN
  132.  
  133. router.post('/token', (req, res) => {
  134.     const token = req.body.token;
  135.     jwt.verify(token, 'secret', (err, decoded) => {
  136.         if(err) {
  137.             res.status(500).json({success: false});
  138.         } else {
  139.             console.log(decoded);
  140.             res.json({success: true, expired: decoded.exp, email: decoded.email});
  141.         }
  142.     })
  143. })
  144.  
  145. module.exports = router
  146.  
  147.  
  148.  
  149.  
  150.  
  151. /*
  152.     USER MODELS
  153. */
  154.  
  155. const mongoose = require('mongoose');
  156. const Schema = mongoose.Schema;
  157.  
  158. const userRegister = new Schema({
  159.     email: {type: String, required: true},
  160.     password: {type: String, required: true},
  161.     firstName: {type: String, required: true},
  162.     lastName: {type: String, required: true},
  163.     birthDay: {type: Date, required: true},
  164.     place: {type: String, required: true},
  165.     age: {type: Number, required: true},
  166.     admin: {type: Boolean, default: false},
  167.     _id: {type: String, required: true},
  168.     male: {type: Boolean, required: true},
  169.     female: {type: Boolean, required: true}
  170. })
  171.  
  172. const userLogin = new Schema({
  173.     email: {type: String, required: true},
  174.     password: {type: String, required: true}
  175. })
  176.  
  177. var registerModel = mongoose.model('UserRegister', userRegister, 'users');
  178. var loginModel = mongoose.model('UserLogin', userLogin, 'users');
  179.  
  180. module.exports = {
  181.     register: registerModel,
  182.     login: loginModel
  183. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top