Skip to content

Commit ed85336

Browse files
authored
Merge pull request #472 from manchenkoff/463-custom-params-login
feat: add optional fetch options to login/logout methods
2 parents cb6c884 + 34c54c7 commit ed85336

File tree

6 files changed

+2567
-2277
lines changed

6 files changed

+2567
-2277
lines changed

docs/content/3.composables/1.useSanctumAuth.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,68 @@ Composable provides 2 computed properties and 4 methods:
1414
- `login` - method for logging in the user
1515
- `logout` - method for logging out the user
1616
- `refreshIdentity` - method for manually re-fetching current authenticated user data
17-
- `checkSession` - method to validate existence of the session (cookie in the browser or token in the storage)
17+
- `checkSession` - method to validate existence of the session (cookie in the browser or token in the storage)
1818

19-
To authenticate a user you should pass the credentials payload as an argument to the `login` method.
19+
To authenticate a user you should pass the credentials payload as an argument to the `login` method.
2020
The payload should contain all fields required by your Laravel Sanctum backend.
2121

2222
```typescript
2323
const { login } = useSanctumAuth();
2424

2525
const userCredentials = {
26-
27-
password: '123123',
28-
};
26+
27+
password: "123123",
28+
}
2929

30-
await login(userCredentials);
30+
await login(userCredentials)
3131
```
3232

33-
If the login operation was successful, the `user` property will be updated with
33+
If the login operation was successful, the `user` property will be updated with
3434
the current user information returned by the Laravel API.
3535

36+
If you do not want to update the `user` property automatically (e.g. *for 2FA authentication*),
37+
you can disable identity fetching by passing optional argument to `login` method:
38+
39+
```typescript
40+
// user identity will not be loaded after successful response
41+
await login(userCredentials, false)
42+
```
43+
3644
By default, methods will use the following Laravel endpoints:
45+
3746
- `/login` to authenticate the user
3847
- `/logout` to log out the user
3948
- `/api/user` to get the current user information
4049
- `/sanctum/csrf-cookie` to get the `CSRF` token cookie
4150

4251
To change the default endpoints, please check the [Configuration](/usage/configuration) section.
4352

53+
### Additional `fetch` options
54+
55+
If you want to pass additional header or change HTTP method for either `login` or `logout` calls,
56+
you can pass optional `options: SanctumFetchOptions` argument.
57+
58+
For example, to log out the user with `DELETE` method instead of default `POST`:
59+
60+
```typescript
61+
const { logout } = useSanctumAuth()
4462

63+
await logout({ method: "DELETE" })
64+
```
65+
66+
Use the same approach when you need to pass additional params to `login` call:
67+
68+
```typescript
69+
const { login } = useSanctumAuth()
70+
71+
const userCredentials = {
72+
73+
password: "123123",
74+
}
75+
76+
await login(
77+
userCredentials,
78+
false,
79+
{ headers: { "X-Custom-Header": "header_value" } }
80+
)
81+
```

docs/package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
"validate": "pnpm lint && pnpm typecheck"
1515
},
1616
"dependencies": {
17-
"@iconify-json/lucide": "^1.2.69",
18-
"@iconify-json/simple-icons": "^1.2.54",
19-
"@iconify-json/vscode-icons": "^1.2.31",
20-
"@nuxt/content": "^3.7.1",
17+
"@iconify-json/lucide": "^1.2.77",
18+
"@iconify-json/simple-icons": "^1.2.61",
19+
"@iconify-json/vscode-icons": "^1.2.37",
20+
"@nuxt/content": "^3.9.0",
2121
"@nuxt/image": "^1.11.0",
22-
"@nuxt/ui": "^4.0.1",
23-
"better-sqlite3": "^12.4.1",
24-
"nuxt": "^4.1.3",
22+
"@nuxt/ui": "^4.2.1",
23+
"better-sqlite3": "^12.5.0",
24+
"nuxt": "^4.2.1",
2525
"nuxt-llms": "0.1.3",
26-
"nuxt-og-image": "^5.1.11"
26+
"nuxt-og-image": "^5.1.12"
2727
},
2828
"devDependencies": {
29-
"@nuxt/eslint": "^1.9.0",
30-
"eslint": "^9.37.0",
29+
"@nuxt/eslint": "^1.11.0",
30+
"eslint": "^9.39.1",
3131
"typescript": "^5.9.3",
32-
"vue-tsc": "^3.1.1"
32+
"vue-tsc": "^3.1.5"
3333
},
3434
"resolutions": {
3535
"unimport": "4.1.1"
3636
},
37-
"packageManager": "pnpm@10.18.1+sha512.77a884a165cbba2d8d1c19e3b4880eee6d2fcabd0d879121e282196b80042351d5eb3ca0935fa599da1dc51265cc68816ad2bddd2a2de5ea9fdf92adbec7cd34"
37+
"packageManager": "pnpm@10.24.0"
3838
}

0 commit comments

Comments
 (0)