From 4b0428212d482edb9fa2d93b6926180a4a4db1aa Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sun, 17 Jun 2018 11:12:11 -0400 Subject: [PATCH] fix: client login --- client/components/login.vue | 64 +++++---- client/graph/login-mutation-login.gql | 4 +- client/graph/login-query-strategies.gql | 2 +- client/static/svg/henry-reading.svg | 136 +++++++------------- server/db/models/users.js | 4 +- server/graph/resolvers/authentication.js | 2 +- server/graph/schemas/authentication.graphql | 4 +- server/jobs/sync-graph-locales.js | 2 +- server/middlewares/auth.js | 2 +- 9 files changed, 91 insertions(+), 129 deletions(-) diff --git a/client/components/login.vue b/client/components/login.vue index 7bfcc97a..4af46123 100644 --- a/client/components/login.vue +++ b/client/components/login.vue @@ -86,10 +86,12 @@ export default { } }, mounted () { - this.refreshStrategies() this.$refs.iptEmail.focus() }, methods: { + /** + * SELECT STRATEGY + */ selectStrategy (key, useForm) { this.selectedStrategy = key this.screen = 'login' @@ -100,28 +102,10 @@ export default { this.$refs.iptEmail.focus() } }, - refreshStrategies () { - this.isLoading = true - this.$apollo.query({ - query: strategiesQuery - }).then(resp => { - if (_.has(resp, 'data.authentication.providers')) { - this.strategies = _.get(resp, 'data.authentication.providers', []) - } else { - throw new Error('No authentication providers available!') - } - this.isLoading = false - }).catch(err => { - console.error(err) - this.$store.commit('showNotification', { - style: 'red', - message: err.message, - icon: 'warning' - }) - this.isLoading = false - }) - }, - login () { + /** + * LOGIN + */ + async login () { if (this.username.length < 2) { this.$store.commit('showNotification', { style: 'red', @@ -138,14 +122,15 @@ export default { this.$refs.iptPassword.focus() } else { this.isLoading = true - this.$apollo.mutate({ - mutation: loginMutation, - variables: { - username: this.username, - password: this.password, - provider: this.selectedStrategy - } - }).then(resp => { + try { + let resp = await this.$apollo.mutate({ + mutation: loginMutation, + variables: { + username: this.username, + password: this.password, + strategy: this.selectedStrategy + } + }) if (_.has(resp, 'data.authentication.login')) { let respObj = _.get(resp, 'data.authentication.login', {}) if (respObj.responseResult.succeeded === true) { @@ -173,7 +158,7 @@ export default { } else { throw new Error('Authentication is unavailable.') } - }).catch(err => { + } catch (err) { console.error(err) this.$store.commit('showNotification', { style: 'red', @@ -181,9 +166,12 @@ export default { icon: 'warning' }) this.isLoading = false - }) + } } }, + /** + * VERIFY TFA CODE + */ verifySecurityCode () { if (this.securityCode.length !== 6) { this.$store.commit('showNotification', { @@ -230,6 +218,16 @@ export default { }) } } + }, + apollo: { + strategies: { + query: strategiesQuery, + update: (data) => data.authentication.strategies, + watchLoading (isLoading) { + this.isLoading = isLoading + this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'login-strategies-refresh') + } + } } } diff --git a/client/graph/login-mutation-login.gql b/client/graph/login-mutation-login.gql index 89fa760f..2e857b7f 100644 --- a/client/graph/login-mutation-login.gql +++ b/client/graph/login-mutation-login.gql @@ -1,6 +1,6 @@ -mutation($username: String!, $password: String!, $provider: String!) { +mutation($username: String!, $password: String!, $strategy: String!) { authentication { - login(username: $username, password: $password, provider: $provider) { + login(username: $username, password: $password, strategy: $strategy) { responseResult { succeeded errorCode diff --git a/client/graph/login-query-strategies.gql b/client/graph/login-query-strategies.gql index 446b571d..85f95e10 100644 --- a/client/graph/login-query-strategies.gql +++ b/client/graph/login-query-strategies.gql @@ -1,6 +1,6 @@ query { authentication { - providers( + strategies( filter: "isEnabled eq true", orderBy: "title ASC" ) { diff --git a/client/static/svg/henry-reading.svg b/client/static/svg/henry-reading.svg index 6e17323a..254e9ccc 100644 --- a/client/static/svg/henry-reading.svg +++ b/client/static/svg/henry-reading.svg @@ -1,87 +1,51 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/server/db/models/users.js b/server/db/models/users.js index 5e1131fc..14af0799 100644 --- a/server/db/models/users.js +++ b/server/db/models/users.js @@ -171,13 +171,13 @@ module.exports = class User extends Model { } static async login (opts, context) { - if (_.has(WIKI.config.auth.strategies, opts.provider)) { + if (_.has(WIKI.auth.strategies, opts.strategy)) { _.set(context.req, 'body.email', opts.username) _.set(context.req, 'body.password', opts.password) // Authenticate return new Promise((resolve, reject) => { - WIKI.auth.passport.authenticate(opts.provider, async (err, user, info) => { + WIKI.auth.passport.authenticate(opts.strategy, async (err, user, info) => { if (err) { return reject(err) } if (!user) { return reject(new WIKI.Error.AuthLoginFailed()) } diff --git a/server/graph/resolvers/authentication.js b/server/graph/resolvers/authentication.js index da578a00..5f88a566 100644 --- a/server/graph/resolvers/authentication.js +++ b/server/graph/resolvers/authentication.js @@ -16,7 +16,7 @@ module.exports = { }, AuthenticationQuery: { async strategies(obj, args, context, info) { - let strategies = await WIKI.db.authentication.query().orderBy('title') + let strategies = await WIKI.db.authentication.getEnabledStrategies() strategies = strategies.map(stg => ({ ...stg, config: _.transform(stg.config, (res, value, key) => { diff --git a/server/graph/schemas/authentication.graphql b/server/graph/schemas/authentication.graphql index 0577cbda..60e4a529 100644 --- a/server/graph/schemas/authentication.graphql +++ b/server/graph/schemas/authentication.graphql @@ -29,7 +29,7 @@ type AuthenticationMutation { login( username: String! password: String! - provider: String! + strategy: String! ): AuthenticationLoginResponse loginTFA( @@ -38,7 +38,7 @@ type AuthenticationMutation { ): DefaultResponse updateStrategy( - provider: String! + strategy: String! isEnabled: Boolean! config: [KeyValuePairInput] ): DefaultResponse diff --git a/server/jobs/sync-graph-locales.js b/server/jobs/sync-graph-locales.js index 81c53190..15ef1330 100644 --- a/server/jobs/sync-graph-locales.js +++ b/server/jobs/sync-graph-locales.js @@ -38,7 +38,7 @@ module.exports = async (job) => { // -> Download locale strings - if (WIKI.config.langAutoUpdate) { + if (WIKI.config.lang.autoUpdate) { const respStrings = await apollo({ query: `query ($code: String!) { localization { diff --git a/server/middlewares/auth.js b/server/middlewares/auth.js index 866f76e4..09e0030a 100644 --- a/server/middlewares/auth.js +++ b/server/middlewares/auth.js @@ -7,7 +7,7 @@ module.exports = (req, res, next) => { // Is user authenticated ? if (!req.isAuthenticated()) { - if (WIKI.config.auth.public !== true) { + if (WIKI.config.public !== true) { return res.redirect('/login') } else { // req.user = rights.guest