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';
// MODULES
const express = require('express')
const bodyParser = require('body-parser')
const helmet = require('helmet')
const morgan = require('morgan')
const cors = require('cors') // module to format the json response
const dotenv = require('dotenv').config()
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const morgan = require('morgan');
const cors = require('cors'); // module to format the json response
const dotenv = require('dotenv').config();
// Creates instances of database connections
const connection = require('./database/bookshelf')
const db = connection.db
const connection = require('./database/bookshelf');
const db = connection.db;
// CONSTANTS
const port = 2814
const port = 2814;
// Import routes
const routes = require('./routes')
const routes = require('./routes');
// Builds app w/ express
let app = express()
app.use(bodyParser.json({ limit: '10kb' }))
app.use(cors())
app.use(morgan('tiny'))
app.use(helmet())
let app = express();
app.use(bodyParser.json({ limit: '10kb' }));
app.use(cors({
origin: [
"http://localhost:8080",
],
credentials: true,
}));
app.use(morgan('dev'));
app.use(helmet());
// Serves
const server = 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,
// })
// )
// }
// })
// Server
app.listen(port, () => console.log(`App listening on port ${port}`));
// Auth guard
const authguard = (req, res, next) => {
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 {
next()
next();
}
}
// Routing
app.use('/api/spells', authguard,routes.spells)
app.use('/api/schools', authguard, routes.schools)
app.use('/api/meta_schools', authguard, routes.meta_schools)
app.use('/api/variables', authguard, routes.variables)
app.use('/api/ingredients', authguard, routes.ingredients)
app.use('/api/users', authguard, routes.users)
app.use('/api/spells', authguard, routes.spells);
app.use('/api/schools', authguard, routes.schools);
app.use('/api/meta_schools', authguard, routes.meta_schools);
app.use('/api/variables', authguard, routes.variables);
app.use('/api/ingredients', authguard, routes.ingredients);
app.use('/api/users', authguard, routes.users);

View File

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

View File

@@ -116,14 +116,18 @@ export default {
this.submitted = true;
let userInput = {
"mail": this.email,
"password": this.password
"user": {
"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."
}
if (!userInput.password) {
if (!userInput.user.password) {
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) {
this.$store.dispatch('user_login', userInput)
.then(() => {

View File

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

View File

@@ -1,4 +1,4 @@
// import cookie from 'vue-cookies';
import cookie from 'vue-cookies';
// API
import { RepositoryFactory } from "~/api/repositories";
@@ -7,46 +7,49 @@ const Users = RepositoryFactory.get('users');
export const namespaced = true;
const state = {
status: {
logged: false,
profile: null,
}
profile: cookie.get('user_profile') || null,
};
const getters = {
getUserProfile: state => {
if (state.status.logged) {
return state.status.profile
} else {
return false
}
return state.profile
}
};
const mutations = {
loginUser(state, user) {
state.status.profile = user;
state.status.logged = true;
state.profile = user;
},
logoutUser(state) {
state.status.profile = null;
state.status.logged = false;
state.profile = null;
},
registerUser() {
// Will contain the email call eventually
}
};
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) {
return new Promise((resolve, reject) => {
Users.login(data)
Users.login(data.user)
.then(v => {
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);
resolve(user);
})
@@ -57,13 +60,18 @@ const actions = {
},
user_logout ({ commit }) {
// cookie.remove('user_token');
cookie.remove('user_profile');
commit('logoutUser');
},
/**
* @param data
* An object containing :
* - user object with string mail, string name, and string password
*/
user_register ({ commit }, data) {
return new Promise((resolve, reject) => {
Users.register(data)
Users.register(data.user)
.then(() => {
commit('registerUser');
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 {
state,
getters,