2022-10-05 06:44:18 +00:00
|
|
|
import { useUserinfoStore, Userinfo } from '@/stores/UserinfoStore'
|
|
|
|
import { useEngineStore } from '@/stores/EngineStore'
|
|
|
|
import { useGameinfoStore } from '@/stores/GameinfoStore'
|
|
|
|
import { usePlayersStore } from '@/stores/PlayersStore'
|
|
|
|
import { useRoundStore } from '@/stores/RoundStore'
|
|
|
|
import useI18n from './useI18n'
|
2022-07-31 20:56:53 +00:00
|
|
|
import { $fetch } from 'ohmyfetch'
|
2022-07-29 21:41:24 +00:00
|
|
|
|
2023-01-29 15:07:24 +00:00
|
|
|
export type AllowRole = '' | 'player' | 'gamemaster' | 'admin' | 'setup'
|
2022-09-11 18:02:24 +00:00
|
|
|
export type AllowRoles = Array<AllowRole>
|
|
|
|
|
2022-07-29 13:08:12 +00:00
|
|
|
export interface useAuth {
|
2022-07-29 19:11:09 +00:00
|
|
|
login(authCode: string): Promise<void>
|
|
|
|
logout(): Promise<void>
|
2022-09-11 18:02:24 +00:00
|
|
|
authenticateAndLoadUserInfo(allowRoles: AllowRoles): Promise<void>
|
2022-07-29 13:08:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:56:53 +00:00
|
|
|
export default (): useAuth => {
|
2022-09-11 18:02:24 +00:00
|
|
|
const user = useUserinfoStore()
|
2022-07-29 21:41:24 +00:00
|
|
|
|
2022-07-29 13:08:12 +00:00
|
|
|
return {
|
2022-09-11 18:02:24 +00:00
|
|
|
authenticateAndLoadUserInfo: async (allowRoles: AllowRoles): Promise<void> => {
|
|
|
|
try {
|
|
|
|
const userInfo = await $fetch('/api/userinfo') as Userinfo
|
|
|
|
user.setUserInfo(userInfo)
|
2022-09-30 06:43:40 +00:00
|
|
|
useI18n({}).setLang(userInfo.lang)
|
2022-09-11 18:02:24 +00:00
|
|
|
if (allowRoles.indexOf(userInfo.role) >= 0 ) {
|
2023-01-29 14:36:06 +00:00
|
|
|
// user is authenticated and authorized, let the user in
|
2022-09-11 18:02:24 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-29 14:36:06 +00:00
|
|
|
|
2023-01-29 15:07:24 +00:00
|
|
|
// game is not initialized yet, needs setup
|
|
|
|
if (userInfo.role === 'setup') {
|
|
|
|
document.location.pathname = '/setup'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-29 14:36:06 +00:00
|
|
|
// user is authenticated but not authorized for this page
|
2022-09-11 18:02:24 +00:00
|
|
|
if (user.isAdmin) {
|
2022-09-25 15:23:27 +00:00
|
|
|
document.location.pathname = '/admin'
|
|
|
|
// can't use navigateTo() for it fails with DOMException if two consecutive redirects happen (at least in docker container)
|
|
|
|
// navigateTo('/admin', { replace: true })
|
2022-09-11 18:02:24 +00:00
|
|
|
} else {
|
2022-09-25 15:23:27 +00:00
|
|
|
document.location.pathname = '/play'
|
2022-09-11 18:02:24 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2023-01-29 14:36:06 +00:00
|
|
|
// user is not authenticated
|
2022-09-11 18:02:24 +00:00
|
|
|
if (allowRoles.indexOf('') == -1 ) {
|
2022-09-25 15:23:27 +00:00
|
|
|
document.location.pathname = '/'
|
2022-09-11 18:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-29 21:41:24 +00:00
|
|
|
},
|
|
|
|
|
2022-07-29 19:11:09 +00:00
|
|
|
login: async (authCode: string): Promise<void> => {
|
2022-07-29 13:31:36 +00:00
|
|
|
if (authCode.length != 6) {
|
2022-07-29 13:08:12 +00:00
|
|
|
throw Error('login failed')
|
|
|
|
}
|
2022-07-29 13:31:36 +00:00
|
|
|
const resp = await $fetch(`/api/login?code=${authCode}`)
|
2022-07-29 13:08:12 +00:00
|
|
|
if (resp !== 'ok') {
|
|
|
|
throw Error('login failed')
|
|
|
|
}
|
|
|
|
},
|
2022-07-29 19:11:09 +00:00
|
|
|
|
|
|
|
logout: async (): Promise<void> => {
|
|
|
|
await $fetch('/api/logout')
|
2022-10-05 06:44:18 +00:00
|
|
|
useEngineStore().reset()
|
|
|
|
useGameinfoStore().reset()
|
|
|
|
usePlayersStore().reset()
|
|
|
|
useUserinfoStore().reset()
|
|
|
|
useRoundStore().reset()
|
2022-07-29 19:11:09 +00:00
|
|
|
},
|
2022-07-29 13:08:12 +00:00
|
|
|
}
|
|
|
|
}
|