mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
feat: auth metric and trace (#4063)
This commit is contained in:
@@ -1,11 +1,53 @@
|
||||
import { isDesktop } from '@affine/env/constant';
|
||||
import {
|
||||
generateRandUTF16Chars,
|
||||
SPAN_ID_BYTES,
|
||||
TRACE_ID_BYTES,
|
||||
traceReporter,
|
||||
} from '@affine/graphql';
|
||||
import { refreshRootMetadataAtom } from '@affine/workspace/atom';
|
||||
import { getCurrentStore } from '@toeverything/infra/atom';
|
||||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
||||
import { signIn, signOut } from 'next-auth/react';
|
||||
import { startTransition } from 'react';
|
||||
|
||||
type TraceParams = {
|
||||
startTime: string;
|
||||
spanId: string;
|
||||
traceId: string;
|
||||
event: string;
|
||||
};
|
||||
|
||||
function genTraceParams(): TraceParams {
|
||||
const startTime = new Date().toISOString();
|
||||
const spanId = generateRandUTF16Chars(SPAN_ID_BYTES);
|
||||
const traceId = generateRandUTF16Chars(TRACE_ID_BYTES);
|
||||
const event = 'signInCloud';
|
||||
return { startTime, spanId, traceId, event };
|
||||
}
|
||||
|
||||
function onResolveHandleTrace<T>(
|
||||
res: Promise<T> | T,
|
||||
params: TraceParams
|
||||
): Promise<T> | T {
|
||||
const { startTime, spanId, traceId, event } = params;
|
||||
traceReporter &&
|
||||
traceReporter.cacheTrace(traceId, spanId, startTime, { event });
|
||||
return res;
|
||||
}
|
||||
|
||||
function onRejectHandleTrace<T>(
|
||||
res: Promise<T> | T,
|
||||
params: TraceParams
|
||||
): Promise<T> {
|
||||
const { startTime, spanId, traceId, event } = params;
|
||||
traceReporter &&
|
||||
traceReporter.uploadTrace(traceId, spanId, startTime, { event });
|
||||
return Promise.reject(res);
|
||||
}
|
||||
|
||||
export const signInCloud: typeof signIn = async (provider, ...rest) => {
|
||||
const traceParams = genTraceParams();
|
||||
if (isDesktop) {
|
||||
if (provider === 'google') {
|
||||
open(
|
||||
@@ -29,25 +71,32 @@ export const signInCloud: typeof signIn = async (provider, ...rest) => {
|
||||
callbackUrl: buildCallbackUrl(callbackUrl),
|
||||
},
|
||||
...tail
|
||||
);
|
||||
)
|
||||
.then(res => onResolveHandleTrace(res, traceParams))
|
||||
.catch(err => onRejectHandleTrace(err, traceParams));
|
||||
}
|
||||
} else {
|
||||
return signIn(provider, ...rest);
|
||||
return signIn(provider, ...rest)
|
||||
.then(res => onResolveHandleTrace(res, traceParams))
|
||||
.catch(err => onRejectHandleTrace(err, traceParams));
|
||||
}
|
||||
};
|
||||
|
||||
export const signOutCloud: typeof signOut = async options => {
|
||||
const traceParams = genTraceParams();
|
||||
return signOut({
|
||||
...options,
|
||||
callbackUrl: '/',
|
||||
}).then(result => {
|
||||
if (result) {
|
||||
startTransition(() => {
|
||||
getCurrentStore().set(refreshRootMetadataAtom);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
});
|
||||
})
|
||||
.then(result => {
|
||||
if (result) {
|
||||
startTransition(() => {
|
||||
getCurrentStore().set(refreshRootMetadataAtom);
|
||||
});
|
||||
}
|
||||
return onResolveHandleTrace(result, traceParams);
|
||||
})
|
||||
.catch(err => onRejectHandleTrace(err, traceParams));
|
||||
};
|
||||
|
||||
export function buildCallbackUrl(callbackUrl: string) {
|
||||
|
||||
Reference in New Issue
Block a user