Skip to content

Commit c47cfb7

Browse files
Merge pull request marshallswain#168 from jd1378/add_user_type
fix: allow custom types for User and export AuthenticateData
2 parents 0b490a3 + abb7903 commit c47cfb7

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/use-auth/use-auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ interface UseAuthOptions {
2020
onLogoutError?: ErrorHandler
2121
}
2222

23-
interface AuthenticateData {
23+
export interface AuthenticateData {
2424
strategy: 'jwt' | 'local'
2525
accessToken?: string
2626
email?: string
2727
password?: string
2828
}
2929

30-
export function useAuth<d = AuthenticateData>(options: UseAuthOptions) {
30+
export function useAuth<d = AuthenticateData, U = any>(options: UseAuthOptions) {
3131
const { api, servicePath, skipTokenCheck } = options
3232
const entityService = servicePath ? api.service(servicePath) : null
3333
const entityKey = options.entityKey || 'user'
@@ -47,7 +47,7 @@ export function useAuth<d = AuthenticateData>(options: UseAuthOptions) {
4747

4848
// user
4949
const userId = ref<NullableId>(null)
50-
const user = computed(() => {
50+
const user = computed<U | null>(() => {
5151
if (!entityService || !userId.value)
5252
return null
5353
const u = entityService?.store.itemsById[userId.value]

tests/use-auth/use-auth.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ describe('useAuth in Pinia store', () => {
5151
password?: string
5252
}
5353

54+
interface User {
55+
id: number;
56+
email: string;
57+
}
58+
5459
defineStore('auth', () => {
55-
const utils = useAuth<AuthenticateData>({ api })
60+
const utils = useAuth<AuthenticateData, User>({ api })
5661
utils.reAuthenticate()
5762
return { ...utils }
5863
})

0 commit comments

Comments
 (0)