mirror of
https://github.com/songquanpeng/one-api.git
synced 2026-03-05 19:24:25 +08:00
* feat: add the ui for configuring the third-party standard OAuth2.0/OIDC. - update SystemSetting.js - add setup ui - add configuration * feat: add the ui for "allow the OAuth 2.0 to login" - update SystemSetting.js * feat: add OAuth 2.0 web ui and its process functions - update common.js - update AuthLogin.js - update config.js * fix: missing "Userinfo" endpoint configuration entry, used by OAuth clients to request user information from the IdP. - update config.js - update SystemSetting.js * feat: updated the icons for Lark and OIDC to match the style of the icons for WeChat, EMail, GitHub. - update lark.svg - new oidc.svg * refactor: Changing OAuth 2.0 to OIDC * feat: add OIDC login method * feat: Add support for OIDC login to the backend * fix: Change the AppId and AppSecret on the Web UI to the standard usage: ClientId, ClientSecret. * feat: Support quick configuration of OIDC through Well-Known Discovery Endpoint * feat: Standardize terminology, add well-known configuration - Change the AppId and AppSecret on the Server End to the standard usage: ClientId, ClientSecret. - add Well-Known configuration to store in database, no actual use in server end but store and display in web ui only
123 lines
3.6 KiB
JavaScript
123 lines
3.6 KiB
JavaScript
import { API } from 'utils/api';
|
|
import { useDispatch } from 'react-redux';
|
|
import { LOGIN } from 'store/actions';
|
|
import { useNavigate } from 'react-router';
|
|
import { showSuccess } from 'utils/common';
|
|
|
|
const useLogin = () => {
|
|
const dispatch = useDispatch();
|
|
const navigate = useNavigate();
|
|
const login = async (username, password) => {
|
|
try {
|
|
const res = await API.post(`/api/user/login`, {
|
|
username,
|
|
password
|
|
});
|
|
const { success, message, data } = res.data;
|
|
if (success) {
|
|
localStorage.setItem('user', JSON.stringify(data));
|
|
dispatch({ type: LOGIN, payload: data });
|
|
navigate('/panel');
|
|
}
|
|
return { success, message };
|
|
} catch (err) {
|
|
// 请求失败,设置错误信息
|
|
return { success: false, message: '' };
|
|
}
|
|
};
|
|
|
|
const githubLogin = async (code, state) => {
|
|
try {
|
|
const res = await API.get(`/api/oauth/github?code=${code}&state=${state}`);
|
|
const { success, message, data } = res.data;
|
|
if (success) {
|
|
if (message === 'bind') {
|
|
showSuccess('绑定成功!');
|
|
navigate('/panel');
|
|
} else {
|
|
dispatch({ type: LOGIN, payload: data });
|
|
localStorage.setItem('user', JSON.stringify(data));
|
|
showSuccess('登录成功!');
|
|
navigate('/panel');
|
|
}
|
|
}
|
|
return { success, message };
|
|
} catch (err) {
|
|
// 请求失败,设置错误信息
|
|
return { success: false, message: '' };
|
|
}
|
|
};
|
|
|
|
const larkLogin = async (code, state) => {
|
|
try {
|
|
const res = await API.get(`/api/oauth/lark?code=${code}&state=${state}`);
|
|
const { success, message, data } = res.data;
|
|
if (success) {
|
|
if (message === 'bind') {
|
|
showSuccess('绑定成功!');
|
|
navigate('/panel');
|
|
} else {
|
|
dispatch({ type: LOGIN, payload: data });
|
|
localStorage.setItem('user', JSON.stringify(data));
|
|
showSuccess('登录成功!');
|
|
navigate('/panel');
|
|
}
|
|
}
|
|
return { success, message };
|
|
} catch (err) {
|
|
// 请求失败,设置错误信息
|
|
return { success: false, message: '' };
|
|
}
|
|
};
|
|
|
|
const oidcLogin = async (code, state) => {
|
|
try {
|
|
const res = await API.get(`/api/oauth/oidc?code=${code}&state=${state}`);
|
|
const { success, message, data } = res.data;
|
|
if (success) {
|
|
if (message === 'bind') {
|
|
showSuccess('绑定成功!');
|
|
navigate('/panel');
|
|
} else {
|
|
dispatch({ type: LOGIN, payload: data });
|
|
localStorage.setItem('user', JSON.stringify(data));
|
|
showSuccess('登录成功!');
|
|
navigate('/panel');
|
|
}
|
|
}
|
|
return { success, message };
|
|
} catch (err) {
|
|
// 请求失败,设置错误信息
|
|
return { success: false, message: '' };
|
|
}
|
|
}
|
|
|
|
const wechatLogin = async (code) => {
|
|
try {
|
|
const res = await API.get(`/api/oauth/wechat?code=${code}`);
|
|
const { success, message, data } = res.data;
|
|
if (success) {
|
|
dispatch({ type: LOGIN, payload: data });
|
|
localStorage.setItem('user', JSON.stringify(data));
|
|
showSuccess('登录成功!');
|
|
navigate('/panel');
|
|
}
|
|
return { success, message };
|
|
} catch (err) {
|
|
// 请求失败,设置错误信息
|
|
return { success: false, message: '' };
|
|
}
|
|
};
|
|
|
|
const logout = async () => {
|
|
await API.get('/api/user/logout');
|
|
localStorage.removeItem('user');
|
|
dispatch({ type: LOGIN, payload: null });
|
|
navigate('/');
|
|
};
|
|
|
|
return { login, logout, githubLogin, wechatLogin, larkLogin,oidcLogin };
|
|
};
|
|
|
|
export default useLogin;
|