Fix AuthCallback's code handling.

This commit is contained in:
Yang Luo
2021-03-21 00:38:00 +08:00
parent 8b921b2c1e
commit 7f367ae346
4 changed files with 38 additions and 7 deletions

View File

@ -334,7 +334,7 @@ class App extends Component {
if (this.isDoorPages()) {
return (
<Switch>
<Route exact path="/login/oauth/authorize" render={(props) => this.renderLoginIfNotLoggedIn(<Face type={"code"} {...props} />)}/>
<Route exact path="/login/oauth/authorize" render={(props) => <Face type={"code"} {...props} />}/>
</Switch>
)
}

View File

@ -17,6 +17,7 @@ import {Spin} from "antd";
import {withRouter} from "react-router-dom";
import * as AuthBackend from "./AuthBackend";
import * as Util from "./Util";
import {authConfig} from "./Auth";
class AuthCallback extends React.Component {
constructor(props) {
@ -29,10 +30,29 @@ class AuthCallback extends React.Component {
};
}
getResponseType() {
// "http://localhost:8000"
const authServerUrl = authConfig.serverUrl;
// For example, for Casbin-OA, realRedirectUri = "http://localhost:9000/login"
// realRedirectUrl = "http://localhost:9000"
const params = new URLSearchParams(this.props.location.search);
const realRedirectUri = params.get("redirect_uri");
const realRedirectUrl = new URL(realRedirectUri).origin;
// For Casdoor itself, we use "login" directly
if (authServerUrl === realRedirectUrl) {
return "login";
} else {
return "code";
}
}
componentWillMount() {
const params = new URLSearchParams(this.props.location.search);
let redirectUri = `${window.location.origin}/callback/${this.state.applicationName}/${this.state.providerName}/${this.state.method}`;
const body = {
type: this.getResponseType(),
application: this.state.applicationName,
provider: this.state.providerName,
code: params.get("code"),
@ -43,10 +63,19 @@ class AuthCallback extends React.Component {
const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.login(body, oAuthParams)
.then((res) => {
if (res.status === "ok") {
window.location.href = '/';
if (res.status === 'ok') {
const responseType = this.getResponseType();
if (responseType === "login") {
// this.props.onLoggedIn();
Util.showMessage("success", `Logged in successfully`);
Util.goToLink("/");
} else if (responseType === "code") {
const code = res.data;
Util.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`);
// Util.showMessage("success", `Authorization code: ${res.data}`);
}
} else {
Util.showMessage("error", res?.msg);
Util.showMessage("error", `Log in failed${res.msg}`);
}
});
}

View File

@ -86,11 +86,12 @@ class Face extends React.Component {
AuthBackend.login(values, oAuthParams)
.then((res) => {
if (res.status === 'ok') {
if (this.state.type === "login") {
const responseType = this.state.type;
if (responseType === "login") {
this.props.onLoggedIn();
Util.showMessage("success", `Logged in successfully`);
Util.goToLink("/");
} else if (this.state.type === "code") {
} else if (responseType === "code") {
const code = res.data;
Util.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`);
// Util.showMessage("success", `Authorization code: ${res.data}`);
@ -221,7 +222,7 @@ class Face extends React.Component {
{
application.providerObjs.map(provider => {
return (
<a href={Provider.getAuthUrl(application, provider, "signup")}>
<a key={provider.displayName} href={Provider.getAuthUrl(application, provider, "signup")}>
<img width={30} height={30} src={Provider.getAuthLogo(provider)} alt={provider.displayName} style={{margin: "3px"}} />
</a>
);