From de90b7d7474282482d195814e889ceb6b769b6bb Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Fri, 18 Dec 2020 11:58:16 +0100 Subject: [PATCH 1/7] Revert "- Added timeline page (will need to be redone anyways)" This reverts commit 0178f15bf19dcc2f7f1399c975a6abffc1905996. --- .../src/components/spells/edit-spell-card.vue | 1 + client/src/components/timeline/timeline.vue | 307 ------------------ client/src/pages/spells/single-spell-page.vue | 10 +- client/src/pages/spells/spells-page.vue | 12 +- client/src/pages/timelines/timeline-page.vue | 14 +- 5 files changed, 16 insertions(+), 328 deletions(-) delete mode 100644 client/src/components/timeline/timeline.vue diff --git a/client/src/components/spells/edit-spell-card.vue b/client/src/components/spells/edit-spell-card.vue index 0a3d53d..9806c20 100644 --- a/client/src/components/spells/edit-spell-card.vue +++ b/client/src/components/spells/edit-spell-card.vue @@ -61,6 +61,7 @@ diff --git a/client/src/components/timeline/timeline.vue b/client/src/components/timeline/timeline.vue deleted file mode 100644 index 458c53d..0000000 --- a/client/src/components/timeline/timeline.vue +++ /dev/null @@ -1,307 +0,0 @@ - - - - - \ No newline at end of file diff --git a/client/src/pages/spells/single-spell-page.vue b/client/src/pages/spells/single-spell-page.vue index a6af9a5..dbaf519 100644 --- a/client/src/pages/spells/single-spell-page.vue +++ b/client/src/pages/spells/single-spell-page.vue @@ -1,13 +1,13 @@ \ No newline at end of file diff --git a/client/src/pages/timelines/timeline-page.vue b/client/src/pages/timelines/timeline-page.vue index f940b66..34a2f62 100644 --- a/client/src/pages/timelines/timeline-page.vue +++ b/client/src/pages/timelines/timeline-page.vue @@ -1,18 +1,12 @@ From 0587656ba65e083e3db04684762744a13fea164b Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Fri, 18 Dec 2020 19:34:02 +0100 Subject: [PATCH 2/7] - Store files --- client/src/components/timeline/timeline.vue | 307 ++++++++++++++++++++ client/src/store/index.js | 19 ++ client/src/store/modules/spell.store.js | 23 ++ client/src/store/modules/user.store.js | 59 ++++ 4 files changed, 408 insertions(+) create mode 100644 client/src/components/timeline/timeline.vue create mode 100644 client/src/store/index.js create mode 100644 client/src/store/modules/spell.store.js create mode 100644 client/src/store/modules/user.store.js diff --git a/client/src/components/timeline/timeline.vue b/client/src/components/timeline/timeline.vue new file mode 100644 index 0000000..458c53d --- /dev/null +++ b/client/src/components/timeline/timeline.vue @@ -0,0 +1,307 @@ + + + + + \ No newline at end of file diff --git a/client/src/store/index.js b/client/src/store/index.js new file mode 100644 index 0000000..22d2bb0 --- /dev/null +++ b/client/src/store/index.js @@ -0,0 +1,19 @@ +import Vue from 'vue'; +import Vuex from 'vuex'; + +import VueCookies from 'vue-cookies'; +Vue.use(VueCookies); +Vue.$cookies.config('30d','',''); + +Vue.use(Vuex); + +import user from './modules/user.store'; + +const debug = process.env.NODE_ENV !== 'production'; + +export default new Vuex.Store({ + modules: { + user + }, + strict: debug +}); \ No newline at end of file diff --git a/client/src/store/modules/spell.store.js b/client/src/store/modules/spell.store.js new file mode 100644 index 0000000..0547e28 --- /dev/null +++ b/client/src/store/modules/spell.store.js @@ -0,0 +1,23 @@ +const state = { + +} + +const getters = { + +} + +const actions = { + +} + +const mutations = { + +} + +export default { + namespaced: true, + state, + getters, + actions, + mutations, +} \ No newline at end of file diff --git a/client/src/store/modules/user.store.js b/client/src/store/modules/user.store.js new file mode 100644 index 0000000..80f1e8e --- /dev/null +++ b/client/src/store/modules/user.store.js @@ -0,0 +1,59 @@ +// API +import { RepositoryFactory } from "~/api/repositories" +const Users = RepositoryFactory.get('users') + +export const namespaced = true + +const state = { + status: { + logged: false, + user_token: null, + } +}; + +const getters = { + getUserToken: state => { + if (state.status.logged) { + return state.status.user_token + } else { + return false + } + } +}; + +const mutations = { + loginUser(state, user_token) { + state.status.user_token = user_token; + state.status.logged = true; + }, + logoutUser(state) { + state.status.user_token = null; + state.status.logged = false; + } +}; + +const actions = { + user_login ({ commit }, data) { + return new Promise((resolve, reject) => { + Users.login(data) + .then(v => { + let user_token = v.data.token; + commit('loginUser', user_token); + resolve(user_token); + }) + .catch(() => { + reject(); + }); + }) + }, + user_logout ({ commit }) { + commit('logoutUser'); + } +}; + +export default { + state, + getters, + actions, + mutations, +}; From fa945e45c95ede1826d00ffb9d90468dbb7b9fab Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Fri, 18 Dec 2020 20:34:47 +0100 Subject: [PATCH 3/7] - Fixed my revert hahaha :) --- api/repositories/user-repository.js | 11 +- .../src/components/global/navbar/navbar.vue | 99 ++++++------ client/src/main.js | 35 +---- client/src/pages/user/login-page.vue | 141 +++++++++--------- client/src/pages/user/profile-page.vue | 24 +-- client/src/routes.js | 140 ++++++++--------- 6 files changed, 215 insertions(+), 235 deletions(-) diff --git a/api/repositories/user-repository.js b/api/repositories/user-repository.js index 88c7046..631b8c7 100644 --- a/api/repositories/user-repository.js +++ b/api/repositories/user-repository.js @@ -116,7 +116,8 @@ class UserRepository { .then(newUser => { resolve({ "message": "Account successfully created !", - "user": newUser, + "code": 201, + "token": newUser.uuid, }) }) .catch(err => { @@ -146,11 +147,13 @@ class UserRepository { if (match) { resolve({ "message": "User successfully logged in !", - "user": fetchedUser, + "code": 200, + "token": fetchedUser.uuid, }) } else { reject({ - "message": "Les informations de connexion sont erronées.", + "message": "Authentification attempt failed ; credentials are incorrect.", + "code": 400, }) } }) @@ -166,7 +169,7 @@ class UserRepository { this.getOneByEmail(mail, false) .then(() => { reject({ - "message": "L'email est déjà utilisé par un autre utilisateur.", + "message": "This email is already linked to an account.", "code": 403 }) }) diff --git a/client/src/components/global/navbar/navbar.vue b/client/src/components/global/navbar/navbar.vue index aac51ae..8ae82e0 100644 --- a/client/src/components/global/navbar/navbar.vue +++ b/client/src/components/global/navbar/navbar.vue @@ -1,60 +1,61 @@ diff --git a/client/src/main.js b/client/src/main.js index d92181e..eeadef3 100644 --- a/client/src/main.js +++ b/client/src/main.js @@ -1,9 +1,10 @@ // Core import Vue from 'vue' -import Vuex from 'vuex' import VueRouter from 'vue-router' import App from './app.vue' +import store from './store' + import Globals from './global-components.js' Globals.forEach(component => { Vue.component(component.name, component) @@ -55,38 +56,6 @@ Vue.filter('truncate', filter) import router from './routes' Vue.use(VueRouter) -// Store -Vue.use(Vuex) -let user = Vue.$cookies.get('U_') - -const store = new Vuex.Store({ - state: user ? - { status: { loggedIn: true }, user } - : { status: { loggedIn: false }, user: null }, - mutations: { - loginSucceed (state, user) { - state.status.loggedIn = true - state.user = user - }, - loginFail (state) { - state.status.loggedIn = false - state.user = null - }, - logout(state) { - state.status.loggedIn = false; - state.user = null; - }, - registerSucceed (state, user) { - state.status.loggedIn = true - state.user = user - }, - registerFail (state) { - state.status.loggedIn = false - state.user = null - }, - } -}) - // Mount Vue const app = new Vue({ render: h => h(App), diff --git a/client/src/pages/user/login-page.vue b/client/src/pages/user/login-page.vue index 5317808..0399ded 100644 --- a/client/src/pages/user/login-page.vue +++ b/client/src/pages/user/login-page.vue @@ -1,92 +1,93 @@ \ No newline at end of file diff --git a/client/src/pages/user/profile-page.vue b/client/src/pages/user/profile-page.vue index 61d836c..1aa4071 100644 --- a/client/src/pages/user/profile-page.vue +++ b/client/src/pages/user/profile-page.vue @@ -1,24 +1,25 @@ \ No newline at end of file diff --git a/client/src/routes.js b/client/src/routes.js index fa1f340..9c0a412 100644 --- a/client/src/routes.js +++ b/client/src/routes.js @@ -1,5 +1,5 @@ -import Vue from 'vue' import VueRouter from 'vue-router' +import store from './store' // Pages import Index from "~/pages/index-page" @@ -18,80 +18,84 @@ import Profile from "~/pages/user/profile-page" // Routes const routes = [ - { - path: "*", - redirect: "/", - }, - { - path: '/', - component: Index, - }, - { - path: '/connexion', - component: Login, - meta: { - antiAuthGuard: true - } - }, - { - path: '/inscription', - component: Register, - meta: { - antiAuthGuard: true - } - }, - { - path: '/sorts', - component: Spells, - }, - { - path: '/sorts/:id', - component: SpellSingle, - props: true, - }, - { - path: '/ecoles', - component: Schools, - }, - { - path: '/ecoles/:id', - component: SchoolSingle, - props: true, - }, - { - path: '/ages', - component: Timeline, - }, - { - path: '/profil', - component: Profile, - props: true, - meta: { - authGuard: true, - } - }, + { + path: "*", + redirect: "/", + }, + { + path: '/', + component: Index, + }, + { + path: '/connexion', + component: Login, + meta: { + loginFilter: true + } + }, + { + path: '/inscription', + component: Register, + meta: { + loginFilter: true + } + }, + { + path: '/sorts', + component: Spells, + }, + { + path: '/sorts/:id', + component: SpellSingle, + props: true, + }, + { + path: '/ecoles', + component: Schools, + }, + { + path: '/ecoles/:id', + component: SchoolSingle, + props: true, + }, + { + path: '/ages', + component: Timeline, + }, + { + path: '/profil', + component: Profile, + props: true, + meta: { + logoutFilter: true, + } + }, ] const router = new VueRouter({ - mode: 'history', - routes, - linkActiveClass: "", - linkExactActiveClass: "active", + mode: 'history', + routes, + linkActiveClass: "", + linkExactActiveClass: "active", }) router.beforeEach((to, from, next) => { - if (to.matched.some(record => record.meta.authGuard)) { - if (Vue.$cookies.get('U_') == null) { - next({ - path: '/connexion', - params: { nextUrl: to.fullPath }, - }) - } else { - next() - } - } else { - next() + if (to.matched.some(record => record.meta.loginFilter)) { + if (store.getters.getUserToken) { + next({ + path: '/', + }); } + next(); + } else if (to.matched.some(record => record.meta.logoutFilter)) { + if (!store.getters.getUserToken) { + next({ + path: '/connexion', + }); + } + next(); + } + next(); }) export default router; \ No newline at end of file From eba2669176917a355a03d867be7a442be5ee059c Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Sat, 19 Dec 2020 17:04:22 +0100 Subject: [PATCH 4/7] - Fixed the way components get store data --- api/repositories/user-repository.js | 4 +- client/src/api/usersRepository.js | 15 +- .../src/components/global/navbar/navbar.vue | 8 +- client/src/components/spells/spell-card.vue | 128 +++++++++--------- client/src/main.js | 6 +- client/src/pages/spells/spells-page.vue | 12 +- client/src/pages/user/login-page.vue | 94 ++++++++++++- client/src/pages/user/profile-page.vue | 31 +++-- client/src/pages/user/register-page.vue | 8 +- client/src/routes.js | 39 +++--- client/src/store/modules/user.store.js | 35 +++-- 11 files changed, 245 insertions(+), 135 deletions(-) diff --git a/api/repositories/user-repository.js b/api/repositories/user-repository.js index 631b8c7..9aceec5 100644 --- a/api/repositories/user-repository.js +++ b/api/repositories/user-repository.js @@ -117,7 +117,7 @@ class UserRepository { resolve({ "message": "Account successfully created !", "code": 201, - "token": newUser.uuid, + "user": newUser, }) }) .catch(err => { @@ -148,7 +148,7 @@ class UserRepository { resolve({ "message": "User successfully logged in !", "code": 200, - "token": fetchedUser.uuid, + "user": fetchedUser, }) } else { reject({ diff --git a/client/src/api/usersRepository.js b/client/src/api/usersRepository.js index 2e041a9..dfa0283 100644 --- a/client/src/api/usersRepository.js +++ b/client/src/api/usersRepository.js @@ -3,10 +3,13 @@ import api from './api' const resource = "/users" export default { - login(data) { - return api.post(`${resource}/login`, data) - }, - register(data) { - return api.post(`${resource}`, data) - }, + getOneFromUUID(uuid) { + return api.get(`${resource}/${uuid}`,) + }, + login(data) { + return api.post(`${resource}/login`, data) + }, + register(data) { + return api.post(`${resource}`, data) + }, } \ No newline at end of file diff --git a/client/src/components/global/navbar/navbar.vue b/client/src/components/global/navbar/navbar.vue index 8ae82e0..59a6df3 100644 --- a/client/src/components/global/navbar/navbar.vue +++ b/client/src/components/global/navbar/navbar.vue @@ -10,8 +10,8 @@ {{ link.text }} - @@ -40,21 +40,19 @@ type="password" id="password" class="form-control" - :class="{ - 'is-invalid': errors.passwordEmpty || errors.login, - 'is-valid': submitted && !errors.passwordEmpty }" + :class="{ 'is-invalid': errors.password || errors.login }" autocomplete="current-password"> Nous vous conseillons d'utiliser un mot de passe unique pour cette application seulement. - Veuillez renseigner un mot de passe. + {{ errors.password }} @@ -98,50 +96,55 @@ export default { submitted: false, errors: { - mailEmpty: "", - passwordEmpty: "", + email: "", + password: "", login: "", } } }, methods: { async logUser(e) { + e.preventDefault(); + // Resets old errors if any + for (const o in this.errors) { + this.errors[o] = "" + } this.submitted = true; - this.errors = this.resetErrors(); - let userInput = { "mail": this.email, "password": this.password }; - if (this.email && this.password) { - this.$store.dispatch('user_login', userInput) - .then(() => { - this.$router.push('/profil'); - }) - .catch(() => { - this.errors.login = "Erreur d'authentification. Les identifiants ne correspondent pas." - }); - } else { - if (!userInput.mail) { - this.errors.mailEmpty = "Veuillez entrer une addresse mail valide." - } - if (!userInput.password) { - this.errors.passwordEmpty = "Veuillez entrer un mot de passe." + 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." + } + + // Stops the method if any errors exist + for (const o in this.errors) { + if (this.errors[o] != "") { + this.submitted = false; } } - e.preventDefault(); - }, - resetErrors() { - return { - mailEmpty: "", - passwordEmpty: "", - login: "", + if (this.submitted) { + this.$store.dispatch('user_login', userInput) + .then(() => { + this.$router.push('/profil') + }) + .catch(err => { + if (err.status === 404) { + this.errors.email = err.data.error; + } else { + this.errors.login = "Une erreur inconnue est survenue, rééssayez plus tard."; + } + }); } - } + }, } } diff --git a/client/src/pages/user/register-page.vue b/client/src/pages/user/register-page.vue index db9a38d..e493d30 100644 --- a/client/src/pages/user/register-page.vue +++ b/client/src/pages/user/register-page.vue @@ -1,143 +1,193 @@ diff --git a/client/src/store/modules/user.store.js b/client/src/store/modules/user.store.js index c3a8824..33424a9 100644 --- a/client/src/store/modules/user.store.js +++ b/client/src/store/modules/user.store.js @@ -14,6 +14,7 @@ const state = { }; const getters = { + getUserProfile: state => { if (state.status.logged) { return state.status.profile @@ -28,9 +29,14 @@ const mutations = { state.status.profile = user; state.status.logged = true; }, + logoutUser(state) { state.status.profile = null; state.status.logged = false; + }, + + registerUser() { + } }; @@ -44,14 +50,28 @@ const actions = { commit('loginUser', user); resolve(user); }) - .catch(() => { - reject(); + .catch(err => { + reject(err.response); }); }) }, + user_logout ({ commit }) { // cookie.remove('user_token'); commit('logoutUser'); + }, + + user_register ({ commit }, data) { + return new Promise((resolve, reject) => { + Users.register(data) + .then(() => { + commit('registerUser'); + resolve(); + }) + .catch(err => { + reject(err.response); + }); + }) } }; diff --git a/client/vue.config.js b/client/vue.config.js index e18e3db..9c7389f 100644 --- a/client/vue.config.js +++ b/client/vue.config.js @@ -1,20 +1,20 @@ const path = require('path') module.exports = { - configureWebpack: { - resolve: { - alias: { - "~": path.resolve(__dirname, 'src/') - } - } - }, - css: { - loaderOptions: { - scss: { - prependData: ` - @import "@/assets/scss/_variables.scss"; - ` - } - } + configureWebpack: { + resolve: { + alias: { + "~": path.resolve(__dirname, 'src/') + } } + }, + css: { + loaderOptions: { + scss: { + prependData: ` + @import "@/assets/scss/_variables.scss"; + ` + } + } + } }; \ No newline at end of file