带有React的Ionicapp-处理验证状态和开关路线(历史未定义)

发布于 2025-01-27 18:42:03 字数 2192 浏览 2 评论 0原文

我正在开发离子应用程序(带有React)。我设置authcontext

import React, {createContext, useEffect, useState} from "react";
import {storageService} from 'src/services/StorageService';

const AuthContext = createContext({
    isLoggedIn: false,
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    setAuthState: (value: boolean) => {},
});

const AuthContextProvider = (props: any) => {
    const setAuthState = async (isLoggedIn: boolean) => {
        setState({...state, isLoggedIn: isLoggedIn})

        isLoggedIn ? await storageService.setAuthenticated() : await storageService.clearAuth();
    }

    const initState = {
        isLoggedIn: false,
        setAuthState: setAuthState
    }

    const [state, setState] = useState(initState);

    const checkAuth = async () => {
        const isAuthenticated = await storageService.isAuthenticated();
        setState({...state, isLoggedIn: isAuthenticated})
    }

    useEffect(() => {
        checkAuth();
    }, []);

    return (
        <AuthContext.Provider value={state}>
            {props.children}
        </AuthContext.Provider>
    )
}

export { AuthContext, AuthContextProvider };

其中storageservice是:

import { Storage } from '@capacitor/storage';

class StorageService {
    async isAuthenticated(): Promise<boolean> {
        const { value } = await Storage.get({ key: 'isAuthenticated' });
        return !!value && value === 'true';
    }

   async setAuthenticated() {
       await Storage.set({
           key: 'isAuthenticated',
           value: 'true',
       });
    }

    async clearAuth() {
        await Storage.remove({ key: 'isAuthenticated' });
    }
}

export const storageService = new StorageService();

如何动态设置路由?要在/登录路由上强制应用程序,直到登录,否则导航到/(即主页)。我尝试在主应用程序中使用使用效果,在该文件中我从AuthContext收听了isloggedin,并尝试使用history(use History Hook)> ,但它是未定义的。那么,在离子/反应中更改路线和处理AUTH状态的最佳方法是什么?

I'm developing Ionic app (with React). I set up authContext:

import React, {createContext, useEffect, useState} from "react";
import {storageService} from 'src/services/StorageService';

const AuthContext = createContext({
    isLoggedIn: false,
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    setAuthState: (value: boolean) => {},
});

const AuthContextProvider = (props: any) => {
    const setAuthState = async (isLoggedIn: boolean) => {
        setState({...state, isLoggedIn: isLoggedIn})

        isLoggedIn ? await storageService.setAuthenticated() : await storageService.clearAuth();
    }

    const initState = {
        isLoggedIn: false,
        setAuthState: setAuthState
    }

    const [state, setState] = useState(initState);

    const checkAuth = async () => {
        const isAuthenticated = await storageService.isAuthenticated();
        setState({...state, isLoggedIn: isAuthenticated})
    }

    useEffect(() => {
        checkAuth();
    }, []);

    return (
        <AuthContext.Provider value={state}>
            {props.children}
        </AuthContext.Provider>
    )
}

export { AuthContext, AuthContextProvider };

Where StorageService is:

import { Storage } from '@capacitor/storage';

class StorageService {
    async isAuthenticated(): Promise<boolean> {
        const { value } = await Storage.get({ key: 'isAuthenticated' });
        return !!value && value === 'true';
    }

   async setAuthenticated() {
       await Storage.set({
           key: 'isAuthenticated',
           value: 'true',
       });
    }

    async clearAuth() {
        await Storage.remove({ key: 'isAuthenticated' });
    }
}

export const storageService = new StorageService();

How to dynamically set up routing? To force app at /login route until it is logged in, otherwise navigate to / (i.e. home). I tried with useEffect in main app file, where I listened to isLoggedIn from authContext, and tried to use history (useHistory hook) but it was undefined. So, what is the best way to change routes and handle auth state in Ionic/React?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文