Fix useAuthentik composable for SSR
- Use useState to pass server headers to client - Fix 'useAuthentik is not defined' error - Ensure headers are read only on server side
This commit is contained in:
@@ -3,31 +3,38 @@
|
|||||||
* Los headers son inyectados por Authentik Proxy Outpost
|
* Los headers son inyectados por Authentik Proxy Outpost
|
||||||
*/
|
*/
|
||||||
export const useAuthentik = () => {
|
export const useAuthentik = () => {
|
||||||
const headers = useRequestHeaders()
|
// Leer headers en el servidor y almacenarlos en state
|
||||||
|
const authentikUser = useState('authentikUser', () => {
|
||||||
|
// Solo en el servidor, leer los headers
|
||||||
|
if (process.server) {
|
||||||
|
const headers = useRequestHeaders()
|
||||||
|
|
||||||
const user = computed(() => {
|
const username = headers['x-authentik-username']
|
||||||
const username = headers['x-authentik-username']
|
const email = headers['x-authentik-email']
|
||||||
const email = headers['x-authentik-email']
|
const name = headers['x-authentik-name']
|
||||||
const name = headers['x-authentik-name']
|
const groups = headers['x-authentik-groups']
|
||||||
const groups = headers['x-authentik-groups']
|
const uid = headers['x-authentik-uid']
|
||||||
const uid = headers['x-authentik-uid']
|
|
||||||
|
|
||||||
// Si no hay username, el usuario no está autenticado
|
// Si no hay username, el usuario no está autenticado
|
||||||
if (!username) {
|
if (!username) {
|
||||||
return null
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
username,
|
||||||
|
email,
|
||||||
|
name,
|
||||||
|
groups: groups ? groups.split('|') : [],
|
||||||
|
uid,
|
||||||
|
// Generar avatar URL usando UI Avatars
|
||||||
|
avatar: `https://ui-avatars.com/api/?name=${encodeURIComponent(name || username)}&background=random&size=128`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return null
|
||||||
username,
|
|
||||||
email,
|
|
||||||
name,
|
|
||||||
groups: groups ? groups.split('|') : [],
|
|
||||||
uid,
|
|
||||||
// Generar avatar URL usando UI Avatars
|
|
||||||
avatar: `https://ui-avatars.com/api/?name=${encodeURIComponent(name || username)}&background=random&size=128`
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const user = computed(() => authentikUser.value)
|
||||||
const isAuthenticated = computed(() => !!user.value)
|
const isAuthenticated = computed(() => !!user.value)
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user