8000 github login is ok · pycoder404/blog-vue@cfeb8c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfeb8c2

Browse files
committed
github login is ok
1 parent e7298bc commit cfeb8c2

File tree

4 files changed

+86
-43
lines changed

4 files changed

+86
-43
lines changed

src/permission.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,57 @@ router.beforeEach(async (to, from, next) => {
1717
// console.log(to.path)
1818
// set page title
1919
document.title = getPageTitle(to.meta.title)
20+
console.info(to)
21+
const thirdPart = to.query && to.query.thirdPart
22+
const oauthCode = to.query && to.query.code
23+
console.info(thirdPart,oauthCode)
24+
if (thirdPart && oauthCode) {
25+
try {
26+
// social login
27+
// console.info(to.query)
28+
delete to.query.code
29+
delete to.query.thirdPart
30+
// console.info("begin to social auth login")
31+
await store.dispatch('user/socialLogin',{'thirdPart': thirdPart, 'oauthCode': oauthCode})
32+
// console.info("social login done")
33+
} catch (error) {
34+
// remove token and go to login page to re-login
35+
await store.dispatch('user/resetToken')
36+
ElMessage.error('Error in login by ',thirdPart)
37+
next({...to, replace: true})
38+
NProgress.done()
39+
}
40+
41+
}
2042

2143
// determine whether the user has logged in
44+
// console.info("xxxxxxxxxxxxxxxxxxxx")
2245
const hasAccessToken = getAccessToken()
46+
// console.info("get haccess token")
2347
// console.log("check is has access token: ",hasAccessToken)
2448
if (hasAccessToken) {
25-
// console.log("has access token:",hasAccessToken)
49+
console.log("has access token:",hasAccessToken)
2650
// TODO 添加对accessToken的过期检查和refresh
51+
// fixme change to check permissions
52+
2753
if (to.path === '/login') {
2854
// if is logged in, redirect to the home page
29-
next({path: '/'})
55+
const next_path = to.query && to.query.redirect ? to.query.redirect : '/'
56+
next({path: next_path})
3057
NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
3158
} else {
3259
// determine whether the user has obtained his permission roles through getInfo
33-
// console.log("check is has roles")
60+
console.log("check is has roles")
3461
// console.log(store.getters.roles)
3562
// note Ctrl + F5强制刷新界面,对store有啥影响吗,为啥会导致没有roles,重新获取Info??
3663
// note: 因为store是存在内存中的,所以每次刷新就会判断为空,需要重新获取数据,而cookie保存在本地,所以刷新不会丢失
3764
const hasRoles = store.getters.roles && store.getters.roles.length > 0
3865
if (hasRoles) {
39-
// console.log("yes has roles")
66+
console.log("yes has roles")
4067
next()
41-
// console.log('next done')
68+
console.log('next done')
4269
} else {
43-
// console.log('can not get roles from store')
70+
console.log('can not get roles from store')
4471
try {
4572
// get user info
4673
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']

src/store/modules/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const actions = {
8383
return new Promise((resolve, reject) => {
8484
socialLogin(thirdPart, data).then(response => {
8585
const data = response
86-
// console.log("login done")
86+
console.info("login succed")
8787
commit('SET_ACCESS_TOKEN', data.access_token)
8888
commit('SET_REFRESH_TOKEN', data.refresh_token)
8989
// token保存在cookie和store中

src/views/login/components/SocialSignin.vue

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,49 @@
2626
},
2727
data() {
2828
return {
29-
githubAuthUri: "https://github.com/login/oauth/authorize?client_id=aa6d9aa35a3d63374015"
29+
30+
githubAuthBaseUri: "https://github.com/login/oauth/authorize?client_id=aa6d9aa35a3d63374015"
3031
}
3132
3233
},
33-
methods: {
34-
wechatHandleClick(thirdpart) {
35-
alert(thirdpart)
36-
console.log(thirdpart)
37-
// this.$store.commit('SET_AUTH_TYPE', thirdpart)
38-
// const appid = 'xxxxx'
34+
computed: {
35+
githubAuthUri() {
36+
// const redirect_uri = 'http://10.89.228.206:28080/article/create/?thirdPart=github'
3937
// const redirect_uri = encodeURIComponent('xxx/redirect?redirect=' + window.location.origin + '/auth-redirect')
40-
// const url = 'https://open.weixin.qq.com/connect/qrconnect?appid=' + appid + '&redirect_uri=' + redirect_uri + '&response_type=code&scope=snsapi_login#wechat_redirect'
41-
// openWindow(url, thirdpart, 540, 540)
42-
},
43-
githubHandleClick(thirdpart) {
44-
// alert(thirdpart)
45-
console.log(thirdpart)
46-
// this.$store.commit('SET_AUTH_TYPE', thirdpart)
47-
// const client_id = 'aa6d9aa35a3d63374015'
48-
// const redirect_uri = encodeURIComponent('/redirect?redirect=' + window.location.origin + '/auth-redirect')
49-
// const url = 'https://github.com/login/oauth/authorize?client_id=' + client_id + '&redirect_uri=' + redirect_uri
50-
// const url = 'https://github.com/login/oauth/authorize?client_id=' + client_id
51-
// this.$router.push(url)
52-
// openWindow(url, thirdpart, 540, 540)
53-
}
38+
// const url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' + client_id + '&redirect_uri=' + redirect_uri
39+
const thirdPart = window.location.search === '' ? "?thirdPart=github" : "&thirdPart=github"
40+
const redirect_uri = window.location.href + thirdPart
41+
console.info(redirect_uri)
42+
return this.githubAuthBaseUri + "&redirect_uri=" + encodeURIComponent(redirect_uri)
43+
}
44+
}
45+
,
46+
methods: {
47+
wechatHandleClick(thirdpart)
48+
{
49+
alert(thirdpart)
50+
console.log(thirdpart)
51+
// this.$store.commit('SET_AUTH_TYPE', thirdpart)
52+
// const appid = 'xxxxx'
53+
// const redirect_uri = encodeURIComponent('xxx/redirect?redirect=' + window.location.origin + '/auth-redirect')
54+
// const url = 'https://open.weixin.qq.com/connect/qrconnect?appid=' + appid + '&redirect_uri=' + redirect_uri + '&response_type=code&scope=snsapi_login#wechat_redirect'
55+
// openWindow(url, thirdpart, 540, 540)
56+
}
57+
,
58+
githubHandleClick(thirdpart)
59+
{
60+
// alert(thirdpart)
61+
console.log(thirdpart)
62+
// this.$store.commit('SET_AUTH_TYPE', thirdpart)
63+
// const client_id = 'aa6d9aa35a3d63374015'
64+
// const redirect_uri = encodeURIComponent('/redirect?redirect=' + window.location.origin + '/auth-redirect')
65+
// const url = 'https://github.com/login/oauth/authorize?client_id=' + client_id + '&redirect_uri=' + redirect_uri
66+
// const url = 'https://github.com/login/oauth/authorize?client_id=' + client_id
67+
// this.$router.push(url)
68+
// openWindow(url, thirdpart, 540, 540)
5469
}
5570
}
71+
}
5672
</script>
5773

5874
<style lang="scss" scoped>

src/views/login/index.vue

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,21 @@
110110
}
111111
},
112112
created() {
113-
this.thirdPart = this.$route.params && this.$route.params.thirdPart
114-
this.oauthCode = this.$route.query && this.$route.query.code
115-
if (this.thirdPart && this.oauthCode) {
116-
this.loading = true
117-
this.$store.dispatch('user/socialLogin', {'thirdPart': this.thirdPart, 'oauthCode': this.oauthCode})
118-
.then(() => {
119-
this.$router.push({path: this.redirect || '/', query: this.otherQuery})
120-
this.loading = false
121-
})
122-
.catch(() => {
123-
this.$router.push({path: '/'})
124-
this.loading = false
125-
})
126-
127-
}
113+
// this.thirdPart = this.$route.params && this.$route.params.thirdPart
114+
// this.oauthCode = this.$route.query && this.$route.query.code
115+
// if (this.thirdPart && this.oauthCode) {
116+
// this.loading = true
117+
// this.$store.dispatch('user/socialLogin', {'thirdPart': this.thirdPart, 'oauthCode': this.oauthCode})
118+
// .then(() => {
119+
// this.$router.push({path: this.redirect || '/', query: this.otherQuery})
120+
// this.loading = false
121+
// })
122+
// .catch(() => {
123+
// this.$router.push({path: '/'})
124+
// this.loading = false
125+
// })
126+
//
127+
// }
128128
129129
console.info("xxxxxxxxxxxxxx")
130130

0 commit comments

Comments
 (0)
0