Created
June 27, 2017 22:33
-
-
Save numfin/f0cda1d40c0003b9f67d2be0516dffa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [GET USER FROM SERVER] | |
export const getAccessToken = ({ commit, dispatch }, { req }) => { | |
if (!req || !req.headers.cookie) return Promise.reject('no-cookie') | |
// [get new tokens] | |
return axios | |
.post(`https://securetoken.googleapis.com/v1/token?key=${projectID}`, { | |
grant_type: 'refresh_token', | |
refresh_token: Cookie.parse(req.headers.cookie).refresh | |
}) | |
.then(res => { | |
if (res.data.access_token && res.data.user_id && res.data.refresh_token) { | |
return Promise.resolve({ | |
NEW_TOKEN: res.data.access_token, | |
NEW_UID: res.data.user_id | |
}) | |
} | |
return Promise.reject('no-cookie') | |
}) | |
.catch(error => Promise.reject(error)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [GET USER DATA FROM FIREBASE DB] | |
export const getUserData = ({ state, commit }, { NEW_TOKEN, NEW_UID }) => { | |
return axios | |
.get( | |
database.child('users').child(NEW_UID).toString() + | |
`.json?auth=${NEW_TOKEN}` | |
) | |
.then(res => { | |
commit('SET_USER', res.data) | |
return Promise.resolve(state.authUser) | |
}) | |
.catch(error => Promise.reject(error)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default ({ store, req, isServer }) => { | |
if (isServer) { | |
return store | |
.dispatch('getAccessToken', { req }) | |
.then(tokens => { | |
return store.dispatch('getUserData', tokens) | |
}) | |
.catch(console.log) | |
} | |
return store.dispatch('getUser') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<h1> | |
{{ user }} | |
</h1> | |
</template> | |
<script> | |
export default { | |
name: 'vueheader', | |
data () { | |
return { | |
user: this.$store.state.authUser | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment