From 449ec98b11e9a7a7ab038a1ee8826137855eb46c Mon Sep 17 00:00:00 2001 From: freesgen Date: Thu, 5 Nov 2020 10:16:43 -0400 Subject: [PATCH 1/2] integrate sdk auth with kanvas --- .eslintrc.js | 3 ++- src/config/index.js | 1 + src/config/kanvasSDK.js | 22 +++++++++++++++++++ src/mixins/auth.js | 43 +++++++++++++++++++++++++------------- src/views/auth/login.vue | 24 ++++++++++++++++++++- src/views/auth/sign-up.vue | 24 ++++++++++++++++++++- 6 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 src/config/kanvasSDK.js diff --git a/.eslintrc.js b/.eslintrc.js index c37402d..b014052 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -85,6 +85,7 @@ module.exports = { accounting: true, axios: true, moment: true, - window: true + window: true, + kanvasSDK: true } } diff --git a/src/config/index.js b/src/config/index.js index fb5def8..77fe2a3 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -5,6 +5,7 @@ import "@/config/cookies"; import "@/config/vee-validate"; import "@/config/sentry"; import "@/config/element-ui"; +import "@/config/kanvasSDK"; import * as filters from "@/utils/filters"; import methods from "@/utils/methods"; diff --git a/src/config/kanvasSDK.js b/src/config/kanvasSDK.js new file mode 100644 index 0000000..823d482 --- /dev/null +++ b/src/config/kanvasSDK.js @@ -0,0 +1,22 @@ +import KanvasSDK from "client-sdk"; +import store from "@/store"; + +const kanvasSDK = new KanvasSDK({ + appKey: process.env.VUE_APPKEY, + endpoint: process.env.VUE_APP_BASE_API_URL, + domain: process.env.VUE_APP_DOMAIN, + cookies: true +}) + +kanvasSDK.auth.on("loggedIn", (token) => { + store.dispatch("User/setToken", token); +}) + +kanvasSDK.auth.on("loggedOut", () => { + store.dispatch("User/setToken", null); +}) + +window.kanvasSDK = kanvasSDK; + +export default kanvasSDK; + diff --git a/src/mixins/auth.js b/src/mixins/auth.js index d2d0c89..53ec3e8 100644 --- a/src/mixins/auth.js +++ b/src/mixins/auth.js @@ -1,4 +1,5 @@ import AuthContainer from "@v/auth/container"; +import { mapState } from "vuex"; export default { components: { @@ -12,7 +13,15 @@ export default { } } }, + data() { + return { + isLoading: false + } + }, computed: { + ...mapState({ + isLoading: state => state.Application.isLoading + }), allowUserRegistration() { return this.appSettings.settings && Boolean(Number(this.appSettings.settings.allow_user_registration)); }, @@ -33,11 +42,13 @@ export default { } }, methods: { - handleResponse({ data }, isSignup = false) { - const auth = isSignup ? data.session : data; - - Cookies.set("token", auth.token, { expires: new Date(auth.expires), path: "/", domain: process.env.VUE_APP_DOMAIN }); - this.$store.dispatch("User/setToken", auth.token); + handleResponse(responseData, isSignup = false) { + if (responseData) { + // support old modules + const auth = isSignup ? responseData.session : responseData.data; + Cookies.set("token", auth.token, { expires: new Date(auth.expires), path: "/", domain: process.env.VUE_APP_DOMAIN }); + this.$store.dispatch("User/setToken", auth.token); + } if (isSignup) { this.$modal.show("after-signup-wizard"); @@ -47,33 +58,37 @@ export default { this.$router.replace({ name: "dashboard" }); this.$store.dispatch("Application/setIsLoading", false); }, - prepareData() { - const data = new FormData(); + prepareData(asObject) { + const data = {}; Object.keys(this.form.fields).forEach((field) => { const apiField = this.form.fields[field].map || field; - data.append(apiField, this.form.fields[field].value); + data[apiField] = this.form.fields[field].value; }); - + if (!asObject) { + const formData = new FormData(); + Object.entries(data).forEach(([key, value]) => { + formData.append(key, value) + }) + } return data; }, - submitData(isSignup) { + async submitData(isSignup) { const data = this.prepareData(); - axios({ url: `${this.form.endpoint}`, method: "POST", data - }).then((response) => { - this.handleResponse(response, isSignup); + }).then(({ data }) => { + this.handleResponse(data, isSignup); }).catch((error) => { this.$notify({ title: "Error", text: error.response.data.errors.message, type: "error" }); - }); + }) }, validateInvitation() { axios({ diff --git a/src/views/auth/login.vue b/src/views/auth/login.vue index b730798..c0a0bff 100644 --- a/src/views/auth/login.vue +++ b/src/views/auth/login.vue @@ -39,8 +39,9 @@ {{ errors.first("password") }} -
@@ -89,6 +90,27 @@ export default { endpoint: "/auth" } } + }, + methods: { + submitData() { + if (!this.isLoading) { + this.isLoading = true; + const data = this.prepareData(true); + kanvasSDK.auth.login(data.email, data.password) + .then(() => { + this.handleResponse() + }) + .catch(error => { + this.$notify({ + title: "Error", + text: error.errors.message, + type: "error" + }); + }).finally(() => { + this.isLoading = false; + }) + } + } } } diff --git a/src/views/auth/sign-up.vue b/src/views/auth/sign-up.vue index 65fa4d7..036e30e 100644 --- a/src/views/auth/sign-up.vue +++ b/src/views/auth/sign-up.vue @@ -91,8 +91,9 @@
{{ errors.first("company") }} -
Already have an account? @@ -151,6 +152,27 @@ export default { endpoint: "/users" } } + }, + methods: { + submitData() { + if (!this.isLoading) { + this.isLoading = true; + const data = this.prepareData(true); + kanvasSDK.auth.register(data) + .then(() => { + this.handleResponse(null, true) + }) + .catch(error => { + this.$notify({ + title: "Error", + text: error.errors.message, + type: "error" + }); + }).finally(() => { + this.isLoading = false; + }) + } + } } } From 507a731169e1652b3e89299679ec57a2e009eddd Mon Sep 17 00:00:00 2001 From: freesgen Date: Tue, 10 Nov 2020 10:30:58 -0400 Subject: [PATCH 2/2] Add reset password integration --- src/mixins/auth.js | 3 --- src/views/auth/forgot-password.vue | 24 ++++++++++++++++++++++-- src/views/auth/reset-password.vue | 24 ++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/mixins/auth.js b/src/mixins/auth.js index 53ec3e8..3eb662a 100644 --- a/src/mixins/auth.js +++ b/src/mixins/auth.js @@ -19,9 +19,6 @@ export default { } }, computed: { - ...mapState({ - isLoading: state => state.Application.isLoading - }), allowUserRegistration() { return this.appSettings.settings && Boolean(Number(this.appSettings.settings.allow_user_registration)); }, diff --git a/src/views/auth/forgot-password.vue b/src/views/auth/forgot-password.vue index 7fae30f..c55e4de 100644 --- a/src/views/auth/forgot-password.vue +++ b/src/views/auth/forgot-password.vue @@ -62,10 +62,10 @@ export default { } }, methods: { - handleResponse(response) { + handleResponse(data) { this.$notify({ title: "Confirmation", - text: response.data, + text: data, type: "success" }); @@ -74,6 +74,26 @@ export default { this.$router.push({ name: "login" }); + }, + + submitData() { + if (!this.isLoading) { + this.isLoading = true; + const data = this.prepareData(true); + kanvasSDK.auth.sendPasswordResetEmail(data.email) + .then((responseData) => { + this.handleResponse(responseData) + }) + .catch(error => { + this.$notify({ + title: "Error", + text: error.errors.message, + type: "error" + }); + }).finally(() => { + this.isLoading = false; + }) + } } } } diff --git a/src/views/auth/reset-password.vue b/src/views/auth/reset-password.vue index 6cd0509..e32b9f2 100644 --- a/src/views/auth/reset-password.vue +++ b/src/views/auth/reset-password.vue @@ -79,16 +79,36 @@ export default { } }, methods: { - handleResponse(response) { + handleResponse(data) { this.$notify({ title: "Confirmation", - text: response.data, + text: data, type: "success" }); this.$router.push({ name: "login" }); + }, + + submitData() { + if (!this.isLoading) { + this.isLoading = true; + const data = this.prepareData(true); + kanvasSDK.auth.resetPassword(data.new_password, data.verify_password,this.$route.params.resetKey) + .then((responseData) => { + this.handleResponse(responseData) + }) + .catch(error => { + this.$notify({ + title: "Error", + text: error.errors.message, + type: "error" + }); + }).finally(() => { + this.isLoading = false; + }) + } } } }