Skip to content

Commit

Permalink
fix(studio): Warns user he will be logged out (#185)
Browse files Browse the repository at this point in the history
When the a 401 (unauthorized) is detected, a warning is shown to the user that they are about to be logged out, and then they are logged out. 

Current behaviour, is whatever action triggered the 401 will show an error message independently (unless it's caught and hidden), and nothing else will happen, until the minute timer detects the user has logged out.
  • Loading branch information
ptrckbp committed Nov 1, 2021
1 parent 371e8ae commit 323e2b2
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion packages/studio-ui/src/web/components/Authentication/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Classes, H5, Intent, Position, Toaster } from '@blueprintjs/core'
import axios from 'axios'
import { auth } from 'botpress/shared'
import _ from 'lodash'
Expand All @@ -23,11 +24,47 @@ const ensureAuthenticated = WrappedComponent => {
componentDidMount() {
authEvents.on('logout', this.promptLogin)
this.setupAuth()
this.interceptUnauthorized = axios.interceptors.response.use(
response => {
return response
},
error => {
if (error.response.status !== 401) {
return Promise.reject(error)
}
if (error.response.config.url !== `${window.API_PATH}/admin/ping`) {
this.checkAuth() // just to make sure
}
return Promise.reject(error)
}
)
}

componentWillUnmount() {
authEvents.off('logout', this.promptLogin)
clearInterval(this.checkInterval)
axios.interceptors.response.eject(this.interceptUnauthorized)
}

explainAndLogUserOut = () => {
const toastContent = (
<div>
<div>
<H5 className={Classes.DARK}>You have been logged out</H5>
<p>
For security reasons, we have logged you out. Log back in to continue working.
</p>
</div>
</div>
)
Toaster.create({ position: Position.TOP_RIGHT }).show({
message: toastContent,
intent: Intent.DANGER,
timeout: 3000,
onDismiss:()=>{
this.promptLogin()
}
})
}

promptLogin = () => {
Expand Down Expand Up @@ -76,7 +113,7 @@ const ensureAuthenticated = WrappedComponent => {
checkAuth = () => {
axios.get(`${window.API_PATH}/admin/ping`).catch(err => {
if (err.response.status === 401) {
this.promptLogin()
this.explainAndLogUserOut()
}
})
}
Expand Down

0 comments on commit 323e2b2

Please sign in to comment.