feat: support google cloud login in client (#1822)

Co-authored-by: Himself65 <himself65@outlook.com>
Co-authored-by: Peng Xiao <pengxiao@outlook.com>
This commit is contained in:
Horus
2023-04-12 02:42:36 +08:00
committed by GitHub
parent 024c469a2c
commit c0669359ed
17 changed files with 252 additions and 42 deletions

View File

@@ -1,4 +1,5 @@
import { DebugLogger } from '@affine/debug';
import { getEnvironment } from '@affine/env';
import { assertExists } from '@blocksuite/global/utils';
import { Slot } from '@blocksuite/store';
import { initializeApp } from 'firebase/app';
@@ -9,6 +10,7 @@ import {
getAuth as getFirebaseAuth,
GithubAuthProvider,
GoogleAuthProvider,
signInWithCredential,
signInWithPopup,
} from 'firebase/auth';
import { decode } from 'js-base64';
@@ -64,6 +66,13 @@ export const setLoginStorage = (login: LoginResponse) => {
);
};
const signInWithElectron = async (firebaseAuth: FirebaseAuth) => {
const code = await window.apis?.googleSignIn();
const credential = GoogleAuthProvider.credential(code);
const user = await signInWithCredential(firebaseAuth, credential);
return await user.user.getIdToken();
};
export const clearLoginStorage = () => {
localStorage.removeItem(STORAGE_KEY);
};
@@ -152,6 +161,7 @@ export function createAffineAuth(prefix = '/') {
method: SignMethod
): Promise<LoginResponse | null> => {
const auth = getAuth();
const environment = getEnvironment();
if (!auth) {
throw new Error('Failed to initialize firebase');
}
@@ -167,9 +177,14 @@ export function createAffineAuth(prefix = '/') {
throw new Error('Unsupported sign method');
}
try {
const response = await signInWithPopup(auth, provider);
const idToken = await response.user.getIdToken();
logger.debug(idToken);
let idToken: string | undefined;
if (environment.isDesktop) {
idToken = await signInWithElectron(auth);
} else {
const response = await signInWithPopup(auth, provider);
idToken = await response.user.getIdToken();
}
logger.debug('idToken', idToken);
return fetch(prefix + 'api/user/token', {
method: 'POST',
headers: {

View File

@@ -1,3 +1,5 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path='../../../apps/electron/layers/preload/preload.d.ts' />
import type { Workspace as RemoteWorkspace } from '@affine/workspace/affine/api';
import type { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import type { FC, PropsWithChildren } from 'react';