I try to do RequierAuth lock to protect against an unauthorized user. First created the context where auth should accept the token:
AuthProvider.js:
const AuthContext = createContext({});
export const AuthProvider = ({ children }) => {
const [auth, setAuth] = useState({});
return (
<AuthContext.Provider value={{ auth, setAuth }}>
{children}
</AuthContext.Provider>
)
}
export default AuthContext;
Next created a hook for the above context:
useAuth.js
const useAuth = () => {
return useContext(AuthContext);
}
export default useAuth;
Next, the actual "lock" for protection, where I check if there is a token and return either the page or send the user to the login page:
RequierAuth.js
const RequierAuth = () => {
const {auth} = useAuth();
const location = useLocation();
return (
auth.token?<Outlet/> : <Navigate to = "/auth" state = {{from:location}} replace />
);
}
export default RequierAuth;
App.js
function App() {
return (
<>
...
<div className="App">
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route path="auth" element={<Login />} />
<Route path="reg" element={<Register />} />
<Route element = {<RequierAuth/>}>
<Route path="home" element={<Home />} />
</Route>
</Route>
</Routes>
</BrowserRouter>
</div>
</>
);
}
export default App;
index.js
root.render(
<React.StrictMode>
<AuthProvider>
<App />
</AuthProvider>
</React.StrictMode>
);
And actually the question is that now when I call setAuth on the login page:
LoginForm.js
const Login = () =>{
const {auth,setAuth} = useAuth();
const [authResponce,setAuthResponce] = useState(null);
const [login,setLogin] = useState("");
const onChangeLogin = (e) => {
e.preventDefault();
const username = e.target.value;
setLogin(username);
};
const [password,setPassword] = useState("");
const onChangePassword = (e) => {
const password = e.target.value;
setPassword(password);
};
const instance = axios.create({
baseURL: "http://localhost:8080",
headers: {
"Content-Type": "application/json",
},
});
const postUser = async (user) =>{
return instance.post("http://localhost:8080/auth", user);
}
const onLogin = (e) =>{
e.preventDefault();
const user = {
login: login,
password: password,
};
(async() => {
const response = await postUser(user);
const data = response.data;
console.log(data);
const token = data.token;
console.log(token);
setAuth({token});
console.log(auth);
console.log(auth.token);
})();
};
return (
<div className="Auth-form-container">
<form className="Auth-form" onSubmit={onLogin}>
<div className="Auth-form-content">
<h3 className="Auth-form-title">Sign In</h3>
<div className="form-group mt-3">
<label>Login</label>
<input
type="login"
className="form-control mt-1"
placeholder="Enter login"
value={login}
onChange={onChangeLogin}
/>
</div>
<div className="form-group mt-3">
<label>Password</label>
<input
type="password"
className="form-control mt-1"
placeholder="Enter password"
value={password}
onChange={onChangePassword}
/>
</div>
<div className="d-grid gap-2 mt-3">
<button type="submit" className="btn btn-primary">
Submit
</button>
</div>
</div>
</form>
</div>
)
};
export default Login;
First, if you get a token for the first time, then why is the value not printed in the console, if you get it again on the form, it already prints the expected value, but when you switch to home, it sends it back to the login.
I set breakpoint and at the moment of checking auth.token? indicates that the value is not set, although setAuth has set the value.
The check itself seems to work, if you put the default value in auth and try to compare with it, then we will normally get to /home.
I've only recently started studying and I can't figure out what the error is, so I'll be glad for help to figure it out.