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