Improve Indieauth

This commit is contained in:
Peter Stuifzand 2018-08-29 17:07:35 +02:00
parent 6d8e657fc5
commit 76af58f09a
4 changed files with 66 additions and 22 deletions

View File

@ -8,6 +8,7 @@
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"
}, },
"dependencies": { "dependencies": {
"micropub-helper": "^1.4.0",
"node-sass": "^4.9.3", "node-sass": "^4.9.3",
"vue": "^2.5.17", "vue": "^2.5.17",
"vue-router": "^3.0.1", "vue-router": "^3.0.1",

View File

@ -23,9 +23,11 @@
<button class="modal-close is-large" aria-label="close" @click="close"></button> <button class="modal-close is-large" aria-label="close" @click="close"></button>
</div> </div>
</template> </template>
<script> <script>
import qs from 'qs' import Micropub from 'micropub-helper';
import helper from '@/helpers';
const CLIENT_ID = 'https://p83.nl/'
export default { export default {
name: "LoginModal", name: "LoginModal",
@ -50,8 +52,24 @@
this.show = false this.show = false
}, },
login() { login() {
// try to login with const state = helper.generateState()
window.location = 'https://p83.nl/auth?response_type=code&me=https://p83.nl/&redirect_uri=http://192.168.178.21:8080/callback&scope=channels+timeline&state=1234&client_id=https://p83.nl/';
let micropub = new Micropub({
clientId: CLIENT_ID,
redirectUri: window.location.origin + '/callback',
me: this.url,
state: state,
scope: 'create post channels timeline'
})
micropub.getAuthUrl().then(url => {
let options = {
tokenEndpoint: micropub.options.tokenEndpoint,
loginState: state
}
this.$store.dispatch('saveEndpoints', options)
window.location = url
// eslint-disable-next-line
}).catch(err => console.log(err))
} }
}, },
@ -66,27 +84,29 @@
return return
} }
let tokenurl = 'https://p83.nl/authtoken' // FIXME: clientId, redirectUri
let params = {} let micropub = new Micropub({
params['grant_type'] = 'authorization_code' clientId: CLIENT_ID,
params['code'] = code redirectUri: window.location.origin + '/callback',
params['client_id'] = 'https://p83.nl/' me: this.$route.query['me'],
params['redirect_uri'] = 'http://192.168.178.21:8080/callback' state: this.$store.state.loginState,
params['me'] = 'https://p83.nl/' scope: 'create post channels timeline',
tokenEndpoint: this.$store.state.tokenEndpoint
})
fetch(tokenurl, { micropub
method: 'POST', .getToken(code)
body: qs.stringify(params, {arrayFormat: 'brackets'}), .then(token => {
headers: { this.$store.dispatch('tokenResponse', {
"Content-Type": "application/x-www-form-urlencoded", access_token: token,
"Accept": "application/json" me: micropub.options.me,
} scope: micropub.options.scope
}).then(response => response.json()) })
.then(response => {
this.$store.dispatch('tokenResponse', response)
this.show = false this.show = false
this.$router.push('/') this.$router.push('/')
}) })
// eslint-disable-next-line
.catch(err => console.log(err));
} }
} }
</script> </script>

12
src/helpers.js Normal file
View File

@ -0,0 +1,12 @@
function statePart() {
return Math.random().toString(36).substring(7)
}
function generateState() {
return statePart() + statePart() + statePart() + statePart();
}
export default {
generateState
}

View File

@ -7,17 +7,24 @@ Vue.use(Vuex)
export default new Vuex.Store({ export default new Vuex.Store({
state() { state() {
let loginData = JSON.parse(window.localStorage.getItem('login_data'))
let newState = { let newState = {
channels: [], channels: [],
timeline: {items: [], paging: {}}, timeline: {items: [], paging: {}},
channel: {}, channel: {},
debug: false, debug: false,
logged_in: false,
micropubEndpoint: '',
microsubEndpoint: ''
}; };
let loginData = JSON.parse(window.localStorage.getItem('login_data'))
if (loginData) { if (loginData) {
newState = { ...newState, ...loginData } newState = { ...newState, ...loginData }
newState.logged_in = loginData.access_token && loginData.access_token.length > 0 newState.logged_in = loginData.access_token && loginData.access_token.length > 0
} }
let endpoints = JSON.parse(window.localStorage.getItem('endpoints'))
if (endpoints) {
newState = { ...newState, ...endpoints}
}
return newState return newState
}, },
@ -77,6 +84,10 @@ export default new Vuex.Store({
commit('newTimeline', {channel: channel, timeline: response}) commit('newTimeline', {channel: channel, timeline: response})
}) })
}, },
saveEndpoints({commit}, endpoints) {
window.localStorage.setItem('endpoints', JSON.stringify(endpoints))
commit('newEndpoints', endpoints)
},
tokenResponse({commit}, response) { tokenResponse({commit}, response) {
window.localStorage.setItem('login_data', JSON.stringify(response)) window.localStorage.setItem('login_data', JSON.stringify(response))
commit('newAccessToken', response) commit('newAccessToken', response)