feat: auth implement

This commit is contained in:
DarkSky
2023-01-04 16:49:40 +08:00
parent 1dc475632a
commit ae94c901b3
6 changed files with 82 additions and 10 deletions
@@ -1,8 +1,8 @@
import assert from 'assert';
import { applyUpdate } from 'yjs';
import type { InitialParams } from '../index.js';
import { token, Callback } from '../../apis/index.js';
import type { ConfigStore, InitialParams, Logger } from '../index.js';
import { token, Callback, getApis } from '../../apis/index.js';
import { LocalProvider } from '../local/index.js';
import { WebsocketProvider } from './sync.js';
@@ -91,4 +91,26 @@ export class AffineProvider extends LocalProvider {
// just a workaround for yjs
doc.getMap('space:meta');
}
static async auth(config: Readonly<ConfigStore<string>>, logger: Logger) {
const refreshToken = await config.get('token');
if (refreshToken) {
await token.refreshToken(refreshToken);
if (token.isLogin && !token.isExpired) {
logger('check login success');
// login success
return;
}
}
logger('start login');
// login with google
const apis = getApis();
assert(apis.signInWithGoogle);
const user = await apis.signInWithGoogle();
assert(user);
logger(`login success: ${user.displayName}`);
// TODO: refresh local workspace data
}
}
@@ -55,6 +55,10 @@ export class BaseProvider {
return this._workspace;
}
static async auth(_config: Readonly<ConfigStore>, _logger: Logger) {
throw Error('Not implemented: auth');
}
// get workspace listreturn a map of workspace id and boolean
// if value is true, it exists locally, otherwise it does not exist locally
static async list(
@@ -1,7 +1,7 @@
import type { BlobStorage } from '@blocksuite/store';
import assert from 'assert';
import type { ConfigStore, InitialParams } from '../index.js';
import type { ConfigStore, InitialParams, Logger } from '../index.js';
import { BaseProvider } from '../base.js';
import { IndexedDBProvider } from './indexeddb.js';
@@ -32,14 +32,14 @@ export class LocalProvider extends BaseProvider {
await this._idb.whenSynced;
this._logger('Local data loaded');
await this._globalConfig.set(this._workspace.room, true);
await this._globalConfig.set(`list:${this._workspace.room}`, true);
}
async clear() {
await super.clear();
await this._blobs.clear();
await this._idb?.clearData();
await this._globalConfig.delete(this._workspace.room!);
await this._globalConfig.delete(`list:${this._workspace.room}`);
}
async destroy(): Promise<void> {
@@ -55,10 +55,18 @@ export class LocalProvider extends BaseProvider {
return this._blobs.set(blob);
}
static async auth(_config: Readonly<ConfigStore>, logger: Logger) {
logger("Local provider doesn't require authentication");
}
static async list(
config: Readonly<ConfigStore<boolean>>
): Promise<Map<string, boolean> | undefined> {
const entries = await config.entries();
return new Map(entries);
return new Map(
entries
.filter(([key]) => key.startsWith('list:'))
.map(([key, value]) => [key.slice(5), value])
);
}
}