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

I'm running into an error with my Login:const Login = ({ history }) => {const handleLogin = us

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]
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346998a4623597.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信