feat: Login script setup (#2417)
Co-authored-by: Dominik Pschenitschni <mail@celement.de> Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2417 Reviewed-by: konrad <k@knt.li> Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de> Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
parent
940063784b
commit
63fb8a1962
6 changed files with 134 additions and 134 deletions
|
@ -1,14 +1,9 @@
|
||||||
import {createRandomID} from '@/helpers/randomId'
|
|
||||||
import {parseURL} from 'ufo'
|
import {parseURL} from 'ufo'
|
||||||
|
|
||||||
export interface Provider {
|
import {createRandomID} from '@/helpers/randomId'
|
||||||
name: string
|
import type {IProvider} from '@/types/IProvider'
|
||||||
key: string
|
|
||||||
authUrl: string
|
|
||||||
clientId: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export const redirectToProvider = (provider: Provider, redirectUrl: string = '') => {
|
export const redirectToProvider = (provider: IProvider, redirectUrl: string = '') => {
|
||||||
|
|
||||||
// We're not using the redirect url provided by the server to allow redirects when using the electron app.
|
// We're not using the redirect url provided by the server to allow redirects when using the electron app.
|
||||||
// The implications are not quite clear yet hence the logic to pass in another redirect url still exists.
|
// The implications are not quite clear yet hence the logic to pass in another redirect url still exists.
|
||||||
|
|
|
@ -275,6 +275,27 @@ export const useAuthStore = defineStore('auth', {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to verify the email
|
||||||
|
* @returns {Promise<boolean>} if the email was successfully confirmed
|
||||||
|
*/
|
||||||
|
async verifyEmail() {
|
||||||
|
const emailVerifyToken = localStorage.getItem('emailConfirmToken')
|
||||||
|
if (emailVerifyToken) {
|
||||||
|
const stopLoading = setModuleLoading(this)
|
||||||
|
try {
|
||||||
|
await HTTPFactory().post('user/confirm', {token: emailVerifyToken})
|
||||||
|
localStorage.removeItem('emailConfirmToken')
|
||||||
|
return true
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error(e.response.data.message)
|
||||||
|
} finally {
|
||||||
|
stopLoading()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
|
||||||
async saveUserSettings({
|
async saveUserSettings({
|
||||||
settings,
|
settings,
|
||||||
showMessage = true,
|
showMessage = true,
|
||||||
|
|
|
@ -132,7 +132,7 @@ export const useBaseStore = defineStore('base', {
|
||||||
|
|
||||||
async loadApp() {
|
async loadApp() {
|
||||||
await checkAndSetApiUrl(window.API_URL)
|
await checkAndSetApiUrl(window.API_URL)
|
||||||
await useAuthStore().checkAuth()
|
useAuthStore().checkAuth()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,6 +4,8 @@ import {parseURL} from 'ufo'
|
||||||
import {HTTPFactory} from '@/http-common'
|
import {HTTPFactory} from '@/http-common'
|
||||||
import {objectToCamelCase} from '@/helpers/case'
|
import {objectToCamelCase} from '@/helpers/case'
|
||||||
|
|
||||||
|
import type {IProvider} from '@/types/IProvider'
|
||||||
|
|
||||||
export interface ConfigState {
|
export interface ConfigState {
|
||||||
version: string,
|
version: string,
|
||||||
frontendUrl: string,
|
frontendUrl: string,
|
||||||
|
@ -29,7 +31,7 @@ export interface ConfigState {
|
||||||
openidConnect: {
|
openidConnect: {
|
||||||
enabled: boolean,
|
enabled: boolean,
|
||||||
redirectUrl: string,
|
redirectUrl: string,
|
||||||
providers: [],
|
providers: IProvider[],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
6
src/types/IProvider.ts
Normal file
6
src/types/IProvider.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
export interface IProvider {
|
||||||
|
name: string;
|
||||||
|
key: string;
|
||||||
|
authUrl: string;
|
||||||
|
clientId: string;
|
||||||
|
}
|
|
@ -14,14 +14,14 @@
|
||||||
class="input" id="username"
|
class="input" id="username"
|
||||||
name="username"
|
name="username"
|
||||||
:placeholder="$t('user.auth.usernamePlaceholder')"
|
:placeholder="$t('user.auth.usernamePlaceholder')"
|
||||||
ref="username"
|
ref="usernameRef"
|
||||||
required
|
required
|
||||||
type="text"
|
type="text"
|
||||||
autocomplete="username"
|
autocomplete="username"
|
||||||
v-focus
|
v-focus
|
||||||
@keyup.enter="submit"
|
@keyup.enter="submit"
|
||||||
tabindex="1"
|
tabindex="1"
|
||||||
@focusout="validateField('username')"
|
@focusout="validateUsernameField()"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p class="help is-danger" v-if="!usernameValid">
|
<p class="help is-danger" v-if="!usernameValid">
|
||||||
|
@ -39,7 +39,7 @@
|
||||||
{{ $t('user.auth.forgotPassword') }}
|
{{ $t('user.auth.forgotPassword') }}
|
||||||
</router-link>
|
</router-link>
|
||||||
</div>
|
</div>
|
||||||
<password tabindex="2" @submit="submit" v-model="password" :validate-initially="validatePasswordInitially"/>
|
<Password tabindex="2" @submit="submit" v-model="password" :validate-initially="validatePasswordInitially"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="field" v-if="needsTotpPasscode">
|
<div class="field" v-if="needsTotpPasscode">
|
||||||
<label class="label" for="totpPasscode">{{ $t('user.auth.totpTitle') }}</label>
|
<label class="label" for="totpPasscode">{{ $t('user.auth.totpTitle') }}</label>
|
||||||
|
@ -101,136 +101,112 @@
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script setup lang="ts">
|
||||||
import {defineComponent} from 'vue'
|
import {computed, onBeforeMount, ref} from 'vue'
|
||||||
|
import {useI18n} from 'vue-i18n'
|
||||||
|
import {useRouter} from 'vue-router'
|
||||||
import {useDebounceFn} from '@vueuse/core'
|
import {useDebounceFn} from '@vueuse/core'
|
||||||
import {mapState} from 'pinia'
|
|
||||||
|
|
||||||
import {HTTPFactory} from '@/http-common'
|
|
||||||
import {getErrorText} from '@/message'
|
|
||||||
import Message from '@/components/misc/message.vue'
|
import Message from '@/components/misc/message.vue'
|
||||||
import {redirectToProvider} from '../../helpers/redirectToProvider'
|
|
||||||
import {getLastVisited, clearLastVisited} from '../../helpers/saveLastVisited'
|
|
||||||
import Password from '@/components/input/password.vue'
|
import Password from '@/components/input/password.vue'
|
||||||
import {setTitle} from '@/helpers/setTitle'
|
|
||||||
|
|
||||||
import {useConfigStore} from '@/stores/config'
|
import {getErrorText} from '@/message'
|
||||||
|
import {redirectToProvider} from '@/helpers/redirectToProvider'
|
||||||
|
import {getLastVisited, clearLastVisited} from '@/helpers/saveLastVisited'
|
||||||
|
|
||||||
import {useAuthStore} from '@/stores/auth'
|
import {useAuthStore} from '@/stores/auth'
|
||||||
|
import {useConfigStore} from '@/stores/config'
|
||||||
|
|
||||||
export default defineComponent({
|
import {useTitle} from '@/composables/useTitle'
|
||||||
components: {
|
|
||||||
Password,
|
const router = useRouter()
|
||||||
Message,
|
const {t} = useI18n({useScope: 'global'})
|
||||||
},
|
useTitle(() => t('user.auth.login'))
|
||||||
data() {
|
|
||||||
return {
|
const authStore = useAuthStore()
|
||||||
confirmedEmailSuccess: false,
|
const configStore = useConfigStore()
|
||||||
errorMessage: '',
|
|
||||||
usernameValid: true,
|
const registrationEnabled = computed(() => configStore.registrationEnabled)
|
||||||
password: '',
|
const localAuthEnabled = computed(() => configStore.auth.local.enabled)
|
||||||
validatePasswordInitially: false,
|
|
||||||
rememberMe: false,
|
const openidConnect = computed(() => configStore.auth.openidConnect)
|
||||||
}
|
const hasOpenIdProviders = computed(() => openidConnect.value.enabled && openidConnect.value.providers?.length > 0)
|
||||||
},
|
|
||||||
beforeMount() {
|
const isLoading = computed(() => authStore.isLoading)
|
||||||
const HTTP = HTTPFactory()
|
|
||||||
// Try to verify the email
|
const confirmedEmailSuccess = ref(false)
|
||||||
// FIXME: Why is this here? Can we find a better place for this?
|
const errorMessage = ref('')
|
||||||
let emailVerifyToken = localStorage.getItem('emailConfirmToken')
|
const password = ref('')
|
||||||
if (emailVerifyToken) {
|
const validatePasswordInitially = ref(false)
|
||||||
const stopLoading = this.setLoading()
|
const rememberMe = ref(false)
|
||||||
HTTP.post('user/confirm', {token: emailVerifyToken})
|
|
||||||
.then(() => {
|
const authenticated = computed(() => authStore.authenticated)
|
||||||
localStorage.removeItem('emailConfirmToken')
|
|
||||||
this.confirmedEmailSuccess = true
|
onBeforeMount(() => {
|
||||||
|
authStore.verifyEmail().then((confirmed) => {
|
||||||
|
confirmedEmailSuccess.value = confirmed
|
||||||
|
}).catch((e: Error) => {
|
||||||
|
errorMessage.value = e.message
|
||||||
})
|
})
|
||||||
.catch(e => {
|
|
||||||
this.errorMessage = e.response.data.message
|
|
||||||
})
|
|
||||||
.finally(stopLoading)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the user is already logged in, if so, redirect them to the homepage
|
// Check if the user is already logged in, if so, redirect them to the homepage
|
||||||
if (this.authenticated) {
|
if (authenticated.value) {
|
||||||
const last = getLastVisited()
|
const last = getLastVisited()
|
||||||
if (last !== null) {
|
if (last !== null) {
|
||||||
this.$router.push({
|
router.push({
|
||||||
name: last.name,
|
name: last.name,
|
||||||
params: last.params,
|
params: last.params,
|
||||||
})
|
})
|
||||||
clearLastVisited()
|
clearLastVisited()
|
||||||
} else {
|
} else {
|
||||||
this.$router.push({name: 'home'})
|
router.push({name: 'home'})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
created() {
|
|
||||||
setTitle(this.$t('user.auth.login'))
|
|
||||||
},
|
|
||||||
computed: {
|
|
||||||
hasOpenIdProviders() {
|
|
||||||
return this.openidConnect.enabled && this.openidConnect.providers?.length > 0
|
|
||||||
},
|
|
||||||
|
|
||||||
...mapState(useAuthStore, {
|
const usernameValid = ref(true)
|
||||||
needsTotpPasscode: state => state.needsTotpPasscode,
|
const usernameRef = ref<HTMLInputElement | null>(null)
|
||||||
authenticated: state => state.authenticated,
|
const validateUsernameField = useDebounceFn(() => {
|
||||||
isLoading: state => state.isLoading,
|
usernameValid.value = usernameRef.value?.value !== ''
|
||||||
}),
|
|
||||||
|
|
||||||
...mapState(useConfigStore, {
|
|
||||||
registrationEnabled: state => state.registrationEnabled,
|
|
||||||
localAuthEnabled: state => state.auth.local.enabled,
|
|
||||||
openidConnect: state => state.auth.openidConnect,
|
|
||||||
}),
|
|
||||||
|
|
||||||
validateField() {
|
|
||||||
// using computed so that debounced function definition stays
|
|
||||||
return useDebounceFn((field) => {
|
|
||||||
this[`${field}Valid`] = this.$refs[field]?.value !== ''
|
|
||||||
}, 100)
|
}, 100)
|
||||||
},
|
|
||||||
},
|
const needsTotpPasscode = computed(() => authStore.needsTotpPasscode)
|
||||||
methods: {
|
const totpPasscode = ref<HTMLInputElement | null>(null)
|
||||||
async submit() {
|
|
||||||
this.errorMessage = ''
|
async function submit() {
|
||||||
|
errorMessage.value = ''
|
||||||
// Some browsers prevent Vue bindings from working with autofilled values.
|
// Some browsers prevent Vue bindings from working with autofilled values.
|
||||||
// To work around this, we're manually getting the values here instead of relying on vue bindings.
|
// To work around this, we're manually getting the values here instead of relying on vue bindings.
|
||||||
// For more info, see https://kolaente.dev/vikunja/frontend/issues/78
|
// For more info, see https://kolaente.dev/vikunja/frontend/issues/78
|
||||||
const credentials = {
|
const credentials = {
|
||||||
username: this.$refs.username.value,
|
username: usernameRef.value?.value,
|
||||||
password: this.password,
|
password: password.value,
|
||||||
longToken: this.rememberMe,
|
longToken: rememberMe.value,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (credentials.username === '' || credentials.password === '') {
|
if (credentials.username === '' || credentials.password === '') {
|
||||||
// Trigger the validation error messages
|
// Trigger the validation error messages
|
||||||
this.validateField('username')
|
validateUsernameField()
|
||||||
this.validatePasswordInitially = true
|
validatePasswordInitially.value = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.needsTotpPasscode) {
|
if (needsTotpPasscode.value) {
|
||||||
credentials.totpPasscode = this.$refs.totpPasscode.value
|
credentials.totpPasscode = totpPasscode.value?.value
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const authStore = useAuthStore()
|
|
||||||
await authStore.login(credentials)
|
await authStore.login(credentials)
|
||||||
authStore.setNeedsTotpPasscode(false)
|
authStore.setNeedsTotpPasscode(false)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.response?.data.code === 1017 && !this.credentials.totpPasscode) {
|
if (e.response?.data.code === 1017 && !credentials.totpPasscode) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const err = getErrorText(e)
|
const err = getErrorText(e)
|
||||||
this.errorMessage = typeof err[1] !== 'undefined' ? err[1] : err[0]
|
errorMessage.value = typeof err[1] !== 'undefined' ? err[1] : err[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
redirectToProvider,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
|
Loading…
Reference in a new issue