改进登录
This commit is contained in:
parent
e7ed1dbe1d
commit
54bef3d8e5
@ -9,10 +9,11 @@ import axios from "axios";
|
|||||||
import { useUserStore } from "@/stores/user";
|
import { useUserStore } from "@/stores/user";
|
||||||
import router from "@/router";
|
import router from "@/router";
|
||||||
import config from "@/config/config";
|
import config from "@/config/config";
|
||||||
|
import { useChatStore } from "@/stores/chat";
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
|
const chatStore = useChatStore();
|
||||||
|
|
||||||
const discovery = await axios.get(config.oauth_discovery_url);
|
axios.get(config.oauth_discovery_url).then((discovery) => {
|
||||||
|
|
||||||
const localCodeVerifier = localStorage.getItem(config.oauth_storage_key);
|
const localCodeVerifier = localStorage.getItem(config.oauth_storage_key);
|
||||||
|
|
||||||
// 从当前页面请求中获取 code
|
// 从当前页面请求中获取 code
|
||||||
@ -27,8 +28,9 @@ const q = new URLSearchParams({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const tokenEndpoint = discovery.data.token_endpoint;
|
const tokenEndpoint = discovery.data.token_endpoint;
|
||||||
const r = await axios.post(tokenEndpoint, q);
|
axios
|
||||||
|
.post(tokenEndpoint, q)
|
||||||
|
.then((r) => {
|
||||||
userStore.access_token = r.data.access_token;
|
userStore.access_token = r.data.access_token;
|
||||||
userStore.refresh_token = r.data.refresh_token;
|
userStore.refresh_token = r.data.refresh_token;
|
||||||
|
|
||||||
@ -39,6 +41,15 @@ userStore.login(
|
|||||||
r.data.expires_in,
|
r.data.expires_in,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
chatStore.getChats();
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.log(e);
|
||||||
|
alert("登录失败");
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
// 跳转到 /
|
// 跳转到 /
|
||||||
router.push("/");
|
router.push("/");
|
||||||
|
});
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
@ -49,5 +49,8 @@ const query = new URLSearchParams({
|
|||||||
code_challenge_method: "S256",
|
code_challenge_method: "S256",
|
||||||
}).toString();
|
}).toString();
|
||||||
|
|
||||||
window.location.href = discovery.data.authorization_endpoint + "?" + query;
|
// 部分浏览器可能无法跳转,创建一个 a 元素然后模拟点击
|
||||||
|
const a = document.createElement("a");
|
||||||
|
a.href = discovery.data.authorization_endpoint + "?" + query;
|
||||||
|
a.click();
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user