refactor(server): auth (#5895)

Remove `next-auth` and implement our own Authorization/Authentication system from scratch.

## Server

- [x] tokens
  - [x] function
  - [x] encryption

- [x] AuthController
  - [x] /api/auth/sign-in
  - [x] /api/auth/sign-out
  - [x] /api/auth/session
  - [x] /api/auth/session (WE SUPPORT MULTI-ACCOUNT!)

- [x] OAuthPlugin
  - [x] OAuthController
  - [x] /oauth/login
  - [x] /oauth/callback
  - [x] Providers
    - [x] Google
    - [x] GitHub

## Client

- [x] useSession
- [x] cloudSignIn
- [x] cloudSignOut

## NOTE:

Tests will be adding in the future
This commit is contained in:
liuyi
2024-03-12 10:00:09 +00:00
parent af49e8cc41
commit fb3a0e7b8f
148 changed files with 3407 additions and 2851 deletions

View File

@@ -21,7 +21,6 @@
"is-svg": "^5.0.0",
"lodash-es": "^4.17.21",
"nanoid": "^5.0.6",
"next-auth": "^4.24.5",
"socket.io-client": "^4.7.4",
"y-protocols": "^1.0.6",
"yjs": "^13.6.12"

View File

@@ -2,6 +2,7 @@ import { WorkspaceFlavour } from '@affine/env/workspace';
import {
createWorkspaceMutation,
deleteWorkspaceMutation,
findGraphQLError,
getWorkspacesQuery,
} from '@affine/graphql';
import { fetcher } from '@affine/graphql';
@@ -16,7 +17,6 @@ import {
import { globalBlockSuiteSchema } from '@toeverything/infra';
import { difference } from 'lodash-es';
import { nanoid } from 'nanoid';
import { getSession } from 'next-auth/react';
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
import { IndexedDBBlobStorage } from '../local/blob-indexeddb';
@@ -27,13 +27,11 @@ import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from './consts';
import { AffineStaticSyncStorage } from './sync';
async function getCloudWorkspaceList() {
const session = await getSession();
if (!session) {
return [];
}
try {
const { workspaces } = await fetcher({
query: getWorkspacesQuery,
}).catch(() => {
return { workspaces: [] };
});
const ids = workspaces.map(({ id }) => id);
return ids.map(id => ({
@@ -41,10 +39,13 @@ async function getCloudWorkspaceList() {
flavour: WorkspaceFlavour.AFFINE_CLOUD,
}));
} catch (err) {
if (err instanceof Array && err[0]?.message === 'Forbidden resource') {
console.log(err);
const e = findGraphQLError(err, e => e.extensions.code === 401);
if (e) {
// user not logged in
return [];
}
throw err;
}
}