yandere_fe/src/components/login_form/login_form.js

83 lines
2.3 KiB
JavaScript
Raw Normal View History

import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
2018-10-26 06:20:39 -07:00
import oauthApi from '../../services/new_api/oauth.js'
2016-10-27 09:02:41 -07:00
const LoginForm = {
data: () => ({
user: {},
error: false
2016-10-27 09:02:41 -07: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 09:02:41 -07:00
},
methods: {
...mapMutations('authFlow', ['requireMFA']),
...mapActions({ login: 'authFlow/login' }),
submit () {
2019-06-13 12:00:13 -07:00
this.isTokenAuth ? this.submitToken() : this.submitPassword()
},
submitToken () {
const { clientId } = this.oauth
const data = {
clientId,
instance: this.instance.server,
2018-10-26 06:16:23 -07:00
commit: this.$store.commit
}
oauthApi.getOrCreateApp(data)
.then((app) => { oauthApi.login({ ...app, ...data }) })
2018-10-26 06:16:23 -07:00
},
submitPassword () {
const { clientId } = this.oauth
2018-11-07 07:56:12 -08:00
const data = {
clientId,
oauth: this.oauth,
2019-06-12 15:02:08 -07:00
instance: this.instance.server,
commit: this.$store.commit
2018-11-07 07:56:12 -08:00
}
this.error = false
2018-11-07 07:56:12 -08:00
oauthApi.getOrCreateApp(data).then((app) => {
oauthApi.getTokenWithCredentials(
{
...app,
2018-11-07 07:56:12 -08:00
instance: data.instance,
username: this.user.username,
2019-01-28 07:48:00 -08:00
password: this.user.password
}
).then((result) => {
2019-01-28 07:48:00 -08:00
if (result.error) {
if (result.error === 'mfa_required') {
this.requireMFA({app: app, settings: result})
} else {
this.error = result.error
this.focusOnPasswordInput()
}
2019-01-28 07:48:00 -08:00
return
}
this.login(result).then(() => {
2019-04-09 09:19:48 -07:00
this.$router.push({name: 'friends'})
})
2019-01-28 07:48:00 -08:00
})
2018-11-07 07:56:12 -08:00
})
},
clearError () { this.error = false },
focusOnPasswordInput () {
let passwordInput = this.$refs.passwordInput
passwordInput.focus()
passwordInput.setSelectionRange(0, passwordInput.value.length)
2016-10-27 09:02:41 -07:00
}
}
}
export default LoginForm