useContext auth hook returning null - node.js

I have created a useAuth hook in order to allow the user access to the private route once authorised the authorising cookie is stored properly but the useAuth hook seems to be returning null. I also tried printing the value of setUser but it doesn't seem to print after the line setUser(res.data.currentUser) but the one before is printed I cannot figure out why.
The cookies are stored and set properly it just seems to be the auth hook that return setUser as null even though the line console.log("RES>DATA = ", res.data.currentUser); logs the correct details of the cookie but when I try to manually change the url to the private route after the cookie is stored it returns a null for the setUser returned from the auth hook and so access to private route isn't granted. This is done using react for frontend with node js and express for the backend. If any more context is necessary please let me know.
useAuth hook:
import { useState, useContext, useEffect } from "react";
import { UserContext } from "./UserContext";
import axios from "axios";
export default function useAuth() {
const { setUser } = useContext(UserContext);
const [error, setError] = useState(null);
useEffect(() => {
console.log("USE effect");
setUserContext();
});
//set user in context and push them home
const setUserContext = async () => {
console.log("In here");
return await axios
.get("auth/checkAuth")
.then((res) => {
console.log("RES>DATA = ", res.data.currentUser); // PRINTS THE CORRECT COOKIE VALUES
setUser(res.data.currentUser); // THIS SEEMS TO BE NULL?????
console.log("SET USER + ", setUser); // THIS DOES NOT PRINT
})
.catch((err) => {
setError(err);
});
};
return {
setUser,
};
}
Private route:
import React, { useContext } from 'react';
import {Route, Redirect, Navigate} from 'react-router-dom';
import { UserContext } from '../hooks/UserContext';
import useAuth from '../hooks/useAuth';
export default function PrivateRoute({children}) {
const auth = useAuth();
console.log("aUTH in priv = ", auth);
return auth ? children : <Navigate to="/"/>
}
App.js:
import "./App.css";
import { Redirect, Route, Routes, Switch } from "react-router-dom";
import useFetch from "./useFetch";
import { useEffect, useState, useRef } from "react";
import axios from "axios";
import Activities from "./components/Activities";
import HomePage from "./components/Home";
import Map from "./components/Map";
import checkUser from "./hooks/checkUser";
import { UserContext } from "./hooks/UserContext";
import useFindUser from "./hooks/checkUser";
import PrivateRoute from "./components/PrivateRoute";
function App() {
const [auth, setAuth] = useState(false);
const [activities, setActivities] = useState([]);
const notInitialRender = useRef(false);
const { user, setUser, isLoading } = useFindUser(); // works as expected
return (
<div className="App">
<UserContext.Provider value={{ user, setUser, isLoading }}>
<Routes>
<Route path="/" element={<HomePage />}></Route>
<Route path="/Activities" element={<Activities />} />
<Route
path="/Private"
element={
<PrivateRoute>
<Map />
</PrivateRoute>
}
/>
</Routes>
</UserContext.Provider>
</div>
);
}
export default App;
auth/checkAuth express route:
export const checkAuth = (req, res) => {
let currentUser;
console.log("res.cookies = ", req);
if (req.cookies.currentUser) {
// res.send(200);
currentUser = req.cookies.currentUser; // set to user object in cookies
console.log("current user = ", currentUser);
// return 200 status code
} else {
currentUser = null;
}
//res.json(currentUser);
res.status(200).send({ currentUser });
};
Map component which is private route component:
import react from 'react';
function Map() {
<div>
<p style={{color:'red'}}>You have access to the private route</p>
</div>
}
export default Map;
UserContext file:
import { createContext } from "react";
export const UserContext = createContext("null");
I tried logging values and using useEffect to call the function when the useAuth hook is called but couldn't figure it out

Related

How do I make redirections with Next.js after user is authenticated?

