2018-11-07 07:48:05 +11:00
|
|
|
import oauthApi from '../../services/new_api/oauth.js'
|
2018-12-04 05:43:58 +11:00
|
|
|
import { humanizeErrors } from '../../modules/errors'
|
2018-11-07 07:48:05 +11:00
|
|
|
|
2017-04-16 02:12:23 +10:00
|
|
|
const registration = {
|
|
|
|
data: () => ({
|
|
|
|
user: {},
|
2018-12-04 05:43:58 +11:00
|
|
|
errors: [],
|
2017-04-16 02:12:23 +10:00
|
|
|
registering: false
|
|
|
|
}),
|
2017-06-20 17:37:51 +10:00
|
|
|
created () {
|
2018-09-10 04:21:23 +10:00
|
|
|
if ((!this.$store.state.instance.registrationOpen && !this.token) || !!this.$store.state.users.currentUser) {
|
2017-06-20 17:37:51 +10:00
|
|
|
this.$router.push('/main/all')
|
|
|
|
}
|
2018-08-05 17:01:38 +10:00
|
|
|
// Seems like this doesn't work at first page open for some reason
|
2018-09-10 04:21:23 +10:00
|
|
|
if (this.$store.state.instance.registrationOpen && this.token) {
|
2018-08-05 17:01:38 +10:00
|
|
|
this.$router.push('/registration')
|
|
|
|
}
|
2017-06-20 17:37:51 +10:00
|
|
|
},
|
2017-06-19 23:35:35 +10:00
|
|
|
computed: {
|
2018-09-10 04:21:23 +10:00
|
|
|
termsofservice () { return this.$store.state.instance.tos },
|
2018-08-05 17:01:38 +10:00
|
|
|
token () { return this.$route.params.token }
|
2017-06-19 23:35:35 +10:00
|
|
|
},
|
2017-04-16 02:12:23 +10:00
|
|
|
methods: {
|
|
|
|
submit () {
|
|
|
|
this.registering = true
|
|
|
|
this.user.nickname = this.user.username
|
2018-08-05 17:01:38 +10:00
|
|
|
this.user.token = this.token
|
2017-04-16 02:12:23 +10:00
|
|
|
this.$store.state.api.backendInteractor.register(this.user).then(
|
|
|
|
(response) => {
|
|
|
|
if (response.ok) {
|
2018-11-07 07:48:05 +11:00
|
|
|
const data = {
|
|
|
|
oauth: this.$store.state.oauth,
|
|
|
|
instance: this.$store.state.instance.server
|
|
|
|
}
|
|
|
|
oauthApi.getOrCreateApp(data).then((app) => {
|
|
|
|
oauthApi.getTokenWithCredentials(
|
2018-11-07 07:51:22 +11:00
|
|
|
{
|
|
|
|
app,
|
|
|
|
instance: data.instance,
|
|
|
|
username: this.user.username,
|
2018-12-04 05:43:58 +11:00
|
|
|
password: this.user.password
|
|
|
|
})
|
2018-11-07 07:48:05 +11:00
|
|
|
.then((result) => {
|
|
|
|
this.$store.commit('setToken', result.access_token)
|
|
|
|
this.$store.dispatch('loginUser', result.access_token)
|
|
|
|
this.$router.push('/main/friends')
|
|
|
|
})
|
|
|
|
})
|
2017-04-16 02:12:23 +10:00
|
|
|
} else {
|
|
|
|
this.registering = false
|
|
|
|
response.json().then((data) => {
|
2018-12-04 05:43:58 +11:00
|
|
|
this.errors = humanizeErrors(JSON.parse(data.error))
|
2017-04-16 02:12:23 +10:00
|
|
|
})
|
|
|
|
}
|
2018-12-04 05:43:58 +11:00
|
|
|
},
|
2017-04-16 02:12:23 +10:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default registration
|