Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

add: logout api function

kruzhambus4ee9ebb89870
diff --git a/src/App.js b/src/App.js
index 8f207f57d..5f4b92342 100644
--- a/src/App.js
+++ b/src/App.js
@@ -26,6 +26,7 @@ import { errorsActions } from 'redux/actions/errors'
import { alertsActions } from 'redux/actions/alerts'
import UIActions from 'redux/actions/ui'
import routes from 'routes'
+import { getRefreshToken } from 'utils/refreshToken'
import { authMe } from './api'
const MainPage = lazy(() => import('pages/MainPage'))
@@ -90,6 +91,7 @@ const App = () => {
const { message, type } = useSelector(state => state.alerts)
const themeType = useSelector(state => state.ui.theme.type)
const accessToken = getAccessToken()
+ const refreshToken = getRefreshToken()
useEffect(() => {
const eventCallback = (data) => {
@@ -124,7 +126,7 @@ const App = () => {
useEffect(() => {
(async () => {
- if (accessToken && !authenticated) {
+ if ((accessToken && refreshToken) && !authenticated) {
try {
const me = await authMe()
dispatch(UIActions.setThemeType(me.theme))
diff --git a/src/api/index.js b/src/api/index.js
index e62576e19..1ed72663a 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -35,8 +35,6 @@ const refreshAuthLogic = (failedRequest) =>
return Promise.resolve()
})
.catch((error) => {
- removeAccessToken()
- removeRefreshToken()
store.dispatch(authActions.logout())
return Promise.reject(error)
})
@@ -71,6 +69,25 @@ export const authMe = () =>
: error.response.data.message
})
+export const logoutApi = (refreshToken) =>
+ axios
+ .post(`${baseURL}v1/auth/logout`, null, {
+ headers: {
+ Authorization: `Bearer ${refreshToken}`,
+ },
+ })
+ .then((response) => {
+ removeAccessToken()
+ removeRefreshToken()
+ return response.data
+ })
+ .catch((error) => {
+ debug('%s', error)
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message
+ })
+
export const refreshToken = () =>
api
.post('v1/auth/refresh-token')
diff --git a/src/redux/actions/auth/index.js b/src/redux/actions/auth/index.js
index 6c9f40b2e..5fd522878 100644
--- a/src/redux/actions/auth/index.js
+++ b/src/redux/actions/auth/index.js
@@ -1,6 +1,7 @@
import { removeItem } from 'utils/localstorage'
import { removeAccessToken } from 'utils/accessToken'
-import { removeRefreshToken } from 'utils/refreshToken'
+import { getRefreshToken, removeRefreshToken } from 'utils/refreshToken'
+import { logoutApi } from 'api'
import { LS_VIEW_PREFS_SETTING } from 'redux/constants'
import { types } from './types'
@@ -33,6 +34,8 @@ export const authActions = {
},
logout(basedOn401Error) {
+ const refreshToken = getRefreshToken()
+ logoutApi(refreshToken)
removeAccessToken()
removeRefreshToken()
removeItem(LS_VIEW_PREFS_SETTING)
@@ -71,7 +74,10 @@ export const authActions = {
},
deleteAccountSuccess() {
+ const refreshToken = getRefreshToken()
+ logoutApi(refreshToken)
removeAccessToken()
+ removeRefreshToken()
removeItem('user_info')
return {
diff --git a/src/redux/sagas/auth/workers/logout.js b/src/redux/sagas/auth/workers/logout.js
index 60cd16f5d..23b5adbad 100644
--- a/src/redux/sagas/auth/workers/logout.js
+++ b/src/redux/sagas/auth/workers/logout.js
@@ -3,7 +3,6 @@ import Debug from 'debug'
import UIActions from 'redux/actions/ui'
import { getGeneralStats } from 'api'
-import { THEME_TYPE } from 'redux/constants'
const debug = Debug('swetrix:rx:s:logout')
@@ -13,8 +12,6 @@ export default function* logoutWorker({ payload: { basedOn401Error } = {} }) {
const stats = yield call(getGeneralStats)
yield put(UIActions.setGeneralStats(stats))
}
-
- yield put(UIActions.setThemeType(THEME_TYPE.classic))
} catch (e) {
debug('Error while getting general stats data: %s', e)
}
diff --git a/src/redux/sagas/ui/initialise.js b/src/redux/sagas/ui/initialise.js
index 882e95785..6d1b53c56 100644
--- a/src/redux/sagas/ui/initialise.js
+++ b/src/redux/sagas/ui/initialise.js
@@ -2,6 +2,7 @@ import { put, call } from 'redux-saga/effects'
import Debug from 'debug'
import { getAccessToken } from 'utils/accessToken'
+import { getRefreshToken } from 'utils/refreshToken'
import { getLastPost } from 'api/blog'
import UIActions from 'redux/actions/ui'
@@ -10,8 +11,9 @@ const debug = Debug('swetrix:rx:s:initialise')
export default function* initialise() {
try {
const token = yield call(getAccessToken)
+ const refreshToken = yield call(getRefreshToken)
- if (token) {
+ if (token && refreshToken) {
yield put(UIActions.loadProjects())
yield put(UIActions.loadSharedProjects())
yield put(UIActions.loadExtensions())