Created
June 27, 2017 19:15
-
-
Save numfin/417aeb6d43638b2f322999d1acd66252 to your computer and use it in GitHub Desktop.
This file contains 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 getUserServer = ({ commit }, { req }) => { | |
return new Promise(resolve => { | |
if (!req || !req.headers.cookie) resolve('no cookie') | |
// [берем refresh token] | |
const refresh = Cookie.parse(req.headers.cookie).refresh || '' | |
// [создаем ссылку для axios] | |
const refreshLink = `https://securetoken.googleapis.com/v1/token?key=${projectID}` | |
// [обновляем токен] | |
const updateToken = axios.post(refreshLink, { | |
grant_type: 'refresh_token', | |
refresh_token: refresh | |
}) | |
updateToken | |
.then(res => { | |
const NEW_TOKEN = res.data.access_token || '' | |
const NEW_UID = res.data.user_id || '' | |
const NEW_REFRESH = res.data.refresh_token || '' | |
if (NEW_TOKEN === '' || NEW_UID === '' || NEW_REFRESH === '') return | |
const ref = | |
database.child('users').child(NEW_UID).toString() + | |
`.json?auth=${NEW_TOKEN}` | |
axios | |
.get(ref) | |
.then(res => { | |
console.log(res.data) | |
commit('SET_USER', res.data) | |
resolve('done') | |
}) | |
.catch(error => { | |
console.log(error.response.status, error.response.statusText) | |
resolve('error') | |
}) | |
}) | |
.catch(error => { | |
console.log(error.response.status, error.response.statusText) | |
resolve('error') | |
}) | |
}) | |
} |
This file contains 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('getUserServer', { req }) | |
} | |
console.log('to client') | |
return store.dispatch('getUser') | |
} |
This file contains 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
SET_USER: function (state, user) { | |
state.authUser = user | |
}, | |
SET_TOKEN: (state, authCookies) => { | |
const keys = Object.keys(authCookies) | |
if (!inBrowser || !keys || !authCookies.token) return | |
keys.forEach(cookieName => { | |
Cookies.set(cookieName, authCookies[cookieName]) | |
}) | |
}, |
This file contains 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 () => ({ | |
authUser: null, | |
keys: null, | |
confident: null | |
}) |
This file contains 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> | |
{{ $store.state.authUser }}</h1> | |
</template> | |
<script> | |
export default { | |
name: 'vueheader' | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment