Browse Source

add:完善第三方登录

master
秋田弘 7 months ago
parent
commit
3308931168
  1. 60
      frontend/src/components/desktop/LockDesktop.vue

60
frontend/src/components/desktop/LockDesktop.vue

@ -156,7 +156,9 @@
import router from "@/system/router"; import router from "@/system/router";
import { RestartApp } from "@/util/goutil"; import { RestartApp } from "@/util/goutil";
import { notifyError } from "@/util/msg"; import { notifyError } from "@/util/msg";
import { onMounted, ref } from "vue"; import { onMounted, ref, watchEffect } from "vue";
import { useRoute } from "vue-router";
const route = useRoute();
const store = useLoginStore(); const store = useLoginStore();
const sys = useSystem(); const sys = useSystem();
const loginCallback = sys._options.loginCallback; const loginCallback = sys._options.loginCallback;
@ -207,13 +209,14 @@
} }
}); });
// code // 使 watchEffect code
watch( watchEffect(() => {
() => router.currentRoute.value.query.code, const code = route.query.code;
() => { if (code) {
console.log(code, "---");
onLogin(); onLogin();
} }
); });
const onLogin = async () => { const onLogin = async () => {
localStorage.removeItem("godoosClientId"); localStorage.removeItem("godoosClientId");
@ -238,8 +241,6 @@
const returnedState = router.currentRoute.value.query const returnedState = router.currentRoute.value.query
.state as string; .state as string;
console.log(returnedState, "---", store.State);
if (returnedState !== store.State) { if (returnedState !== store.State) {
notifyError("登录失败,请重试"); notifyError("登录失败,请重试");
return; return;
@ -405,8 +406,6 @@
const success = await loginFunction(); const success = await loginFunction();
if (success) { if (success) {
loginSuccess(); loginSuccess();
} else {
notifyError("登录失败");
} }
} }
}; };
@ -415,20 +414,35 @@
// stateCSRF,使 // stateCSRF,使
const state = Date.now() + Math.random().toString(36).substring(2, 15); const state = Date.now() + Math.random().toString(36).substring(2, 15);
store.State = state; store.State = state;
// url
const currentUrl = window.location.href;
const url = config.userInfo.url + "/github/authorize?state=" + state; const url = config.userInfo.url + "/github/authorize?state=" + state;
const res: any = await fetch(url, { const res: any = await fetch(url, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
state: state, state: state,
redirect_url: currentUrl,
}), }),
}); });
if (!res.ok) { if (!res.ok) {
return false; return false;
} }
const data = await res.json(); const data = await res.json();
console.log(data.data.url); if (data && data.data && data.data.url) {
window.location.href = data.data.url; console.log(data.data.url, "---");
return true; // 使URL
const urlPattern = /client_id=[^&]+/;
if (urlPattern.test(data.data.url)) {
window.location.href = data.data.url;
return true;
} else {
notifyError("请先在系统配置中设置github登陆配置");
return false;
}
} else {
notifyError("获取授权URL失败");
return false;
}
}; };
const authWithWechat = (): boolean | PromiseLike<boolean> => { const authWithWechat = (): boolean | PromiseLike<boolean> => {
@ -447,20 +461,34 @@
// stateCSRF,使 // stateCSRF,使
const state = Date.now() + Math.random().toString(36).substring(2, 15); const state = Date.now() + Math.random().toString(36).substring(2, 15);
store.State = state; store.State = state;
// url
const currentUrl = window.location.href;
const url = config.userInfo.url + "/gitee/authorize?state=" + state; const url = config.userInfo.url + "/gitee/authorize?state=" + state;
const res: any = await fetch(url, { const res: any = await fetch(url, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
state: state, state: state,
redirect_url: currentUrl,
}), }),
}); });
if (!res.ok) { if (!res.ok) {
return false; return false;
} }
const data = await res.json(); const data = await res.json();
// Gitee if (data && data.data && data.data.url) {
window.location.href = data.data.url; // 使URL
return true; const urlPattern = /client_id=[^&]+/;
if (urlPattern.test(data.data.url)) {
window.location.href = data.data.url;
return true;
} else {
notifyError("请先在系统配置中设置gitee登陆配置");
return false;
}
} else {
notifyError("获取授权URL失败");
return false;
}
}; };
</script> </script>

Loading…
Cancel
Save