Save localStorage data into google drive for cross-access database in SPA.
- Follow this link, and make 'web' client type.
- set
clientId
inconfig.js
// config.js
const clientId = '<your client id>';
- If you want to enable 'Authorization Code Flow', add
redirectUrl
inconfig.js
andclientSecret
insecret.js
. Notice that leakingclientSecret
is vulnerable.
// config.js
const clientId = '<your client id>';
const redirectUrl = '<redirect page url>';
// secret.js
const clientSecret = '<your client secret>';
const googleDriveSync = new GoogleDriveSync({
useOffline: true, // require redirectUrl, clientSecret
saveRefreshToken: true,
});
Methods
- local storage methods
- load(key)
- save(key, value)
- remove(key)
- google methods
- initGoogleLibrary
- login
- logout
- remote storage methods
- loadRemote(key)
- loadRemoteForce(key)
- saveRemote(key, value)
- removeRemote(key)
- syncRemote()
Events
- SyncReady
- UserLogout
- TokenExpired
const googleDriveSync = new GoogleDriveSync({
useOffline: true,
saveRefreshToken: true,
});
googleDriveSync.initGoogleLibrary();
const button = document.querySelector('button');
button.addEventListener('click', () => {
googleDriveSync.login();
});
window.addEventListener('SyncReady', async () => {
const localFoo = googleDriveSync.load('foo');
const remoteFoo = await googleDriveSync.loadRemote('foo');
const localFoo2 = googleDriveSync.load('foo'); // remoteFoo and localFoo2 are same
await googleDriveSync.saveRemote('foo', { hello: 'world' });
await googleDriveSync.saveRemote('foo', { hello: 'world' }); // ignored
googleDriveSync.save('bar', {}); // no remote sync
googleDriveSync.save('foo', { asdf: 'asdf' }); // no remote sync
await googleDriveSync.saveRemote('baz', 'baz'); // sync 'bar', 'foo', 'baz' at this time
});
- Implement methods to resolve conflict
- Fix some bugs when data files have deleted in google drive by user
- Please remove the index file to fix this bug