From 1735767cce17d5976eaa414cc9e91c75f36e1d6d Mon Sep 17 00:00:00 2001 From: Grzegorz Leoniec Date: Wed, 17 Oct 2018 19:57:24 +0200 Subject: [PATCH] added jwt login and authentication plus some toast --- assets/styles/imports/_toast.scss | 32 +++++++ assets/styles/main.scss | 1 + components/Author.vue | 38 ++++++-- components/FollowButton.vue | 8 +- layouts/default.vue | 19 ++++ middleware/authenticated.js | 26 +++++ nuxt.config.js | 56 +++++++++-- package.json | 8 +- pages/index.vue | 7 +- pages/login.vue | 47 ++++++++- pages/logout.vue | 39 ++++++++ pages/profile/_slug.vue | 11 ++- pages/profile/index.vue | 8 ++ plugins/izi-toast.js | 13 +++ store/auth.js | 152 ++++++++++++++++++++++++++++++ store/index.js | 9 ++ yarn.lock | 87 +++++++++++++++++ 17 files changed, 523 insertions(+), 38 deletions(-) create mode 100644 assets/styles/imports/_toast.scss create mode 100644 middleware/authenticated.js create mode 100644 pages/logout.vue create mode 100644 pages/profile/index.vue create mode 100644 plugins/izi-toast.js create mode 100644 store/auth.js create mode 100644 store/index.js diff --git a/assets/styles/imports/_toast.scss b/assets/styles/imports/_toast.scss new file mode 100644 index 000000000..0ef81a5b2 --- /dev/null +++ b/assets/styles/imports/_toast.scss @@ -0,0 +1,32 @@ +.iziToast-target, .iziToast { + &, + &:after, + &.iziToast-color-dark:after { + box-shadow: none !important; + } +} + +.iziToast .iziToast-message { + font-weight: 400 !important; +} + +.iziToast.iziToast-color-red { + background: $color-danger !important; + border-color: $color-danger !important; +} +.iziToast.iziToast-color-orange { + background: $color-warning !important; + border-color: $color-warning !important; +} +.iziToast.iziToast-color-yellow { + background: $color-yellow !important; + border-color: $color-yellow !important; +} +.iziToast.iziToast-color-blue { + background: $color-secondary !important; + border-color: $color-secondary !important; +} +.iziToast.iziToast-color-green { + background: $color-success !important; + border-color: $color-success !important; +} diff --git a/assets/styles/main.scss b/assets/styles/main.scss index e44241d88..e7a456a08 100644 --- a/assets/styles/main.scss +++ b/assets/styles/main.scss @@ -1,5 +1,6 @@ @import 'vue-cion-design-system/src/system/tokens/generated/tokens.scss'; @import './imports/_tooltip.scss'; +@import './imports/_toast.scss'; // Transition Easing $easeOut: cubic-bezier(0.19, 1, 0.22, 1); diff --git a/components/Author.vue b/components/Author.vue index ec891f5ae..fc7e01518 100644 --- a/components/Author.vue +++ b/components/Author.vue @@ -21,7 +21,22 @@ style="min-width: 250px;" @mouseover="popoverMouseEnter" @mouseleave="popoveMouseLeave"> + + + + {{ fanCount }} + Fans + + Kommentare - - - {{ fanCount }} - Fans - - @@ -100,6 +106,9 @@ export default { } }, computed: { + itsMe() { + return this.author.slug === this.$store.getters['auth/user'].slug + }, fanCount() { let count = Number(this.author.followedByCount) || 0 if (this.voted) { @@ -145,3 +154,12 @@ export default { } } + + diff --git a/components/FollowButton.vue b/components/FollowButton.vue index a174fac80..790a86d12 100644 --- a/components/FollowButton.vue +++ b/components/FollowButton.vue @@ -1,6 +1,7 @@ + + diff --git a/middleware/authenticated.js b/middleware/authenticated.js new file mode 100644 index 000000000..af2a7076c --- /dev/null +++ b/middleware/authenticated.js @@ -0,0 +1,26 @@ +import { isEmpty } from 'lodash' + +export default async ({ store, env, route, redirect }) => { + let publicPages = env.publicPages + // only affect non public pages + if (publicPages.indexOf(route.name) >= 0) { + return true + } + + // await store.dispatch('auth/refreshJWT', 'authenticated middleware') + const isAuthenticated = await store.dispatch('auth/check') + if (isAuthenticated === true) { + return true + } + + // try to logout user + // await store.dispatch('auth/logout', null, { root: true }) + + // set the redirect path for after the login + let params = {} + if (!isEmpty(route.path) && route.path !== '/') { + params.path = route.path + } + + return redirect('/login', params) +} diff --git a/nuxt.config.js b/nuxt.config.js index 6959ab7e1..832d87e4a 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -1,4 +1,17 @@ const pkg = require('./package') +const envWhitelist = [ + 'BUILD_COMMIT', + 'NODE_ENV', + 'WEBAPP_HOST', + 'WEBAPP_PORT', + 'WEBAPP_BASE_URL', + 'API_HOST', + 'API_PORT', + 'EMBED_API_URL', + 'SENTRY_DNS_PUBLIC', + 'MAPBOX_TOKEN', + 'MAINTENANCE' +] module.exports = { mode: 'universal', @@ -7,6 +20,19 @@ module.exports = { name: 'slide-up', mode: 'out-in' }, + + env: { + // pages which do NOT require a login + publicPages: [ + 'login', + 'logout', + 'register', + 'signup', + 'reset', + 'reset-token', + 'pages-slug' + ] + }, /* ** Headers of the page */ @@ -42,15 +68,12 @@ module.exports = { { src: '~/plugins/design-system.js', ssr: true }, { src: '~/plugins/vue-directives.js', ssr: false }, { src: '~/plugins/v-tooltip.js', ssr: false }, + { src: '~/plugins/izi-toast.js', ssr: false }, { src: '~/plugins/vue-filters.js' } ], router: { - // middleware: [ - // 'maintenance', - // 'check-auth', - // 'authenticated' - // ], + middleware: ['authenticated'], linkActiveClass: 'router-active-link' }, /* router: { @@ -81,14 +104,17 @@ module.exports = { /* ** Nuxt.js modules */ - modules: ['@nuxtjs/apollo'], + modules: [ + '@nuxtjs/apollo', + ['@nuxtjs/dotenv', { only: envWhitelist }], + ['nuxt-env', { keys: envWhitelist }] + ], // Give apollo module options apollo: { - tokenName: 'yourApolloTokenName', // optional, default: apollo-token - tokenExpires: 10, // optional, default: 7 (days) - includeNodeModules: true, // optional, default: false (this includes graphql-tag for node_modules folder) - authenticationType: 'Basic', // optional, default: 'Bearer' + // tokenName: 'yourApolloTokenName', // optional, default: apollo-token + tokenExpires: 1, // optional, default: 7 (days) + // includeNodeModules: true, // optional, default: false (this includes graphql-tag for node_modules folder) // optional errorHandler(error) { console.log( @@ -97,6 +123,14 @@ module.exports = { error.message ) }, + + // Watch loading state for all queries + // See 'Smart Query > options > watchLoading' for detail + // TODO: find a way to get this working + // watchLoading(isLoading) { + // console.log('Global loading', countModifier) + // this.$nuxt.$loading.start() + // }, // required clientConfigs: { default: { @@ -107,6 +141,8 @@ module.exports = { httpLinkOptions: { credentials: 'same-origin' }, + credentials: true, + // You can use `wss` for secure connection (recommended in production) // Use `null` to disable subscriptions // wsEndpoint: 'ws://localhost:4000', // optional diff --git a/package.json b/package.json index 10ab719ce..8b738f630 100644 --- a/package.json +++ b/package.json @@ -12,18 +12,22 @@ "generate": "nuxt generate", "lint": "eslint --ext .js,.vue --ignore-path .gitignore .", "styleguide": "cd ./styleguide && yarn dev", - "styleguide:build": "cd ./styleguide && yarn build:lib && cd ../ && yarn upgrade file:./styleguide", + "styleguide:build": "cd ./styleguide && yarn build:lib && cd ../ && yarn upgrade --ignore-engines file:./styleguide", "precommit": "yarn lint" }, "dependencies": { "@nuxtjs/apollo": "^4.0.0-rc3", + "@nuxtjs/dotenv": "^1.3.0", "cross-env": "^5.2.0", "date-fns": "^1.29.0", "express": "^4.16.3", "graphql-tag": "^2.10.0", + "jsonwebtoken": "^8.3.0", "nuxt": "^2.0.0", + "nuxt-env": "^0.0.4", "v-tooltip": "^2.0.0-rc.33", - "vue-cion-design-system": "file:./styleguide" + "vue-cion-design-system": "file:./styleguide", + "vue-izitoast": "^1.1.0" }, "devDependencies": { "babel-eslint": "^10.0.1", diff --git a/pages/index.vue b/pages/index.vue index ebcf4666f..1a557c705 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -71,12 +71,7 @@ export default { shoutedCount } } - `), - variables() { - return { - id: this.$route.query.post || 'p1' - } - } + `) } } } diff --git a/pages/login.vue b/pages/login.vue index 677993048..44a9108c8 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -11,7 +11,7 @@ Wenn Du ein Konto bei Human Connection hast, melde Dich bitte hier an. -
+ Anmelden @@ -57,8 +65,39 @@ diff --git a/pages/logout.vue b/pages/logout.vue new file mode 100644 index 000000000..7927be45f --- /dev/null +++ b/pages/logout.vue @@ -0,0 +1,39 @@ + + + diff --git a/pages/profile/_slug.vue b/pages/profile/_slug.vue index 2b21f7754..2ac3cccc3 100644 --- a/pages/profile/_slug.vue +++ b/pages/profile/_slug.vue @@ -35,8 +35,9 @@ + @update="voted = true && fetchUser()" /> @@ -49,7 +50,7 @@ Wem folgt {{ user.name | truncate(15) }}? -