Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Add refresh option to getUser method #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ const auth = useAuth() // return auth state

onMounted(async () => {
await $sanctumAuth.getUser() // fetch and set user data
// await $sanctumAuth.getUser({ refresh: true }) // force fetch user data

loading.value = false
})
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export default defineNuxtPlugin(async () => {
useCookie(config.csrf.tokenCookieKey).value = null
}

async function getUser<T>(): Promise<T | undefined> {
if (auth.value.loggedIn && auth.value.user) {
async function getUser<T>({ refresh = false }: {refresh?: boolean; } = {}): Promise<T | undefined> {
if (!refresh && auth.value.loggedIn && auth.value.user) {
return auth.value.user as T
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type Callback = (response: any) => void
export interface SanctumAuthPlugin {
login: (data: any, callback?: Callback | undefined) => Promise<void>
logout: (callback?: Callback | undefined) => Promise<void>
getUser<T>(): Promise<T | undefined>
getUser<T>({ refresh }?: { refresh?: boolean }): () => Promise<T | undefined>
}

// @ts-ignore
Expand Down