Support cred auto-login.

This commit is contained in:
Yang Luo
2021-05-16 18:18:55 +08:00
parent 338c589e51
commit 18806f07a8
6 changed files with 55 additions and 23 deletions

View File

@ -109,7 +109,17 @@ class App extends Component {
getAccessTokenParam() {
// "/page?access_token=123"
const params = new URLSearchParams(this.props.location.search);
return params.get("access_token");
const accessToken = params.get("access_token");
return accessToken === null ? "" : `?accessToken=${accessToken}`;
}
getCredentialParams() {
// "/page?username=abc&password=123"
const params = new URLSearchParams(this.props.location.search);
if (params.get("username") === null || params.get("password") === null) {
return "";
}
return `?username=${params.get("username")}&password=${params.get("password")}`;
}
getUrlWithoutQuery() {
@ -118,18 +128,21 @@ class App extends Component {
}
getAccount() {
const accessToken = this.getAccessTokenParam();
if (accessToken !== null) {
let query = this.getAccessTokenParam();
if (query === "") {
query = this.getCredentialParams();
}
if (query !== "") {
window.history.replaceState({}, document.title, this.getUrlWithoutQuery());
}
AuthBackend.getAccount(accessToken)
AuthBackend.getAccount(query)
.then((res) => {
let account = null;
if (res.status === "ok") {
account = res.data;
account.organization = res.data2;
} else {
if (res.msg === "Invalid JWT token") {
if (res.msg !== "Please sign in first") {
Setting.showMessage("error", `Failed to sign in: ${res.msg}`);
}
}

View File

@ -14,9 +14,8 @@
import {authConfig} from "./Auth";
export function getAccount(accessToken) {
let param = (accessToken === null) ? "" : `?accessToken=${accessToken}`;
return fetch(`${authConfig.serverUrl}/api/get-account${param}`, {
export function getAccount(query) {
return fetch(`${authConfig.serverUrl}/api/get-account${query}`, {
method: 'GET',
credentials: 'include'
}).then(res => res.json());

View File

@ -65,7 +65,7 @@ export function deleteUser(user) {
export function uploadAvatar(avatar) {
let account;
AuthBackend.getAccount(null).then((res) => {
AuthBackend.getAccount("").then((res) => {
account = res.data;
let formData = new FormData();
formData.append("avatarfile", avatar);