pleroma-fe/src/components/login_form/login_form.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
2018-10-26 15:20:39 +02:00
import oauthApi from '../../services/new_api/oauth.js'
2020-10-20 20:18:23 +02:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
faTimes
} from '@fortawesome/free-solid-svg-icons'
library.add(
faTimes
)
2016-10-27 18:02:41 +02:00
const LoginForm = {
data: () => ({
user: {},
error: false
2016-10-27 18:02:41 +02:00
}),
computed: {
isPasswordAuth () { return this.requiredPassword },
isTokenAuth () { return this.requiredToken },
...mapState({
registrationOpen: state => state.instance.registrationOpen,
instance: state => state.instance,
loggingIn: state => state.users.loggingIn,
oauth: state => state.oauth
}),
...mapGetters(
'authFlow', ['requiredPassword', 'requiredToken', 'requiredMFA']
)
2016-10-27 18:02:41 +02:00
},
methods: {
...mapMutations('authFlow', ['requireMFA']),
...mapActions({ login: 'authFlow/login' }),
submit () {
2019-06-13 21:00:13 +02:00
this.isTokenAuth ? this.submitToken() : this.submitPassword()
},
submitToken () {
2019-06-20 05:20:14 +02:00
const { clientId, clientSecret } = this.oauth
const data = {
clientId,
2019-06-20 05:20:14 +02:00
clientSecret,
instance: this.instance.server,
2018-10-26 15:16:23 +02:00
commit: this.$store.commit
}
oauthApi.getOrCreateApp(data)
.then((app) => { oauthApi.login({ ...app, ...data }) })
2018-10-26 15:16:23 +02:00
},
submitPassword () {
const { clientId } = this.oauth
2018-11-07 16:56:12 +01:00
const data = {
clientId,
oauth: this.oauth,
2019-06-13 00:02:08 +02:00
instance: this.instance.server,
commit: this.$store.commit
2018-11-07 16:56:12 +01:00
}
this.error = false
2018-11-07 16:56:12 +01:00
oauthApi.getOrCreateApp(data).then((app) => {
oauthApi.getTokenWithCredentials(
{
...app,
2018-11-07 16:56:12 +01:00
instance: data.instance,
username: this.user.username,
2019-01-28 16:48:00 +01:00
password: this.user.password
}
).then((result) => {
2019-01-28 16:48:00 +01:00
if (result.error) {
if (result.error === 'mfa_required') {
2019-12-12 19:19:13 +01:00
this.requireMFA({ settings: result })
2019-10-16 12:00:26 +02:00
} else if (result.identifier === 'password_reset_required') {
this.$router.push({ name: 'password-reset', params: { passwordResetRequested: true } })
} else {
this.error = result.error
this.focusOnPasswordInput()
}
2019-01-28 16:48:00 +01:00
return
}
this.login(result).then(() => {
2019-07-05 09:02:14 +02:00
this.$router.push({ name: 'friends' })
})
2019-01-28 16:48:00 +01:00
})
2018-11-07 16:56:12 +01:00
})
},
clearError () { this.error = false },
focusOnPasswordInput () {
2022-07-31 11:35:48 +02:00
const passwordInput = this.$refs.passwordInput
passwordInput.focus()
passwordInput.setSelectionRange(0, passwordInput.value.length)
2016-10-27 18:02:41 +02:00
}
}
}
export default LoginForm