Commit 1d08007b authored by Jean Luc FERNANDES's avatar Jean Luc FERNANDES

first commit

parents
Pipeline #24191 failed with stages
in 52 seconds

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.

node_modules
.idea
# Default ignored files
/workspace.xml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<includedPredefinedLibrary name="Node.js Core" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="JSX" />
</component>
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/poc-express.iml" filepath="$PROJECT_DIR$/poc-express.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
var createError = require('http-errors');
var express = require('express');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
var usersRouter = require('./routes/users');
var movieDbRouter = require('./routes/movie-db');
var authentRouter = require('./routes/authent');
const {checkToken} = require("./middleware/auth-middleware");
var app = express();
var db = mongoose.connection;
// Mongo
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true});
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function () {
console.log('Connected on the database');
});
var header = function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,authorization,Authorization');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
};
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use('/', header);
app.use('/users', usersRouter);
app.use('/movies', checkToken, movieDbRouter);
app.use('/auth', authentRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
app.listen(9000);
module.exports = app;
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
if (token && token.startsWith('Bearer ')) {
// Remove Bearer from string
token = token.slice(7, token.length);
}
if (token) {
jwt.verify(token, PRIVATE_KEY, (err, decoded) => {
if (err) {
return res.status(401).json({
success: false,
message: 'Token is not valid'
});
} else {
req.decoded = decoded;
next();
}
});
} else {
return res.status(401).json({
success: false,
message: 'Auth token is not supplied'
});
}
};
\ No newline at end of file
var User = require('../models/user');
exports.removeMovie = function (req, res) {
User.findById(req.params.userId, function (err, user_instance) {
if (err) {
res.status(400).send(err);
} else {
const movieIndex = user_instance.movies.findIndex(movie => movie.id == req.params.movieId);
if (movieIndex === -1) {
res.status(400).send('Mauvais movie id.');
} else {
user_instance.movies.splice(movieIndex, 1);
user_instance.save();
res.json({movies: user_instance.movies});
}
}
});
};
exports.addMovie = function (req, res) {
User.findById(req.body.id, function (err, user_instance) {
if (err) {
res.status(400).send(err);
} else {
user_instance.movies.push(req.body.movie);
user_instance.save();
res.send(user_instance);
}
});
};
exports.movieSeen = function (req, res) {
User.findById(req.body.id, function (err, user_instance) {
if (err) {
res.status(400).send(err);
} else {
user_instance.movies.find(movie => movie.id == req.body.movieId).seen = true;
user_instance.save();
res.send(user_instance);
}
});
};
\ No newline at end of file
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = MovieSchema = new Schema({
id: Number,
title: String,
poster_path: String,
registrationDate: {
type: Date, default: function () {