I'm working on a web application with next.js and I have a separated API where I generate the JWT token.
I have a login form that I use to send the user info and recover the JWT. Everything is working fine, but when I persist the data and I try to redirect the user to the dashboard which is supposed to be a protected route, it stays protected even though the user is authenticated. I don't know what am I doing wrong..
Here is the context file :
import React, {createContext, useEffect, useState} from 'react'
import { useRouter } from "next/router";
import axios from 'axios'
export const Context = createContext(null)
const devURL = "http://localhost:4444/api/v1/"
export const ContextProvider = ({children}) => {
const router = useRouter()
const [user, setUser] = useState()
const [userToken, setUserToken] = useState()
const [loading, setLoading] = useState(false)
const [successMessage, setSuccessMessage] = useState("")
const [errorMessage, setErrorMessage] = useState("")
const Login = (em,pass) => {
setLoading(true)
axios.post(devURL+"authentication/login", {
email : em,
password : pass
})
.then((res)=>{
setSuccessMessage(res.data.message)
setErrorMessage(null)
setUser(res.data.user)
setUserToken(res.data.token)
localStorage.setItem('userToken', res.data.token)
localStorage.setItem('user', res.data.user)
setLoading(false)
})
.catch((err)=>{
setErrorMessage(err.response.data.message)
setSuccessMessage(null)
setLoading(false)
})
}
const Logout = () => {
setUserToken()
setUser()
localStorage.removeItem('userToken')
localStorage.removeItem('user')
router.push('/authentication/admin')
}
const isUserAuthenticated = () => {
return !!userToken
}
useEffect(()=>{
let token = localStorage.getItem('userToken')
if(token){
setUserToken(token)
}
},[userToken])
return (
<Context.Provider value={{
Login,
user,
loading,
userToken,
setUserToken,
Logout,
successMessage,
setSuccessMessage,
setErrorMessage,
isUserAuthenticated,
errorMessage}}>
{children}
</Context.Provider>
)
}
and here is the dashboard page :
import React, {useContext, useEffect} from 'react'
import DashboardIndexView from '../../views/dashboard'
import { useRouter } from "next/router";
import { Context } from '../../context/context';
export default function DashboardIndex() {
const router = useRouter();
const {isUserAuthenticated} = useContext(Context);
console.log(isUserAuthenticated())
useEffect(()=>{
if(isUserAuthenticated()) {
router.push("/dashboard")
} else {
router.push("/authentication/admin")
}
}, [])
return (
<>
<DashboardIndexView />
</>
)
}
I believe that the useEffect in the dashboard page is always going to be redirecting the user to the authentication form, because I debugged what happens (picture) and apparently the isUserAuthenticated function runs twice, once it returns false, and then true, and the first return 'false' redirects the user to the authentication form.
How can I fix this ?
Picture of the console :

React useState can't use from another component

I Created a node.js server and i can write user info to MongoDb and i can create JWT in postman. so i want to use this on react project.
i created react router with private route which it's checking if there is an any user info in the local storage. example , (i did not create a axios post for login api. i just want to write user info with hardcode for see the code is working)
import { useAuth } from "../Context/AuthContext";
import { Navigate,useLocation } from "react-router-dom";
export default function PrivateRoutes({children}){
const user = JSON.parse(localStorage.getItem('user'))
const location = useLocation();
if(!user){
return <Navigate to="/login" state={{return_url:location.pathname}} />
}
return children;
}
authcontext
So , when i'm in a login page , i created a button and if i click this button i want to access to my AuthProvider and set user info to the LocalStorage in AuthProvider.
Login page,
LoginPage.js
import { useAuth } from "../../../Context/AuthContext";
import { useNavigate,useLocation } from "react-router-dom";
export default function LoginPage(){
const navigate = useNavigate();
const location = useLocation();
const { setUser } = useAuth();
const loginHandle = () => {
setUser({
id : 1,
username : 'umitcamurcuk'
})
navigate(location?.state?.return_url || '/');
}
const logoutHandle = () => {
setUser(false);
navigate('/');
}
return (
<div>
<button onClick={loginHandle}>Sign In</button>
<button onClick={logoutHandle}>Cikis yap</button>
</div>
)
}
and my AuthContext page,
AuthContext.js
import { createContext, useState , useContext, useEffect } from "react";
const Context = createContext()
export const AuthProvider = ({ children }) => {
const [user , setUser] = useState(JSON.parse(localStorage.getItem('user')) || false);
const data = [ user, setUser ]
useEffect(() => {
localStorage.setItem('user', JSON.stringify(user))
},[user])
return(
<Context.Provider value={data}>
{children}
</Context.Provider>
)
}
export const useAuth = () => useContext(Context);
But when i click login button , this error show up
error
LoginPage.js:12 Uncaught TypeError: setUser is not a function
and my indexJS
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {BrowserRouter} from 'react-router-dom';
import { AuthProvider } from './Context/AuthContext.js';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<BrowserRouter>
<AuthProvider>
<App />
</AuthProvider>
</BrowserRouter>
);
is anyone for help me ?
Thanks
You defined your value as array and not an object
const [user, setUser] = useAuth();
or
const data = { user, setUser }

Converting function React component to class component

