改进登录
This commit is contained in:
parent
e7ed1dbe1d
commit
54bef3d8e5
@ -9,36 +9,47 @@ import axios from "axios";
|
||||
import { useUserStore } from "@/stores/user";
|
||||
import router from "@/router";
|
||||
import config from "@/config/config";
|
||||
import { useChatStore } from "@/stores/chat";
|
||||
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
|
||||
const code = new URLSearchParams(window.location.search).get("code");
|
||||
|
||||
// 从当前页面请求中获取 code
|
||||
const code = new URLSearchParams(window.location.search).get("code");
|
||||
const q = new URLSearchParams({
|
||||
client_id: config.oauth_client_id,
|
||||
grant_type: "authorization_code",
|
||||
redirect_uri: config.oauth_callback_url,
|
||||
code_verifier: localCodeVerifier || "",
|
||||
code: code || "",
|
||||
});
|
||||
|
||||
const q = new URLSearchParams({
|
||||
client_id: config.oauth_client_id,
|
||||
grant_type: "authorization_code",
|
||||
redirect_uri: config.oauth_callback_url,
|
||||
code_verifier: localCodeVerifier || "",
|
||||
code: code || "",
|
||||
const tokenEndpoint = discovery.data.token_endpoint;
|
||||
axios
|
||||
.post(tokenEndpoint, q)
|
||||
.then((r) => {
|
||||
userStore.access_token = r.data.access_token;
|
||||
userStore.refresh_token = r.data.refresh_token;
|
||||
|
||||
userStore.login(
|
||||
r.data.id_token,
|
||||
r.data.access_token,
|
||||
r.data.refresh_token,
|
||||
r.data.expires_in,
|
||||
);
|
||||
|
||||
chatStore.getChats();
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
alert("登录失败");
|
||||
})
|
||||
.finally(() => {
|
||||
// 跳转到 /
|
||||
router.push("/");
|
||||
});
|
||||
});
|
||||
|
||||
const tokenEndpoint = discovery.data.token_endpoint;
|
||||
const r = await axios.post(tokenEndpoint, q);
|
||||
|
||||
userStore.access_token = r.data.access_token;
|
||||
userStore.refresh_token = r.data.refresh_token;
|
||||
|
||||
userStore.login(
|
||||
r.data.id_token,
|
||||
r.data.access_token,
|
||||
r.data.refresh_token,
|
||||
r.data.expires_in,
|
||||
);
|
||||
|
||||
// 跳转到 /
|
||||
router.push("/");
|
||||
</script>
|
||||
|
@ -49,5 +49,8 @@ const query = new URLSearchParams({
|
||||
code_challenge_method: "S256",
|
||||
}).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>
|
||||
|
Loading…
Reference in New Issue
Block a user