mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 01:04:25 +08:00
support Nextcloud OAuth 2.0 authentication
This commit is contained in:
+57
-1
@@ -21,8 +21,8 @@ import type {
|
||||
UserProfileUpdateRequest,
|
||||
UserProfileUpdateResponse
|
||||
} from '@/models/user.ts';
|
||||
import type { LocalizedPresetCategory } from '@/core/category.ts';
|
||||
import type { ForgetPasswordRequest } from '@/models/forget_password.ts';
|
||||
import type { LocalizedPresetCategory } from '@/core/category.ts';
|
||||
|
||||
import {
|
||||
isObject,
|
||||
@@ -77,6 +77,10 @@ export const useRootStore = defineStore('root', () => {
|
||||
currentNotification.value = content;
|
||||
}
|
||||
|
||||
function generateOAuth2LoginUrl(platform: 'mobile' | 'desktop', clientSessionId: string): string {
|
||||
return services.generateOAuth2LoginUrl(platform, clientSessionId);
|
||||
}
|
||||
|
||||
function authorize(req: UserLoginRequest): Promise<AuthResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.authorize(req).then(response => {
|
||||
@@ -187,6 +191,56 @@ export const useRootStore = defineStore('root', () => {
|
||||
});
|
||||
}
|
||||
|
||||
function authorizeOAuth2({ provider, password, token }: { provider: string, password?: string, token: string }): Promise<AuthResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.authorizeOAuth2({
|
||||
req: {
|
||||
provider,
|
||||
password
|
||||
},
|
||||
token
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result || !data.result.token) {
|
||||
reject({ message: 'Unable to log in' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (settingsStore.appSettings.applicationLock || hasUserAppLockState()) {
|
||||
const appLockState = getUserAppLockState();
|
||||
|
||||
if (!appLockState || appLockState.username !== data.result.user?.username) {
|
||||
clearCurrentTokenAndUserInfo(true);
|
||||
settingsStore.setEnableApplicationLock(false);
|
||||
settingsStore.setEnableApplicationLockWebAuthn(false);
|
||||
clearWebAuthnConfig();
|
||||
}
|
||||
}
|
||||
|
||||
settingsStore.setApplicationSettingsFromCloudSettings(data.result.applicationCloudSettings);
|
||||
|
||||
updateCurrentToken(data.result.token);
|
||||
|
||||
if (data.result.user && isObject(data.result.user)) {
|
||||
userStore.storeUserBasicInfo(data.result.user);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to login', error);
|
||||
|
||||
if (error && error.processed) {
|
||||
reject(error);
|
||||
} else if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else {
|
||||
reject({ message: 'Unable to log in' });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function register({ user, presetCategories }: { user: User, presetCategories?: LocalizedPresetCategory[] }): Promise<RegisterResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.register(user.toRegisterRequest(presetCategories)).then(response => {
|
||||
@@ -588,8 +642,10 @@ export const useRootStore = defineStore('root', () => {
|
||||
currentNotification,
|
||||
// functions
|
||||
setNotificationContent,
|
||||
generateOAuth2LoginUrl,
|
||||
authorize,
|
||||
authorize2FA,
|
||||
authorizeOAuth2,
|
||||
register,
|
||||
lock,
|
||||
logout,
|
||||
|
||||
Reference in New Issue
Block a user