change the page from Auth to Home after login

Karim Sawma

I have this web app that, after a successful login, the page should redirect from Auth.js to Home.js, but what it is doing is after a successful login it is just a changing the URL without redirecting to the page I want:

// app.js 
import React, {Component} from 'react';
import './App.css';
import Authen from './Pages/Authen';
import Home from './Pages/Home';
import {
    BrowserRouter as Router,
    Route,
    Link
} from 'react-router-dom';

class App extends Component {

    render() {
        return (
            <div className="App">

                <Router>
                    <div>
                        <ul>
                        </ul>
                        <Route exact path="/" component={Authen}/>
                        <Route path="Home" component={Home}/>
                    </div>
                </Router>

            </div>

        );
    }
}

export default App;
// Auth.js
import React, {Component} from 'react';
import {withRouter} from 'react-router-dom'

var firebase = require('firebase');

class Authen extends React.Component {
    Login = () => {
        //login method
        const email = this.refs.email.value;
        const password = this.refs.password.value;
        console.log(email, password);

        const auth = firebase.auth();

        const promise = auth.signInWithEmailAndPassword(email, password);

        promise.catch(e => {
            var err = e.message;
            console.log(err);
            this.setState({err: err});
        });
        //gets user uid
        firebase.auth().onAuthStateChanged((user) => {
            if (user) {
                console.log(user.uid);
                this.props.history.push('/Home')
            }
        });
    }

    constructor(props) {
        super(props);

        this.state = {
            err: ''
        };
        this.Login = this.Login.bind(this);
    }

    render() {
        return (
            <div className="login_div">
                <div className="main-div">
                    <h3>N.N.NASSAR</h3>
                    <input ref="email" type="email" placeholder="Email..." id="email_field"/>
                    <input ref="password" type="password" placeholder="Password..." id="password_field"/>
                    <p>{this.state.err}</p>
                    <button onClick={() => this.Login()}> Login</button>
                </div>
            </div>
        );
    }
}

export default withRouter(Authen);
//Home.js
import React, {Component} from 'react';

class Home extends Component {
    render() {
        return (
            <div>
                <h1>saaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad</h1>
            </div>
        );
    }
}

export default Home;
acdcjunior

Your auth is code is redirecting to /Home:

    firebase.auth().onAuthStateChanged((user) => {
        if (user) {
            console.log(user.uid);
            this.props.history.push('/Home')
        }
    });

In your app.js, fix your Route path to Home. From:

 <Route path="Home" component={Home}/>

To:

 <Route path="/Home" component={Home}/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

onBackPressed() to Home Screen not to Login Page

After success login I can't to redirect to home page

Tab bar not showing on home page after login

Change page after login in Xamarin Forms

Reactjs routing to home page after login success

Change page after successful login

How to separate home page for user and admin after they login/signin?

FOSUserBundle redirect from login page after logged in

change home page after first visit

Spring Security - Able to return to login page after initial redirect to home page

How to redirect already authenticated user from login page to home page

redirect to default (home) page after login in jsf2.2 and primefaces

Codeigniter redirect after login to a Rule page before home page

Redirect user to another page after login auth and if statement with PHP PDO

Attempting to redirect users to home page after wordpress login

fetching id from login session and populating in home page : php and mysql

Can't redirect from login page to home page

Unable to login after password change, having home directory encrypted

Xamarin.forms Home Page after login via facebook

Laravel 5.4 return to login page after auth session expire

Routerlink on Home page after Login

How to disable Standard Login Tab from GitLab home Page

Laravel routes/web.php how does Auth::routes() calls register/login/home page?

how to redirect to home page after login: ReactJS?

Laravel Auth::login "expires" after page reloads

Flask Login Returning a 302 Status Code After Login (Not Redirecting to Home Page)

How to redirect already authenticated users from login page to home in django?

Forced page transition after login with nuxt/auth

Failed to redirect from login_page to home page using devise