oak-general-business/es/components/oauth/index.js

72 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from "assert";
export default OakComponent({
// Virtual Component
isList: false,
filters: [],
properties: {
onRetry: null,
onSuccess: null,
},
data: {
hasError: false,
errorMessage: '',
loading: true,
},
lifetimes: {
async ready() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code');
const state = urlParams.get('state');
const error = urlParams.get('error');
const errorDescription = urlParams.get('error_description');
if (error) {
this.setErrorMsg(errorDescription || 'OAuth authorization error: ' + error);
this.setState({ loading: false });
return;
}
assert(state, 'State parameter is missing');
assert(code, 'Code parameter is missing');
this.setState({ hasError: false, errorMessage: '' });
if (!state) {
this.setErrorMsg('Invalid state parameter');
return;
}
this.performLogin(code, state);
},
},
methods: {
performLogin(code, state) {
this.features.token.loginByOAuth(code, state).then(() => {
console.log('OAuth login successful');
}).catch((err) => {
console.error('OAuth login failed:', err);
this.setErrorMsg(err.message || 'OAuth login failed');
}).finally(() => {
this.setState({ loading: false });
});
},
setErrorMsg(message) {
if (!message) {
this.setState({ hasError: false, errorMessage: '' });
return;
}
this.setState({ hasError: true, errorMessage: message });
},
retry() {
if (!this.props.onRetry) {
console.error('未设置失败重试回调函数请在使用Oauth组件时配置onRetry属性');
return;
}
this.props.onRetry();
},
returnToIndex() {
if (!this.props.onSuccess) {
console.error('未设置成功回调函数请在使用Oauth组件时配置onSuccess属性');
return;
}
this.props.onSuccess();
}
}
});