最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React with Firebase Auth: How to set Persistence? - Stack Overflow

programmeradmin2浏览0评论

I'm running into an error with my Login:

const Login = ({ history }) => {
const handleLogin = useCallback(
    async event => {
        event.preventDefault();
        const { email, password } = event.target.elements;
        try {
            await app
                .auth()
                .signInWithEmailAndPassword(email.value, password.value);
            app.auth().setPersistence(app.auth.Auth.Persistence.SESSION);
            history.push("/feed");
        } catch (error) {
            alert(error);
        }
    },
    [history]
);

I think that my setPersistence is at the wrong place but I don't know how to fix that. My import list:

import React, { useCallback, useContext } from "react";
import { withRouter, Redirect } from "react-router";
import app from "../../firebase";
import { AuthContext } from "../../Auth";

Thank you!

I'm running into an error with my Login:

const Login = ({ history }) => {
const handleLogin = useCallback(
    async event => {
        event.preventDefault();
        const { email, password } = event.target.elements;
        try {
            await app
                .auth()
                .signInWithEmailAndPassword(email.value, password.value);
            app.auth().setPersistence(app.auth.Auth.Persistence.SESSION);
            history.push("/feed");
        } catch (error) {
            alert(error);
        }
    },
    [history]
);

I think that my setPersistence is at the wrong place but I don't know how to fix that. My import list:

import React, { useCallback, useContext } from "react";
import { withRouter, Redirect } from "react-router";
import app from "../../firebase";
import { AuthContext } from "../../Auth";

Thank you!

Share Improve this question asked Dec 2, 2019 at 23:00 bnexdbnexd 1614 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You have to call setPersistence before calling signInWithEmailAndPassword.

const Login = ({ history }) => {
const handleLogin = useCallback(
    async event => {
        event.preventDefault();
        const { email, password } = event.target.elements;
        try {
            await app.auth().setPersistence(app.auth.Auth.Persistence.SESSION);
            await app
                .auth()
                .signInWithEmailAndPassword(email.value, password.value);
            history.push("/feed");
        } catch (error) {
            alert(error);
        }
    },
    [history]
);
发布评论

评论列表(0)

  1. 暂无评论