Add getOAuthGetParameters().

This commit is contained in:
Yang Luo 2021-03-20 16:51:10 +08:00
parent 63a4066a8d
commit f89f454e0e
4 changed files with 35 additions and 14 deletions

View File

@ -29,15 +29,19 @@ export function register(values) {
}).then(res => res.json());
}
export function getApplicationLogin(clientId, responseType, redirectUri, scope, state) {
return fetch(`${authConfig.serverUrl}/api/get-app-login?clientId=${clientId}&responseType=${responseType}&redirectUri=${redirectUri}&scope=${scope}&state=${state}`, {
function oAuthParamsToQuery(oAuthParams) {
return `?clientId=${oAuthParams.clientId}&responseType=${oAuthParams.responseType}&redirectUri=${oAuthParams.redirectUri}&scope=${oAuthParams.scope}&state=${oAuthParams.state}`;
}
export function getApplicationLogin(oAuthParams) {
return fetch(`${authConfig.serverUrl}/api/get-app-login${oAuthParamsToQuery(oAuthParams)}`, {
method: 'GET',
credentials: 'include',
}).then(res => res.json());
}
export function login(values) {
return fetch(`${authConfig.serverUrl}/api/login`, {
export function login(values, oAuthParams) {
return fetch(`${authConfig.serverUrl}/api/login${oAuthParamsToQuery(oAuthParams)}`, {
method: 'POST',
credentials: "include",
body: JSON.stringify(values),

View File

@ -46,7 +46,8 @@ class AuthCallback extends React.Component {
redirectUri: redirectUri,
method: this.state.method,
};
AuthBackend.login(body)
const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.login(body, oAuthParams)
.then((res) => {
if (res.status === "ok") {
window.location.href = '/';

View File

@ -42,13 +42,8 @@ class Face extends React.Component {
}
getApplicationLogin() {
const queries = new URLSearchParams(window.location.search);
const clientId = queries.get("client_id");
const responseType = queries.get("response_type");
const redirectUri = queries.get("redirect_uri");
const scope = queries.get("scope");
const state = queries.get("state");
AuthBackend.getApplicationLogin(clientId, responseType, redirectUri, scope, state)
const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.getApplicationLogin(oAuthParams)
.then((res) => {
if (res.status === "ok") {
this.setState({
@ -86,8 +81,9 @@ class Face extends React.Component {
}
onFinish(values) {
values.type = this.state.type;
AuthBackend.login(values)
values["type"] = this.state.type;
const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.login(values, oAuthParams)
.then((res) => {
if (res.status === 'ok') {
if (this.state.type === "login") {

View File

@ -42,3 +42,23 @@ export function trim(str, ch) {
return (start > 0 || end < str.length) ? str.substring(start, end) : str;
}
export function getOAuthGetParameters() {
const queries = new URLSearchParams(window.location.search);
const clientId = queries.get("client_id");
const responseType = queries.get("response_type");
const redirectUri = queries.get("redirect_uri");
const scope = queries.get("scope");
const state = queries.get("state");
if (clientId === undefined) {
return null;
} else {
return {
clientId: clientId,
responseType: responseType,
redirectUri: redirectUri,
scope: scope,
state: state,
};
}
}