casdoor/web/src/auth/AuthBackend.js

102 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-02-13 12:15:19 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-02-14 16:59:08 +08:00
import {authConfig} from "./Auth";
2021-02-14 15:51:56 +08:00
2021-05-16 18:18:55 +08:00
export function getAccount(query) {
return fetch(`${authConfig.serverUrl}/api/get-account${query}`, {
method: "GET",
2022-08-06 23:54:56 +08:00
credentials: "include",
2021-02-13 12:15:19 +08:00
}).then(res => res.json());
}
2021-04-27 22:47:44 +08:00
export function signup(values) {
return fetch(`${authConfig.serverUrl}/api/signup`, {
method: "POST",
2021-02-13 12:15:19 +08:00
credentials: "include",
body: JSON.stringify(values),
}).then(res => res.json());
}
export function getEmailAndPhone(values) {
return fetch(`${authConfig.serverUrl}/api/get-email-and-phone`, {
method: "POST",
credentials: "include",
body: JSON.stringify(values),
}).then((res) => res.json());
}
2021-03-20 16:51:10 +08:00
function oAuthParamsToQuery(oAuthParams) {
2021-08-01 11:20:06 +08:00
// login
if (oAuthParams === null) {
return "";
}
// code
return `?clientId=${oAuthParams.clientId}&responseType=${oAuthParams.responseType}&redirectUri=${oAuthParams.redirectUri}&scope=${oAuthParams.scope}&state=${oAuthParams.state}&nonce=${oAuthParams.nonce}&code_challenge_method=${oAuthParams.challengeMethod}&code_challenge=${oAuthParams.codeChallenge}`;
2021-03-20 16:51:10 +08:00
}
export function getApplicationLogin(oAuthParams) {
return fetch(`${authConfig.serverUrl}/api/get-app-login${oAuthParamsToQuery(oAuthParams)}`, {
method: "GET",
credentials: "include",
2021-03-20 11:34:04 +08:00
}).then(res => res.json());
}
2021-03-20 16:51:10 +08:00
export function login(values, oAuthParams) {
return fetch(`${authConfig.serverUrl}/api/login${oAuthParamsToQuery(oAuthParams)}`, {
method: "POST",
2021-02-13 12:15:19 +08:00
credentials: "include",
body: JSON.stringify(values),
}).then(res => res.json());
}
export function loginCas(values, params) {
return fetch(`${authConfig.serverUrl}/api/login?service=${params.service}`, {
method: "POST",
credentials: "include",
body: JSON.stringify(values),
}).then(res => res.json());
}
2021-02-13 12:15:19 +08:00
export function logout() {
2021-02-14 16:59:08 +08:00
return fetch(`${authConfig.serverUrl}/api/logout`, {
method: "POST",
2021-02-13 12:15:19 +08:00
credentials: "include",
}).then(res => res.json());
}
2021-02-14 00:22:24 +08:00
2021-04-18 23:14:46 +08:00
export function unlink(values) {
return fetch(`${authConfig.serverUrl}/api/unlink`, {
method: "POST",
2021-04-18 23:14:46 +08:00
credentials: "include",
body: JSON.stringify(values),
2021-02-14 16:59:08 +08:00
}).then(res => res.json());
}
export function getSamlLogin(providerId, relayState) {
return fetch(`${authConfig.serverUrl}/api/get-saml-login?id=${providerId}&relayState=${relayState}`, {
method: "GET",
credentials: "include",
}).then(res => res.json());
}
export function loginWithSaml(values, param) {
return fetch(`${authConfig.serverUrl}/api/login${param}`, {
method: "POST",
credentials: "include",
body: JSON.stringify(values),
}).then(res => res.json());
}