I'm trying to add a login page to my existing app. Following along this tutorial https://www.digitalocean.com/community/tutorials/how-to-add-login-authentication-to-react-applications
Got stuck in step 3. My app is a class component, while in the tutorial it's a function component.
How can I convert this to a class component?
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import './App.css';
import Dashboard from '../Dashboard/Dashboard';
import Login from '../Login/Login';
import Preferences from '../Preferences/Preferences';
function setToken(userToken) {
sessionStorage.setItem('token', JSON.stringify(userToken));
}
function getToken() {
const tokenString = sessionStorage.getItem('token');
const userToken = JSON.parse(tokenString);
return userToken?.token
}
function App() {
const token = getToken();
if(!token) {
return <Login setToken={setToken} />
}
return (
<div className="wrapper">
...
</div>
);
}
export default App;
I've tried as below, but it says getToken is not defined inside of a render.
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import './App.css';
import Dashboard from '../Dashboard/Dashboard';
import Login from '../Login/Login';
import Preferences from '../Preferences/Preferences';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
token: undefined
}
this.setToken = this.setToken.bind(this);
this.getToken = this.getToken.bind(this);
}
setToken(userToken) {
sessionStorage.setItem('token', JSON.stringify(userToken));
}
getToken() {
const tokenString = sessionStorage.getItem('token');
const userToken = JSON.parse(tokenString);
return userToken?.token;
}
render() {
const {token} = getToken(); /* In this line getToken is not defined */
if (!token) {
return <Login setToken={(newToken) => this.setState({ token: newToken })} />
}
return (
<div className="pomodoro-clock">
</div>
);
}
I see in the original code, getToken and setToken declared outside of an app. I've tried this, but anyway, getToken is not defined .
I can provide the full code if needed. Any help is appreciated.
If you want to make the functions members of the class, then you need to refer to it using this.getToken(), not just getToken();
render() {
const {token} = this.getToken();
Though since the getToken and setToken functions don't make any reference to this they don't actually need to part of the class. You could keep them outside the class, and refer to them the same way the function component refers to them:
function setToken(userToken) {
sessionStorage.setItem('token', JSON.stringify(userToken));
}
function getToken() {
const tokenString = sessionStorage.getItem('token');
const userToken = JSON.parse(tokenString);
return userToken?.token
}
export default class App extends Component {
constructor(props) {
super(props);
}
render() {
const {token} = getToken();
// ...
}
}

Authentication reactjs website with Firebase using email and password

I tried to create an authentication website with Firebase using email and password. I can't even load the Login page.
Here's Auth.js
import React, { useState, useEffect} from "react";
import { auth } from './config'
import { onAuthStateChanged } from "firebase/auth";
export const AuthContext = React.createContext();
export const AuthProvider = ({ children }) => {
const [currentUser, setCurrentUser] = useState(null);
useEffect(() => {
onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
})
}, [])
return (
<AuthContext.Provider value={{currentUser}}>
{children}
</AuthContext.Provider>
)
}
And this is Login.js
import React, {useContext ,useState } from "react";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";
import "./Login.css";
import { auth } from './config'
import { signInWithEmailAndPassword } from "firebase/auth";
import { AuthContext } from "./Auth";
import { useHistory } from "react-router-dom";
const Login = () => {
let history = useHistory();
const handleSubmit = (event) => {
event.preventDefault();
const { email, password } = event.target.elements;
signInWithEmailAndPassword(auth, email.value, password.value)
.then((userCredential) => {
const user = userCredential.user;
console.log(user.uid);
})
.catch((error) => {
console.log(error.massage);
});
}
const currentUser = useContext(AuthContext);
if(currentUser) {
return history.push('/dashboard');
}
return (
<div className="Login">
<h1>Login</h1>
<Form onSubmit={handleSubmit}>
//Login Form
</Form>
</div>
);
}
export default Login
And DashBoard.js
import React, {useContext} from 'react'
import { AuthContext } from './Auth'
import { auth } from './config'
import { signOut } from 'firebase/auth'
import { useHistory } from "react-router-dom";
const DashBoard = () => {
const currentUser = useContext(AuthContext);
let history = useHistory();
if(!currentUser) {
return history.push('/login');
}
const signOutFunc = () => {
signOut(auth)
}
return (
<div>
<div className='container mt-5'>
<h1>Welcome</h1>
<h2>If you see this you are logged in.</h2>
<button className='btn btn-danger' onClick={signOutFunc}>Sign Out</button>
</div>
</div>
)
}
export default DashBoard;
Lastly App.js
import { BrowserRouter as Router, Route, Switch} from 'react-router-dom'
import Login from './Login'
import DashBoard from './DashBoard';
import { AuthProvider } from './Auth'
function App() {
return (
<AuthProvider>
<Router>
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/dashboard" component={Dashboard} />
</Switch>
</Router>
</AuthProvider>
);
}
export default App;
When I open /login, it would send me to /dasgboard immediately. If I typed /login again it gives me this error
Error: Login(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
I can't figure it out what's wrong with it. Please help me.
Thank you
You have multiple places in your code where you return history.push('/dashboard'); or another path. You should return there a null:
if(!currentUser) {
history.push('/login');
return null
}

react router changes navigation but does not render page using redux

I have got a simple react-router-redux application going on where /home has a button which when clicked should navigate to /profile page. Currently my code looks like this.
actions/index.js
import { push } from 'react-router-redux'
import * as actionTypes from '../constants'
const homeClicked = () => {
return { type: actionTypes.HOME_CLICK }
}
const profileClicked = () => {
return { type: actionTypes.PROFILE_CLICK }
}
export const handleHomeClick = () => {
return (dispatch) => {
dispatch(homeClicked())
dispatch(push('/profile'))
}
}
export const handleProfileClick = () => {
return (dispatch) => {
dispatch(profileClicked())
dispatch(push('/'))
}
}
containers/HomeContainer.js
import React from 'react'
import { connect } from 'react-redux'
import * as actions from '../actions'
import { withRouter } from 'react-router-dom'
import PropTypes from 'prop-types'
class Home extends React.Component {
handleClick = () => {
this.props.handleHomeClick();
}
render() {
return (
<div className='Home'>
<button onClick={this.handleClick}>Home</button>
</div>
)
}
}
Home.propTypes = {
handleHomeClick: PropTypes.func.isRequired
}
const mapStateToProps = () => {
return {}
}
export default withRouter(connect(mapStateToProps, actions)(Home))
containers/ProfileContainer.js
import React from 'react'
import { connect } from 'react-redux'
import * as actions from '../actions'
import { withRouter } from 'react-router-dom'
import PropTypes from 'prop-types'
class Profile extends React.Component {
handleClick = () => {
this.props.handleProfileClick();
}
render() {
return (
<div className='Profile'>
<button onClick={this.handleClick}>Profile</button>
</div>
)
}
}
Profile.propTypes = {
handleProfileClick: PropTypes.func.isRequired
}
const mapStateToProps = () => {
return {}
}
export default withRouter(connect(mapStateToProps, actions)(Profile))
reducers/index.js
import { HOME_CLICK, PROFILE_CLICK } from '../constants'
import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
const clickReducer = (state={ message: 'HOME' }, action) => {
switch(action.type) {
case HOME_CLICK:
return { message: 'PROFILE' };
case PROFILE_CLICK:
return { message: 'HOME' };
default:
return state
}
}
export default combineReducers({
clicking: clickReducer,
routing: routerReducer
})
constants.js
export const HOME_CLICK = 'HOME_CLICK'
export const PROFILE_CLICK = 'PROFILE_CLICK'
history.js
import { createBrowserHistory } from 'history'
export default createBrowserHistory()
index.js
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux';
import createRoutes from './routes'
import rootReducer from './reducers'
import thunk from 'redux-thunk'
import browserHistory from './history'
import reduxLogger from 'redux-logger'
import { createStore, applyMiddleware } from 'redux'
import { syncHistoryWithStore, routerMiddleware } from 'react-router-redux';
const middlewares = applyMiddleware(
thunk,
routerMiddleware(browserHistory),
reduxLogger
);
const store = createStore(rootReducer, middlewares)
const history = syncHistoryWithStore(browserHistory, store)
const routes = createRoutes(history)
ReactDOM.render(
<Provider store={store}>
{routes}
</Provider>,
document.getElementById('root')
)
routes.js
import React from 'react'
import { Router, Route, Switch } from 'react-router'
import HomeContainer from './containers/HomeContainer'
import ProfileContainer from './containers/ProfileContainer'
const createRoutes = (history) => {
return (
<Router history={history}>
<Switch>
<Route exact path='/' component={HomeContainer}/>
<Route path='/profile' component={ProfileContainer}/>
</Switch>
</Router>
)
}
export default createRoutes
app.js
import express from 'express'
import config from './config'
import path from 'path'
const app = express()
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')))
app.get('*', (req, resp) => {
resp.render('index');
})
app.listen(config.port, config.host, () => {
console.info('Server listening to', config.serverUrl())
})
This code is changing the url but not rendering the profile page when the home button on the home page is clicked. Also here's a link of the picture of redux logger output.
I am stuck on this for a few hours and other SO answers have not been much of a help. Any help would be appreciated.
When you click home it should render the home route as you have it written now? Isn't that what it is supposed to do.

Resources