Implemented component version of authentication guard.
This commit is contained in:
30
sports-matcher/client/src/components/AuthenticationGuard.js
Normal file
30
sports-matcher/client/src/components/AuthenticationGuard.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import React from "react";
|
||||
import { Navigate } from "react-router-dom";
|
||||
import { globalContext } from "../context";
|
||||
import { apiClient } from "../utils/httpClients";
|
||||
|
||||
export default class AuthenticationGuard extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
async componentDidMount() {
|
||||
if (!this.context.user) {
|
||||
let userDataResponse = await apiClient.get("/user/");
|
||||
if (userDataResponse.status === 200) {
|
||||
this.context.user = userDataResponse.data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static contextType = globalContext;
|
||||
|
||||
render() {
|
||||
if (!this.context.user) {
|
||||
return (
|
||||
<Navigate to="/signup" replace="true" />
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user