-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update login scenario, online connection check
- Loading branch information
Ivanov N
committed
Dec 15, 2024
1 parent
fab55b4
commit 10da015
Showing
2 changed files
with
51 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
import { i18n } from '@/i18n' | ||
import axios from 'axios' | ||
|
||
const checkUrl = 'http://ipv4.icanhazip.com' | ||
|
||
export default (store) => { | ||
window.addEventListener('online', handleEvent) | ||
window.addEventListener('offline', handleEvent) | ||
|
||
function handleEvent(event) { | ||
store.commit('setIsOnline', event.type === 'online') | ||
store.dispatch('snackbar/show', { | ||
message: i18n.global.t(`connection.${event.type}`), | ||
timeout: 3000 | ||
}) | ||
// window.addEventListener('online', handleEvent) | ||
// window.addEventListener('offline', handleEvent) | ||
|
||
// async function handleEvent(event) { | ||
// if (event.type === 'online') { | ||
// store.commit('setIsOnline', !!res) | ||
// } else { | ||
// store.commit('setIsOnline', false) | ||
// } | ||
|
||
setInterval(async () => { | ||
const res = Boolean(await inetConnectionCheck()) | ||
const storeVal = store.getters['isOnline'] | ||
if (storeVal !== res) { | ||
store.commit('setIsOnline', res) | ||
const snackMsg = res ? 'online' : 'offline' | ||
store.dispatch('snackbar/show', { | ||
message: i18n.global.t(`connection.${snackMsg}`), | ||
timeout: 3000 | ||
}) | ||
} | ||
}, 1000) | ||
} | ||
|
||
const inetConnectionCheck = async () => { | ||
try { | ||
const res = await axios.get(`${checkUrl}`, | ||
{ | ||
headers: { | ||
'Cache-Control': 'no-cache', | ||
'Pragma': 'no-cache', | ||
'Expires': '0', | ||
}, | ||
} | ||
) | ||
return res.status >=200 && res.status < 300 | ||
} catch(err) { | ||
if (err.code === 'ERR_NETWORK') return false | ||
} | ||
} | ||
} |