feat: AuthService

This commit is contained in:
tiddlygit-test 2021-01-03 17:51:50 +08:00
parent 409cd3a2ae
commit f06c62c2b8
3 changed files with 71 additions and 7 deletions

View file

@ -1,15 +1,72 @@
/* eslint-disable unicorn/no-null */
import { injectable } from 'inversify';
import settings from 'electron-settings';
import { IUserInfo as IAuthingUserInfo } from '@services/types';
const defaultUserInfos = {
userName: 'TiddlyGit User',
}
authing: undefined as IAuthingUserInfo | undefined,
};
export type IUserInfos = typeof defaultUserInfos;
/**
* Handle login to Github GitLab Coding.net
*/
@injectable()
export class Authentication {
cachedUserInfo: IUserInfos;
readonly version = '2021.1';
}
constructor() {
this.cachedUserInfo = this.getInitUserInfoForCache();
}
/**
* load UserInfos in sync, and ensure it is an Object
*/
getInitUserInfoForCache = (): IUserInfos => {
let userInfosFromDisk = settings.getSync(`userInfo.${this.version}`) ?? {};
userInfosFromDisk = typeof userInfosFromDisk === 'object' && !Array.isArray(userInfosFromDisk) ? userInfosFromDisk : {};
return { ...defaultUserInfos, ...this.sanitizeUserInfo(userInfosFromDisk) };
};
private sanitizeUserInfo(info: Partial<IUserInfos>): Partial<IUserInfos> {
return info;
}
/**
* Batch update all UserInfos
*/
private async setUserInfos(newUserInfos: IUserInfos): Promise<void> {
await settings.set(`userInfos.${this.version}`, newUserInfos as any);
}
/**
* get UserInfos, may return cached version
*/
public getUserInfos = (): IUserInfos => {
// store in memory to boost performance
if (this.cachedUserInfo === undefined) {
return this.getInitUserInfoForCache();
}
return this.cachedUserInfo;
};
public get<K extends keyof IUserInfos>(key: K): IUserInfos[K] | undefined {
if (this.cachedUserInfo[key] !== null && this.cachedUserInfo[key] !== undefined) {
return this.cachedUserInfo[key];
}
}
public async reset(): Promise<void> {
await settings.unset();
const UserInfos = this.getUserInfos();
this.cachedUserInfo = UserInfos;
await this.setUserInfos(UserInfos);
// TODO: sendToAllWindows
// Object.keys(UserInfos).forEach((key) => {
// const value = UserInfos[key as keyof IUserInfos];
// this.windowService.sendToAllWindows(UserInfoChannel.update, key, value);
// });
}
}

View file

@ -1,4 +1,5 @@
export default {
Authentication: Symbol.for('Authentication'),
Window: Symbol.for('Window'),
Preference: Symbol.for('Preference'),
Notification: Symbol.for('Notification'),

View file

@ -10,6 +10,7 @@ import chokidar from 'chokidar';
import { trim, compact, debounce } from 'lodash';
import serviceIdentifiers from '@services/serviceIdentifier';
import { Authentication } from '@services/auth';
import { Window } from '@services/windows';
import { Preference } from '@services/preferences';
import { View } from '@services/view';
@ -28,6 +29,7 @@ import { updateSubWikiPluginContent } from './update-plugin-content';
@injectable()
export class Wiki {
constructor(
@inject(serviceIdentifiers.Authentication) private readonly authService: Authentication,
@inject(serviceIdentifiers.Window) private readonly windowService: Window,
@inject(serviceIdentifiers.Preference) private readonly preferenceService: Preference,
@inject(serviceIdentifiers.Workspace) private readonly workspaceService: Workspace,
@ -325,17 +327,21 @@ export class Wiki {
// do nothing
}
const userName = this.preferenceService.get('userName');
const userInfo = this.preferenceService.get('github-user-info');
const userName = this.authService.get('userName') ?? '';
const userInfo = this.authService.get('authing');
const { name: wikiPath, gitUrl: githubRepoUrl, port, isSubWiki, id } = workspace;
// if is main wiki
if (!isSubWiki) {
this.setWikiStarted(wikiPath);
await this.startNodeJSWiki(wikiPath, port, userName, id);
userInfo && watchWiki(wikiPath, githubRepoUrl, userInfo, path.join(wikiPath, TIDDLERS_PATH));
if (userInfo !== undefined) {
await this.watchWiki(wikiPath, githubRepoUrl, userInfo, path.join(wikiPath, TIDDLERS_PATH));
}
} else {
// if is private repo wiki
userInfo && watchWiki(wikiPath, githubRepoUrl, userInfo);
if (userInfo !== undefined) {
await this.watchWiki(wikiPath, githubRepoUrl, userInfo);
}
// if we are creating a sub-wiki, restart the main wiki to load content from private wiki
const mainWikiPath = workspace.mainWikiToLink;
if (!this.justStartedWiki[mainWikiPath]) {