@@ -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.
2020The payload should contain all fields required by your Laravel Sanctum backend.
2121
2222``` typescript
2323const { login } = useSanctumAuth ();
2424
2525const 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
3434the 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+
3644By 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
4251To 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+ ```
0 commit comments