Skip to content

Commit

Permalink
Auto logout with alert
Browse files Browse the repository at this point in the history
  • Loading branch information
Starute committed Sep 3, 2021
1 parent d99f37a commit 343dfe7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
14 changes: 10 additions & 4 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Verify from "./Components/Verify";
import ContactList from "./Components/contactList";
import SearchUser from "./Components/searchUser";


class App extends Component {
constructor(props) {
super(props);
Expand All @@ -25,7 +24,14 @@ class App extends Component {
}

async componentDidMount() {
const basic = AuthService.getBasicInfo();
let basic = AuthService.getBasicInfo();
if (basic) {
await AuthService.validToken(basic.token);
basic = AuthService.getBasicInfo();
if (!basic) {
this.setState({ basic: null, currentUser: null });
}
}

if (basic) {
await AuthService.getUserDataFromBackend(basic.token, basic.id);
Expand All @@ -43,7 +49,7 @@ class App extends Component {
return (
<div className="App">
<Router>
<NavigationBar user = {currentUser} onLogOut = {this.handleLogOut} />
<NavigationBar user={currentUser} onLogOut={this.handleLogOut} />
<Switch>
<Route exact path="/signup" component={SignUp} />
<Route path="/login" component={LogIn} />
Expand All @@ -56,7 +62,7 @@ class App extends Component {
<Route path="/signup/:email/:code">
<Verify />
</Route>
<Route path = "/">
<Route path="/">
<HomePage />
</Route>
</Switch>
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/Services/AuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ class AuthService {
}
}

async validToken(token) {
const response = await axios.post(API_URL + "/jwt/checkExpired", {
authToken: token,
});

if (!response.data) {
this.logout();
alert("Login expired, please login again.");
}
}

async login(username, password) {
const response = await axios.post(API_URL + "/login", {
username,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/constant.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// url for local
// export const API_URL = "http://localhost:8080";
export const API_URL = "http://localhost:8080";

// url for heroku
export const API_URL = "https://crm-c-and-h-backend.herokuapp.com"
//export const API_URL = "https://crm-c-and-h-backend.herokuapp.com"

0 comments on commit 343dfe7

Please sign in to comment.