-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): 🏷️ Improved typings for config and composables
- Loading branch information
Showing
9 changed files
with
114 additions
and
106 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
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,38 +1,43 @@ | ||
import { useState, computed, useRequestFetch, navigateTo } from '#imports' | ||
import type { Ref, ComputedRef } from '#imports' | ||
import type { ProviderKeys } from '../types/oidc' | ||
import type { UserSession } from '../types/session' | ||
|
||
const useSessionState = () => useState<UserSession>('nuxt-session', () => ({})) | ||
|
||
export const useOidcAuth = () => { | ||
const sessionState = useSessionState() | ||
const sessionState: Ref<UserSession> = useSessionState() | ||
const user: ComputedRef<UserSession> = computed(() => sessionState.value || null) | ||
const loggedIn: ComputedRef<boolean> = computed<boolean>(() => Boolean(sessionState.value.loggedInAt)) | ||
const currentProvider: ComputedRef<ProviderKeys | undefined> = computed(() => sessionState.value.provider || undefined) | ||
async function fetch() { | ||
useSessionState().value = (await useRequestFetch()('/api/_auth/session', { | ||
headers: { | ||
Accept: 'text/json' | ||
} | ||
}).catch(() => ({})) as UserSession) | ||
} | ||
|
||
async function refresh() { | ||
await $fetch('/api/_auth/refresh', { method: 'POST' }) | ||
await fetch() | ||
} | ||
|
||
async function login(provider?: ProviderKeys) { | ||
await navigateTo(`/auth${provider ? '/' + provider : ''}/login`, { external: true, redirectCode: 302 }) | ||
} | ||
|
||
async function logout(provider?: ProviderKeys) { | ||
await navigateTo(`/auth${provider ? '/' + provider : ''}/logout`, { external: true }) | ||
} | ||
|
||
return { | ||
loggedIn: computed(() => Boolean(sessionState.value.loggedInAt)), | ||
user: computed(() => sessionState.value || null), | ||
currentProvider: computed(() => sessionState.value.provider || ''), | ||
loggedIn, | ||
user, | ||
currentProvider, | ||
fetch, | ||
refresh, | ||
login, | ||
logout, | ||
} | ||
} | ||
|
||
async function fetch() { | ||
useSessionState().value = (await useRequestFetch()('/api/_auth/session', { | ||
headers: { | ||
Accept: 'text/json' | ||
} | ||
}).catch(() => ({})) as UserSession) | ||
} | ||
|
||
async function refresh() { | ||
await $fetch('/api/_auth/refresh', { method: 'POST' }) | ||
await fetch() | ||
} | ||
|
||
async function login(provider?: string) { | ||
navigateTo(`/auth${provider ? '/' + provider : ''}/login`, { external: true, redirectCode: 302 }) | ||
} | ||
|
||
async function logout(provider?: string) { | ||
navigateTo(`/auth${provider ? '/' + provider : ''}/logout`, { external: true }) | ||
} |
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
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
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