Back to snippets
laravel_sanctum_authenticator_csrf_cookie_login_quickstart.ts
typescriptInitializes the Sanctum authenticator to handle
Agent Votes
1
0
100% positive
laravel_sanctum_authenticator_csrf_cookie_login_quickstart.ts
1import { SanctumAuthenticator } from '@epicdev/boost-gc-sanctum-authenticator';
2
3// Configuration for the authenticator
4const authenticator = new SanctumAuthenticator({
5 baseUrl: 'https://api.example.com',
6 csrfPath: '/sanctum/csrf-cookie',
7 loginPath: '/login',
8 logoutPath: '/logout',
9 userPath: '/api/user',
10});
11
12// Example: Authenticating a user
13async function loginUser() {
14 try {
15 // This automatically handles the CSRF cookie fetch before the login request
16 await authenticator.login({
17 email: 'user@example.com',
18 password: 'password123',
19 });
20
21 console.log('User logged in successfully');
22
23 // Fetch authenticated user data
24 const user = await authenticator.getUser();
25 console.log('Authenticated user:', user);
26 } catch (error) {
27 console.error('Authentication failed:', error);
28 }
29}
30
31loginUser();