Commit 4cef5a62 authored by jean-X's avatar jean-X

Corrections de bug et ajout du signin

parent 8ebc9f41
......@@ -2,9 +2,8 @@ const jwt = require("jsonwebtoken");
PRIVATE_KEY = 'BABADOOK';
exports.checkToken = function (req, res, next) {
let token = req.headers['x-access-token'] || req.headers['authorization']; // Express headers are auto converted to lowercase
let token = req.headers['x-access-token'] || req.headers['authorization'];
if (token && token.startsWith('Bearer ')) {
// Remove Bearer from string
token = token.slice(7, token.length);
}
if (token) {
......
......@@ -7,7 +7,7 @@ exports.removeMovie = function (req, res) {
} else {
const movieIndex = user_instance.movies.findIndex(movie => movie.id == req.params.movieId);
if (movieIndex === -1) {
res.status(400).send('Mauvais movie id.');
res.status(404).send('Mauvais movie id.');
} else {
user_instance.movies.splice(movieIndex, 1);
user_instance.save();
......
......@@ -13,21 +13,17 @@ async function hashPassword(password) {
})
}
// SIGN UP
router.post('/signup', async (req, res) => {
// SIGN IN
router.post('/signin', async (req, res) => {
const user = new User(req.body);
user.password = await hashPassword(user.password);
user.save(function (err, user) {
if (err) {
console.log('ERROR MONGO', err, PRIVATE_KEY);
return res.status(400).json({message: err.message});
}
jwt.sign({name: user.name, password: user.password}, PRIVATE_KEY, {expiresIn: '1h'}, (err, token) => {
if (err) {
console.log('ERROR JWT', err, PRIVATE_KEY);
}
res.json({token});
})
return res.status(201).json({message: 'Created'})
});
});
......@@ -45,7 +41,7 @@ router.post('/login', async (req, res) => {
if (err) {
console.log('ERROR JWT', err, PRIVATE_KEY);
}
res.json({token, user});
return res.json({token, user});
})
}
});
......
<template>
<div id="app">
<div id="nav">
<router-link to="/movies">Search</router-link> |
<router-link v-if="userIsConnected()" to="/home">My movies</router-link> |
<router-link to="/">Login</router-link>
<router-link v-if="userIsConnected()" to="/movies">Search | </router-link>
<router-link v-if="userIsConnected()" to="/home">My movies | </router-link>
<router-link to="/">Login | </router-link>
<router-link to="/signin">Signin</router-link>
</div>
<router-view/>
</div>
......
......@@ -2,6 +2,7 @@ import Vue from 'vue'
import VueRouter from 'vue-router'
import Search from '../views/Search.vue'
import Login from '../views/Login.vue'
import Signin from '../views/Signin.vue'
import Home from '../views/Home.vue'
Vue.use(VueRouter);
......@@ -12,6 +13,11 @@ const routes = [
name: 'authent',
component: Login
},
{
path: '/signin',
name: 'signin',
component: Signin
},
{
path: '/movies',
name: 'movies',
......@@ -21,14 +27,6 @@ const routes = [
path: '/home',
name: 'home',
component: Home
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
];
......
......@@ -2,7 +2,7 @@
<div id='login'>
<div>
<input type="text" placeholder="Name" v-model="name">
<input type="text" placeholder="Password" v-model="password">
<input type="password" placeholder="Password" v-model="password" v-on:keyup="pressKeyLogin">
<button @click="login">login</button>
</div>
<div>
......@@ -24,6 +24,11 @@
mounted() {
},
methods: {
pressKeyLogin(e) {
if(e.keyCode === 13 ) {
this.login();
}
},
login() {
axios.post('http://localhost:9000/auth/login', {name: this.name, password: this.password})
.then(response => {
......@@ -37,5 +42,6 @@
});
}
}
}
</script>
\ No newline at end of file
<template>
<div id='signin'>
<div>
<input type="text" placeholder="Name" v-model="name">
<input type="password" placeholder="Password" v-model="password" v-on:keyup="pressKeySignin">
<button @click="signin">signin</button>
</div>
<div>
<span>{{error}}</span>
</div>
</div>
</template>
<script>
const axios = require('axios');
export default {
name: 'signin',
data() {
return {
name: String(''),
password: String(''),
error: String('')
}
},
mounted() {
},
methods: {
pressKeySignin(e) {
if(e.keyCode === 13 ) {
this.signin();
}
},
signin() {
axios.post('http://localhost:9000/auth/signin', {name: this.name, password: this.password})
.then( () => {
this.$router.push('/');
}).catch(error => {
this.error = error;
});
}
}
}
</script>
......@@ -17,6 +17,28 @@
"@types/babel-types": "7.0.7"
}
},
"@types/bson": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.3.tgz",
"integrity": "sha512-mVRvYnTOZJz3ccpxhr3wgxVmSeiYinW+zlzQz3SXWaJmD1DuL05Jeq7nKw3SnbKmbleW5qrLG5vdyWe/A9sXhw==",
"requires": {
"@types/node": "14.14.31"
}
},
"@types/mongodb": {
"version": "3.6.8",
"resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.8.tgz",
"integrity": "sha512-8qNbL5/GFrljXc/QijcuQcUMYZ1iWNcqnJ6tneROwbfU0LsAjQ9bmq3aHi5lWXM4cyBPd2F/n9INAk/pZZttHw==",
"requires": {
"@types/bson": "4.0.3",
"@types/node": "14.14.31"
}
},
"@types/node": {
"version": "14.14.31",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.31.tgz",
"integrity": "sha512-vFHy/ezP5qI0rFgJ7aQnjDXwAMrG0KqqIH7tQG5PPv3BWBayOPIQNBjVc/P6hhdZfMx51REc6tfDNXHUio893g=="
},
"abbrev": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
......@@ -199,15 +221,6 @@
"resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
"integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ=="
},
"bl": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-2.2.0.tgz",
"integrity": "sha512-wbgvOpqopSr7uq6fJrLH8EsvYMJf9gzfo2jCsL2eTy75qXPukA4pCgHamOQkZtY5vmfVtjB+P3LNlMHW5CEZXA==",
"requires": {
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2"
}
},
"bluebird": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
......@@ -269,9 +282,9 @@
}
},
"bson": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.3.tgz",
"integrity": "sha512-TdiJxMVnodVS7r0BdL42y/pqC9cL2iKynVwA0Ho3qbsQYr428veL3l7BQyuqiw+Q5SqqoT0m4srSY/BlZ9AxXg=="
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.5.tgz",
"integrity": "sha512-kDuEzldR21lHciPQAIulLs1LZlCXdLziXI6Mb/TDkwXhb//UORJNPXgcRs2CuO4H0DcMkpfT3/ySsP3unoZjBg=="
},
"buffer-equal-constant-time": {
"version": "1.0.1",
......@@ -1047,9 +1060,9 @@
}
},
"kareem": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.1.tgz",
"integrity": "sha512-l3hLhffs9zqoDe8zjmb/mAN4B8VT3L56EUvKNqLFVs9YlFA+zx7ke1DO8STAdDyYNkeSo1nKmjuvQeI12So8Xw=="
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.2.tgz",
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
},
"kind-of": {
"version": "3.2.2",
......@@ -1197,51 +1210,62 @@
}
},
"mongodb": {
"version": "3.5.2",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.5.2.tgz",
"integrity": "sha512-Lxt4th2tK2MxmkDBR5cMik+xEnkvhwg0BC5kGcHm9RBwaNEsrIryvV5istGXOHbnif5KslMpY1FbX6YbGJ/Trg==",
"version": "3.6.4",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.4.tgz",
"integrity": "sha512-Y+Ki9iXE9jI+n9bVtbTOOdK0B95d6wVGSucwtBkvQ+HIvVdTCfpVRp01FDC24uhC/Q2WXQ8Lpq3/zwtB5Op9Qw==",
"requires": {
"bl": "2.2.0",
"bson": "1.1.3",
"bl": "2.2.1",
"bson": "1.1.5",
"denque": "1.4.1",
"require_optional": "1.0.1",
"safe-buffer": "5.1.2",
"saslprep": "1.0.3"
},
"dependencies": {
"bl": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/bl/-/bl-2.2.1.tgz",
"integrity": "sha512-6Pesp1w0DEX1N550i/uGV/TqucVL4AM/pgThFSN/Qq9si1/DF9aIHs1BxD8V/QU0HoeHO6cQRTAuYnLPKq1e4g==",
"requires": {
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2"
}
},
"bson": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.5.tgz",
"integrity": "sha512-kDuEzldR21lHciPQAIulLs1LZlCXdLziXI6Mb/TDkwXhb//UORJNPXgcRs2CuO4H0DcMkpfT3/ySsP3unoZjBg=="
}
}
},
"mongoose": {
"version": "5.8.10",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.8.10.tgz",
"integrity": "sha512-3sRiZhtMIB4egqxWbry23C+xX87kQ0aTvPtMXxWXCBGfCRkXMJl/CLiftYcle/JPy09Lv5u+ZCBpIJUgwDMtxw==",
"requires": {
"bson": "1.1.3",
"kareem": "2.3.1",
"mongodb": "3.4.1",
"version": "5.11.18",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.11.18.tgz",
"integrity": "sha512-RsrPR9nhkXZbO3ml0DcmdbfeMvFNhgFrP81S6o1P+lFnDTNEKYnGNRCIL+ojD69wj7H5jJaAdZ0SJ5IlKxCHqw==",
"requires": {
"@types/mongodb": "3.6.8",
"bson": "1.1.5",
"kareem": "2.3.2",
"mongodb": "3.6.4",
"mongoose-legacy-pluralize": "1.0.2",
"mpath": "0.6.0",
"mquery": "3.2.2",
"mpath": "0.8.3",
"mquery": "3.2.4",
"ms": "2.1.2",
"regexp-clone": "1.0.0",
"safe-buffer": "5.1.2",
"safe-buffer": "5.2.1",
"sift": "7.0.1",
"sliced": "1.0.1"
},
"dependencies": {
"mongodb": {
"version": "3.4.1",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.4.1.tgz",
"integrity": "sha512-juqt5/Z42J4DcE7tG7UdVaTKmUC6zinF4yioPfpeOSNBieWSK6qCY+0tfGQcHLKrauWPDdMZVROHJOa8q2pWsA==",
"requires": {
"bson": "1.1.3",
"require_optional": "1.0.1",
"safe-buffer": "5.1.2",
"saslprep": "1.0.3"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
}
}
},
......@@ -1263,14 +1287,14 @@
}
},
"mpath": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.6.0.tgz",
"integrity": "sha512-i75qh79MJ5Xo/sbhxrDrPSEG0H/mr1kcZXJ8dH6URU5jD/knFxCVqVC/gVSW7GIXL/9hHWlT9haLbCXWOll3qw=="
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.8.3.tgz",
"integrity": "sha512-eb9rRvhDltXVNL6Fxd2zM9D4vKBxjVVQNLNijlj7uoXUy19zNDsIif5zR+pWmPCWNKwAtqyo4JveQm4nfD5+eA=="
},
"mquery": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.2.tgz",
"integrity": "sha512-XB52992COp0KP230I3qloVUbkLUxJIu328HBP2t2EsxSFtf4W1HPSOBWOXf1bqxK4Xbb66lfMJ+Bpfd9/yZE1Q==",
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.4.tgz",
"integrity": "sha512-uOLpp7iRX0BV1Uu6YpsqJ5b42LwYnmu0WeF/f8qgD/On3g0XDaQM6pfn0m6UxO6SM8DioZ9Bk6xxbWIGHm2zHg==",
"requires": {
"bluebird": "3.5.1",
"debug": "3.1.0",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment