|
| 1 | +<!-- |
| 2 | + - @copyright Copyright (c) 2021 Sorunome <mail@sorunome.de> |
| 3 | + - @copyright Copyright (c) 2020-2021 Gary Kim <gary@garykim.dev> |
| 4 | + - @copyright Copyright (c) 2020 Samuel Llamzon |
| 5 | + - |
| 6 | + - @author Sorunome <mail@sorunome.de> |
| 7 | + - |
| 8 | + - @license GNU AGPL version 3 or any later version |
| 9 | + - |
| 10 | + - This program is free software: you can redistribute it and/or modify |
| 11 | + - it under the terms of the GNU Affero General Public License as |
| 12 | + - published by the Free Software Foundation, either version 3 of the |
| 13 | + - License, or (at your option) any later version. |
| 14 | + - |
| 15 | + - This program is distributed in the hope that it will be useful, |
| 16 | + - but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + - GNU Affero General Public License for more details. |
| 19 | + - |
| 20 | + - You should have received a copy of the GNU Affero General Public License |
| 21 | + - along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + - |
| 23 | + --> |
| 24 | + |
| 25 | +<template> |
| 26 | + <div class="section"> |
| 27 | + <SettingsSection |
| 28 | + :title="t('riotchat', 'Matrix Login')" |
| 29 | + :description="t('riotchat', 'Log in to your Matrix Account')" |
| 30 | + > |
| 31 | + <form @submit="login"> |
| 32 | + <label |
| 33 | + ref="matrix_username" |
| 34 | + for="matrix_username" |
| 35 | + >{{ t('riotchat', 'Matrix Username:') }}</label> |
| 36 | + <input |
| 37 | + id="matrix_username" |
| 38 | + v-model="matrix_username" |
| 39 | + type="text" |
| 40 | + > |
| 41 | + <br> |
| 42 | + <label |
| 43 | + ref="matrix_password" |
| 44 | + for="matrix_password" |
| 45 | + >{{ t('riotchat', 'Matrix Password:') }}</label> |
| 46 | + <input |
| 47 | + id="matrix_password" |
| 48 | + v-model="matrix_password" |
| 49 | + type="text" |
| 50 | + > |
| 51 | + <input type="submit" value="login"> |
| 52 | + <br> |
| 53 | + </form> |
| 54 | + <template v-if="loggedIn"> |
| 55 | + <p>{{ currentlyLoggedInMessage }}</p> |
| 56 | + <input type="button" @submit="logout"> |
| 57 | + </template> |
| 58 | + </SettingsSection> |
| 59 | + </div> |
| 60 | +</template> |
| 61 | + |
| 62 | +<script> |
| 63 | +import Axios from '@nextcloud/axios'; |
| 64 | +import { showError, showSuccess } from '@nextcloud/dialogs'; |
| 65 | +import { generateUrl } from '@nextcloud/router'; |
| 66 | +import { loadState } from '@nextcloud/initial-state'; |
| 67 | +import { SettingsSection, Tooltip } from '@nextcloud/vue'; |
| 68 | +
|
| 69 | +export default { |
| 70 | + name: "PersonalSettings", |
| 71 | + components: { |
| 72 | + SettingsSection, |
| 73 | + }, |
| 74 | + directives: { |
| 75 | + Tooltip, |
| 76 | + }, |
| 77 | + data() { |
| 78 | + return { |
| 79 | + // Form inputs |
| 80 | + "matrix_username": "", |
| 81 | + "matrix_password": "", |
| 82 | +
|
| 83 | + // Data |
| 84 | + "loggedIn": false, |
| 85 | + "currentUsername": "", |
| 86 | + }; |
| 87 | + }, |
| 88 | + computed: { |
| 89 | + currentlyLoggedInMessage() { |
| 90 | + return this.t('riotchat', 'Currently logged in as {username}', { |
| 91 | + username: this.currentUsername, |
| 92 | + }); |
| 93 | + } |
| 94 | + }, |
| 95 | + methods: { |
| 96 | + login () { |
| 97 | + const url = generateUrl('apps/riotchat/share/login'); |
| 98 | + Axios.post( |
| 99 | + url, |
| 100 | + { |
| 101 | + username: this.username, |
| 102 | + password: this.password, |
| 103 | + } |
| 104 | + ).then(() => { |
| 105 | + showSuccess(t('riotchat', 'Successfully logged into Matrix account')); |
| 106 | + this.whoAmI(); |
| 107 | + }).catch(() => { |
| 108 | + showSuccess(t('riotchat', 'Failed to login to Matrix account. Are your account details correct?')); |
| 109 | + }); |
| 110 | + }, |
| 111 | + logout () { |
| 112 | + const url = generateUrl('apps/riotchat/share/logout'); |
| 113 | + Axios.post(url).then(this.whoAmI).catch(() => { |
| 114 | + showError(t('riotchat', 'Failed to logout of Matrix account.')); |
| 115 | + }); |
| 116 | + }, |
| 117 | + whoAmI () { |
| 118 | + const url = generateUrl('apps/riotchat/share/whoami'); |
| 119 | + Axios.get(url).then((res) => { |
| 120 | + this.loggedIn = res.data.logged_in; |
| 121 | + this.currentUsername = res.data.user_id; |
| 122 | + }).catch(() => { |
| 123 | + showError(t('riotchat', 'Could not load user info. Please reload the page.')); |
| 124 | + }); |
| 125 | + } |
| 126 | + }, |
| 127 | + mounted () { |
| 128 | + this.whoAmI(); |
| 129 | + }, |
| 130 | +} |
| 131 | +</script> |
0 commit comments