From 3b05f4646f3e2a5d7c153a05b17d983d2293b07b Mon Sep 17 00:00:00 2001
From: Maxim Filippov <colixer@gmail.com>
Date: Fri, 13 Sep 2019 23:45:00 +0300
Subject: [PATCH] Add "login-pleroma" link

---
 CHANGELOG.md                |  3 ++-
 src/permission.js           |  2 +-
 src/router/index.js         |  5 +++++
 src/services/pleromaAuth.js | 15 +++++++++++++++
 src/views/login/index.vue   |  5 +++--
 src/views/login/pleroma.vue | 26 ++++++++++++++++++++++++++
 6 files changed, 52 insertions(+), 4 deletions(-)
 create mode 100644 src/services/pleromaAuth.js
 create mode 100644 src/views/login/pleroma.vue

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58c433f1..276a2236 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,12 +9,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 ### Added
 
 - add ability to configure new settings (UploadS3 bucket namespace, Rate limit for Activity pub routes, Email notifications settings, MRF Vocabulary, user bio and name length and others)
+- add sign in via PleromaFE
 
 ### Changed
 
 - removes "Dashboard" from dropdown menu
 - makes all single selects clearable and allow to enter custom values in all multiple selects
-- remove legacy activitypub accept_blocks setting 
+- remove legacy activitypub accept_blocks setting
 
 ### Fixed
 
diff --git a/src/permission.js b/src/permission.js
index 7dbf3c47..764c666e 100644
--- a/src/permission.js
+++ b/src/permission.js
@@ -14,7 +14,7 @@ function hasPermission(roles, permissionRoles) {
   return roles.some(role => permissionRoles.indexOf(role) >= 0)
 }
 
-const whiteList = ['/login', '/auth-redirect']// no redirect whitelist
+const whiteList = ['/login', '/auth-redirect', '/login-pleroma']// no redirect whitelist
 
 export const beforeEachRoute = (to, from, next) => {
   NProgress.start() // start progress bar
diff --git a/src/router/index.js b/src/router/index.js
index e215ca4b..7ffbb61c 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -18,6 +18,11 @@ export const constantRouterMap = [
       }
     ]
   },
+  {
+    path: '/login-pleroma',
+    component: () => import('@/views/login/pleroma'),
+    hidden: true
+  },
   {
     path: '/login',
     component: () => import('@/views/login/index'),
diff --git a/src/services/pleromaAuth.js b/src/services/pleromaAuth.js
new file mode 100644
index 00000000..9008f5c0
--- /dev/null
+++ b/src/services/pleromaAuth.js
@@ -0,0 +1,15 @@
+import localforage from 'localforage'
+import _ from 'lodash'
+
+const pleromaFEStateKey = 'vuex-lz'
+
+export const authenticateWithPleromaFE = async(store) => {
+  const pleromaFEState = await localforage.getItem(pleromaFEStateKey)
+  const token = _.get(pleromaFEState, 'oauth.userToken')
+
+  if (token === undefined) {
+    throw new Error('PleromaFE token not found')
+  }
+
+  await store.dispatch('LoginByPleromaFE', { token })
+}
diff --git a/src/views/login/index.vue b/src/views/login/index.vue
index fdb00fe2..875bbf46 100644
--- a/src/views/login/index.vue
+++ b/src/views/login/index.vue
@@ -53,6 +53,7 @@ import SvgIcon from '@/components/SvgIcon'
 import localforage from 'localforage'
 import _ from 'lodash'
 import i18n from '@/lang'
+import { authenticateWithPleromaFE } from '@/services/pleromaAuth'
 
 export default {
   name: 'Login',
@@ -112,16 +113,16 @@ export default {
     async handlePleromaFELogin() {
       this.loadingPleromaFE = true
       try {
-        await this.$store.dispatch('LoginByPleromaFE', { token: this.pleromaFEState.oauth.userToken })
+        await authenticateWithPleromaFE(this.$store)
       } catch (error) {
         this.loadingPleromaFE = false
         this.$message.error(i18n.t('login.pleromaFELoginFailed'))
       }
 
       this.loadingPleromaFE = false
-      this.$router.push({ path: this.redirect || '/users/index' })
 
       this.$message.success(i18n.t('login.pleromaFELoginSucceed'))
+      this.$router.push({ path: this.redirect || '/users/index' })
     },
     getLoginData() {
       const [username, authHost] = this.loginForm.username.split('@')
diff --git a/src/views/login/pleroma.vue b/src/views/login/pleroma.vue
new file mode 100644
index 00000000..9b6a56d0
--- /dev/null
+++ b/src/views/login/pleroma.vue
@@ -0,0 +1,26 @@
+<template>
+  <div />
+</template>
+
+<script>
+import { Loading } from 'element-ui'
+import { authenticateWithPleromaFE } from '@/services/pleromaAuth'
+import i18n from '@/lang'
+
+export default {
+  name: 'LoginPleroma',
+  async mounted() {
+    const loadingInstance = Loading.service({ fullscreen: true })
+
+    try {
+      await authenticateWithPleromaFE(this.$store)
+    } catch (error) {
+      this.$message.error(i18n.t('login.pleromaFELoginFailed'))
+    }
+
+    loadingInstance.close()
+    this.$router.push({ path: '/users/index' })
+    this.$message.success(i18n.t('login.pleromaFELoginSucceed'))
+  }
+}
+</script>
-- 
GitLab