-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitHub OAuth2 for testing environment.
Mentions: #5
- Loading branch information
1 parent
2886230
commit aedc107
Showing
8 changed files
with
2,369 additions
and
2,153 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<template lang="pug"> | ||
div(id="github-login") | ||
p(v-show="!done") 身分驗證中,請稍候 ... | ||
p(v-show="done && successful") 登入完成 | ||
p(v-show="done && !successful") | ||
| 登入失敗, | ||
a(:href="URL_AUTHORIZE") 再試一次 | ||
</template> | ||
|
||
<script> | ||
import axios from 'axios' | ||
export default { | ||
data () { | ||
return { | ||
client_id: 'a564c04f554415036378', | ||
client_secret: '1973a4a16b88767ffcff49db34d9f3057471c584', | ||
done: false, | ||
successful: false | ||
} | ||
}, | ||
computed: { | ||
URL_AUTHORIZE () { | ||
return 'https://github.com/login/oauth/authorize?client_id=' + this.client_id | ||
}, | ||
URL_TOKEN () { | ||
// CORS Proxies: | ||
const proxies = [ | ||
'https://thingproxy.freeboard.io/fetch/', | ||
'https://cors-anywhere.herokuapp.com/', | ||
'https://crossorigin.me/' | ||
] | ||
return proxies[0] + 'https://github.com/login/oauth/access_token' | ||
}, | ||
URL_USER () { | ||
return 'https://api.github.com/user' | ||
} | ||
}, | ||
methods: { | ||
pairsToObject (pairs) { | ||
const params = {} | ||
pairs.split('&').forEach((stmt) => { | ||
const eqp = stmt.indexOf('=') | ||
if (eqp > 0) { | ||
const k = stmt.substr(0, eqp) | ||
const v = decodeURIComponent(stmt.substr(eqp + 1)).replace(/\+/g, ' ') | ||
params[k] = v | ||
} else { | ||
if (eqp === -1) { | ||
params[stmt] = '' | ||
} | ||
} | ||
}) | ||
return params | ||
} | ||
}, | ||
created () { | ||
const pairs = window.location.search.substr(1) | ||
const params = this.pairsToObject(pairs) | ||
if (params['code'] !== undefined) { | ||
this.code = params['code'] | ||
axios.post(this.URL_TOKEN, { | ||
client_id: this.client_id, | ||
client_secret: this.client_secret, | ||
code: params['code'] | ||
}).then((resp) => { | ||
if (resp.status === 200) { | ||
console.log('Get GitHub access token.') | ||
const result = this.pairsToObject(resp.data) | ||
if (result['access_token'] !== undefined) { | ||
return axios.get(this.URL_USER, { | ||
headers: { | ||
'Authorization': 'token ' + result.access_token | ||
} | ||
}) | ||
} else { | ||
throw result.error_description | ||
} | ||
} else { | ||
throw resp.statusText | ||
} | ||
}).then((resp) => { | ||
if (resp.status === 200) { | ||
console.log('Get GitHub user info.') | ||
const userInfo = { | ||
login: resp.data.login, | ||
name: resp.data.name, | ||
auth: resp.config.headers.Authorization | ||
} | ||
this.done = true | ||
this.successful = true | ||
this.$store.commit('login', userInfo) | ||
setTimeout(() => this.$router.push('mapviewer'), 1500) | ||
return | ||
} | ||
throw resp.statusText | ||
}).catch((ex) => { | ||
console.error(ex) | ||
this.done = true | ||
}) | ||
} | ||
} | ||
} | ||
</script> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<template lang="pug"> | ||
div(id="editing-tool-layer") | ||
span OOO | ||
span(v-show="!authorized") | ||
a(href="https://github.com/login/oauth/authorize?client_id=a564c04f554415036378") 登入 | ||
span(v-show="authorized") | ||
| {{ username }} | ||
a(@click="logout") 登出 | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: [ 'mapState' ], | ||
computed: { | ||
authorized () { | ||
return this.$store.getters.userInfo.auth !== false | ||
}, | ||
username () { | ||
console.log(this.$store.getters.userInfo) | ||
return this.$store.getters.userInfo.name | ||
} | ||
}, | ||
methods: { | ||
logout () { | ||
this.$store.commit('logout') | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
#editing-tool-layer { | ||
position: absolute; | ||
right: 5px; | ||
top: 5px; | ||
border: 1px solid black; | ||
border-radius: 5px; | ||
padding: 5px 10px; | ||
font-size: 10pt; | ||
font-weight: normal; | ||
user-select: none; | ||
} | ||
</style> |
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
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
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