Merge pull request #25 from AlexisNP/issues/cookies

Added cookie for login
This commit is contained in:
AlexisNP
2020-12-26 17:13:15 +01:00
committed by GitHub
5 changed files with 75 additions and 75 deletions

View File

@@ -1,60 +1,51 @@
'use strict'; 'use strict';
// MODULES // MODULES
const express = require('express') const express = require('express');
const bodyParser = require('body-parser') const bodyParser = require('body-parser');
const helmet = require('helmet') const helmet = require('helmet');
const morgan = require('morgan') const morgan = require('morgan');
const cors = require('cors') // module to format the json response const cors = require('cors'); // module to format the json response
const dotenv = require('dotenv').config() const dotenv = require('dotenv').config();
// Creates instances of database connections // Creates instances of database connections
const connection = require('./database/bookshelf') const connection = require('./database/bookshelf');
const db = connection.db const db = connection.db;
// CONSTANTS // CONSTANTS
const port = 2814 const port = 2814;
// Import routes // Import routes
const routes = require('./routes') const routes = require('./routes');
// Builds app w/ express // Builds app w/ express
let app = express() let app = express();
app.use(bodyParser.json({ limit: '10kb' })) app.use(bodyParser.json({ limit: '10kb' }));
app.use(cors()) app.use(cors({
app.use(morgan('tiny')) origin: [
app.use(helmet()) "http://localhost:8080",
],
credentials: true,
}));
app.use(morgan('dev'));
app.use(helmet());
// Serves // Server
const server = app.listen( port, () => {console.log(`App listening on port ${port}`)}) app.listen(port, () => console.log(`App listening on port ${port}`));
// Get credentials
// app.get('/api/login', (req, res, next) => {
// if (req.headers.auracle_key !== process.env.API_KEY_PUBLIC) {
// return res.status(401).send('Credentials error !')
// } else {
// return res.status(200).send(JSON.stringify(
// {
// "secret_key": process.env.API_KEY_PRIVATE,
// })
// )
// }
// })
// Auth guard // Auth guard
const authguard = (req, res, next) => { const authguard = (req, res, next) => {
if (req.headers.auracle_key !== process.env.API_KEY_PUBLIC) { if (req.headers.auracle_key !== process.env.API_KEY_PUBLIC) {
return res.status(401).send('The API key is either missing or incorrect.') return res.status(401).send('The API key is either missing or incorrect.');
} else { } else {
next() next();
} }
} }
// Routing // Routing
app.use('/api/spells', authguard,routes.spells) app.use('/api/spells', authguard, routes.spells);
app.use('/api/schools', authguard, routes.schools) app.use('/api/schools', authguard, routes.schools);
app.use('/api/meta_schools', authguard, routes.meta_schools) app.use('/api/meta_schools', authguard, routes.meta_schools);
app.use('/api/variables', authguard, routes.variables) app.use('/api/variables', authguard, routes.variables);
app.use('/api/ingredients', authguard, routes.ingredients) app.use('/api/ingredients', authguard, routes.ingredients);
app.use('/api/users', authguard, routes.users) app.use('/api/users', authguard, routes.users);

View File

@@ -22,7 +22,6 @@ Globals.forEach(component => {
// Cookies // Cookies
import VueCookies from 'vue-cookies' import VueCookies from 'vue-cookies'
Vue.use(VueCookies) Vue.use(VueCookies)
Vue.$cookies.config('30d','','')
// Jquery // Jquery
import jquery from 'jquery' import jquery from 'jquery'

View File

@@ -116,14 +116,18 @@ export default {
this.submitted = true; this.submitted = true;
let userInput = { let userInput = {
"mail": this.email, "user": {
"password": this.password "mail": this.email,
"password": this.password,
},
"remember_me": this.remember_me
}; };
if (!userInput.mail) { // Check is all inputs are set
if (!userInput.user.mail) {
this.errors.email = "Vous devez renseigner une addresse mail." this.errors.email = "Vous devez renseigner une addresse mail."
} }
if (!userInput.password) { if (!userInput.user.password) {
this.errors.password = "Vous devez renseigner un mot de passe." this.errors.password = "Vous devez renseigner un mot de passe."
} }
@@ -134,6 +138,7 @@ export default {
} }
} }
// If no errors are present, submits the form
if (this.submitted) { if (this.submitted) {
this.$store.dispatch('user_login', userInput) this.$store.dispatch('user_login', userInput)
.then(() => { .then(() => {

View File

@@ -148,23 +148,23 @@ export default {
this.submitted = true; this.submitted = true;
let userInput = { let userInput = {
"name": this.name, "user": {
"mail": this.email, "name": this.name,
"password": this.password "mail": this.email,
"password": this.password
}
} }
// Check if all inputs are valid without sending anything
if (!userInput.name) { if (!userInput.name) {
this.errors.name = "Vous devez renseigner un pseudonyme." this.errors.name = "Vous devez renseigner un pseudonyme."
} }
if (!userInput.mail) { if (!userInput.mail) {
this.errors.email = "Vous devez renseigner une addresse mail." this.errors.email = "Vous devez renseigner une addresse mail."
} }
if (!userInput.password) { if (!userInput.password) {
this.errors.password = "Vous devez renseigner un mot de passe." this.errors.password = "Vous devez renseigner un mot de passe."
} }
if ((userInput.password.localeCompare(this.password_check)) != 0) { if ((userInput.password.localeCompare(this.password_check)) != 0) {
this.errors.password_check = "Les mots de passe ne correspondent pas." this.errors.password_check = "Les mots de passe ne correspondent pas."
} }
@@ -176,12 +176,14 @@ export default {
} }
} }
// If no errors are present, submits the form
if (this.submitted) { if (this.submitted) {
this.$store.dispatch('user_register', userInput) this.$store.dispatch('user_register', userInput)
.then(() => { .then(() => {
this.$router.push('/'); this.$router.push('/');
}) })
.catch(err => { .catch(err => {
// Sets errors in case something wrong comes back from the API
if (err.status === 409) { if (err.status === 409) {
this.errors.email = err.data.error; this.errors.email = err.data.error;
} else { } else {

View File

@@ -1,4 +1,4 @@
// import cookie from 'vue-cookies'; import cookie from 'vue-cookies';
// API // API
import { RepositoryFactory } from "~/api/repositories"; import { RepositoryFactory } from "~/api/repositories";
@@ -7,46 +7,49 @@ const Users = RepositoryFactory.get('users');
export const namespaced = true; export const namespaced = true;
const state = { const state = {
status: { profile: cookie.get('user_profile') || null,
logged: false,
profile: null,
}
}; };
const getters = { const getters = {
getUserProfile: state => { getUserProfile: state => {
if (state.status.logged) { return state.profile
return state.status.profile
} else {
return false
}
} }
}; };
const mutations = { const mutations = {
loginUser(state, user) { loginUser(state, user) {
state.status.profile = user; state.profile = user;
state.status.logged = true;
}, },
logoutUser(state) { logoutUser(state) {
state.status.profile = null; state.profile = null;
state.status.logged = false;
}, },
registerUser() { registerUser() {
// Will contain the email call eventually
} }
}; };
const actions = { const actions = {
/**
* @param data
* An object containing :
* - user object with mail and password hash properties
* - remember_me boolean to check cookie expiration time
*/
user_login ({ commit }, data) { user_login ({ commit }, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Users.login(data) Users.login(data.user)
.then(v => { .then(v => {
let user = v.data.user; let user = v.data.user;
// cookie.set('user_token', user.uuid);
// Check if the use wishes to be remembered
if (data.remember_me) {
cookie.set('user_profile', user, 60 * 60 * 24 * 30); // Expires after a month
} else {
cookie.set('user_profile', user, 0); // Expires after browser session expires
}
commit('loginUser', user); commit('loginUser', user);
resolve(user); resolve(user);
}) })
@@ -57,13 +60,18 @@ const actions = {
}, },
user_logout ({ commit }) { user_logout ({ commit }) {
// cookie.remove('user_token'); cookie.remove('user_profile');
commit('logoutUser'); commit('logoutUser');
}, },
/**
* @param data
* An object containing :
* - user object with string mail, string name, and string password
*/
user_register ({ commit }, data) { user_register ({ commit }, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Users.register(data) Users.register(data.user)
.then(() => { .then(() => {
commit('registerUser'); commit('registerUser');
resolve(); resolve();
@@ -75,11 +83,6 @@ const actions = {
} }
}; };
// Check if a cookie with user token exists
// if (cookie.get('user_token')) {
// mutations.loginUser(state, cookie.get('user_token'));
// }
export default { export default {
state, state,
getters, getters,