Re-activate registration, use oauth password flow to fetch token.

This commit is contained in:
Roger Braun 2018-11-06 21:48:05 +01:00
parent b6cd4ff32a
commit 4d9680e797
3 changed files with 41 additions and 5 deletions

View file

@ -7,7 +7,10 @@
<div class="panel-body"> <div class="panel-body">
<form v-on:submit.prevent='oAuthLogin' class="login-form"> <form v-on:submit.prevent='oAuthLogin' class="login-form">
<div class='form-group'> <div class='form-group'>
<button class="btn btn-default">Login with OAuth</button> <div class='login-bottom'>
<div><router-link :to="{name: 'registration'}" v-if='registrationOpen' class='register'>{{$t('login.register')}}</router-link></div>
<button :disabled="loggingIn" type='submit' class='btn btn-default'>{{$t('login.login')}}</button>
</div>
</div> </div>
</form> </form>
</div> </div>

View file

@ -1,3 +1,5 @@
import oauthApi from '../../services/new_api/oauth.js'
const registration = { const registration = {
data: () => ({ data: () => ({
user: {}, user: {},
@ -25,9 +27,22 @@ const registration = {
this.$store.state.api.backendInteractor.register(this.user).then( this.$store.state.api.backendInteractor.register(this.user).then(
(response) => { (response) => {
if (response.ok) { if (response.ok) {
this.$store.dispatch('loginUser', this.user) const data = {
this.$router.push('/main/all') oauth: this.$store.state.oauth,
this.registering = false instance: this.$store.state.instance.server
}
oauthApi.getOrCreateApp(data).then((app) => {
oauthApi.getTokenWithCredentials(
{app,
instance: data.instance,
username: this.user.username,
password: this.user.password})
.then((result) => {
this.$store.commit('setToken', result.access_token)
this.$store.dispatch('loginUser', result.access_token)
this.$router.push('/main/friends')
})
})
} else { } else {
this.registering = false this.registering = false
response.json().then((data) => { response.json().then((data) => {

View file

@ -40,6 +40,22 @@ const login = (args) => {
}) })
} }
const getTokenWithCredentials = ({app, instance, username, password}) => {
const url = `${instance}/oauth/token`
const form = new window.FormData()
form.append('client_id', app.client_id)
form.append('client_secret', app.client_secret)
form.append('grant_type', 'password')
form.append('username', username)
form.append('password', password)
return window.fetch(url, {
method: 'POST',
body: form
}).then((data) => data.json())
}
const getToken = ({app, instance, code}) => { const getToken = ({app, instance, code}) => {
const url = `${instance}/oauth/token` const url = `${instance}/oauth/token`
const form = new window.FormData() const form = new window.FormData()
@ -58,7 +74,9 @@ const getToken = ({app, instance, code}) => {
const oauth = { const oauth = {
login, login,
getToken getToken,
getTokenWithCredentials,
getOrCreateApp
} }
export default oauth export default oauth