Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(in-app): lazy local anonymous login #952

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions in-app/v1/src/App/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Suspense, useEffect, useMemo } from 'react';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { decodeQueryParams, JsonParam, StringParam } from 'serialize-query-params';
import { decodeQueryParams, JsonParam } from 'serialize-query-params';
import { parse } from 'query-string';
import { Helmet, HelmetProvider } from 'react-helmet-async';

Expand Down Expand Up @@ -34,6 +34,11 @@ import Test from './Test';

function RequireAuth({ children }: { children: JSX.Element }) {
const { user, loading, error } = useAuth();

useAutoLogin({
strategies: [loginByLocalAnonymousId],
});

if (loading) {
return <Loading fullScreen />;
}
Expand Down Expand Up @@ -77,13 +82,11 @@ export default function App() {
}, []);

useAutoLogin({
hash: params,
strategies: [
loginByAnonymousId,
loginByXDAccessToken,
loginByTDSCredential,
loginByJWT,
loginByLocalAnonymousId,
loginByCurrentUser,
],
});
Expand Down Expand Up @@ -151,11 +154,6 @@ function useHashConfiguration() {
{
meta: JsonParam,
fields: JsonParam,
'anonymous-id': StringParam,
'xd-access-token': StringParam,
'tds-credential': StringParam,
credential: StringParam,
'associate-anonymous-id': StringParam,
},
parse(window.location.hash)
),
Expand Down
13 changes: 8 additions & 5 deletions in-app/v1/src/auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User } from 'open-leancloud-storage/auth';
import { customAlphabet } from 'nanoid';

import { auth, http } from '@/leancloud';
import { useSetAuth } from '@/states/auth';
import { useAuthState } from '@/states/auth';

async function login(data: any) {
let sessionToken: string;
Expand Down Expand Up @@ -81,17 +81,20 @@ export const loginByLocalAnonymousId: Strategy = async ({ query }) => {
return login({ type: 'anonymous', anonymousId });
};

const { hash: hashSnapshot, search: searchSnapshot } = window.location;

interface UseAuthLoginOptions {
strategies: Strategy[];
hash: Record<string, any>;
}

export function useAutoLogin({ strategies, hash }: UseAuthLoginOptions) {
const setAuth = useSetAuth();
export function useAutoLogin({ strategies }: UseAuthLoginOptions) {
const [auth, setAuth] = useAuthState();
useEffect(() => {
if (auth.user) return;
const autoLogin = async () => {
setAuth({ loading: true });
const query = parse(window.location.search);
const query = parse(searchSnapshot);
const hash = parse(hashSnapshot);
for (const strategy of strategies) {
try {
const user = await strategy({ hash, query });
Expand Down
7 changes: 6 additions & 1 deletion in-app/v1/src/states/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { User } from 'open-leancloud-storage/auth';
import { atom, useRecoilValue, useSetRecoilState } from 'recoil';
import { atom, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';

interface AuthState {
user?: User;
Expand All @@ -9,8 +9,13 @@ interface AuthState {

const authState = atom<AuthState>({
key: 'auth',
default: {},
});

export function useAuthState() {
return useRecoilState(authState);
}

export function useSetAuth() {
return useSetRecoilState(authState);
}
Expand Down