@@ -3,6 +3,7 @@ import type { AuthConfig, User } from 'react-native-laravel-sanctum';
33
44class AuthService {
55 private readonly config : AuthConfig | null ;
6+ private csrfToken : string | null = null ; // CSRF-Token speichern
67
78 constructor ( authConfig : AuthConfig ) {
89 if ( authConfig === null ) {
@@ -11,27 +12,75 @@ class AuthService {
1112 this . config = authConfig ;
1213 }
1314
14- private async handleResponse ( response : Response ) {
15+ private async handleResponse ( response : Response ) : Promise < void > {
1516 if ( ! response . ok ) {
16- throw new Error ( 'The request was not successful' ) ;
17+ throw new Error ( 'Request was not successful' ) ;
1718 }
1819 }
1920
21+ private async fetchCSRFToken ( ) {
22+ try {
23+ if ( ! this . config || ! this . config . csrfTokenUrl ) {
24+ return ;
25+ }
26+
27+ const response = await fetch ( this . config . csrfTokenUrl , {
28+ method : 'GET' ,
29+ headers : {
30+ 'Content-Type' : 'application/json' ,
31+ } ,
32+ } ) ;
33+
34+ await this . handleResponse ( response ) ;
35+
36+ // Extrahieren des CSRF-Tokens aus dem Set-Cookie-Header
37+ const setCookieHeader = response . headers . get ( 'set-cookie' ) ;
38+ if ( setCookieHeader ) {
39+ const csrfTokenMatch = setCookieHeader . match ( / X S R F - T O K E N = ( [ ^ ; ] * ) / ) ;
40+ if ( csrfTokenMatch ) {
41+ this . csrfToken = csrfTokenMatch [ 1 ] ?? null ;
42+ }
43+ }
44+ } catch ( error ) {
45+ console . error ( 'Error while fetching CSRF token:' , error ) ;
46+ throw error ;
47+ }
48+ }
49+
50+ private async getRequestHeaders ( ) {
51+ const headers : Record < string , string > = {
52+ 'Content-Type' : 'application/json' ,
53+ } ;
54+
55+ if ( this . csrfToken ) {
56+ headers [ 'X-XSRF-TOKEN' ] = this . csrfToken ;
57+ }
58+
59+ const currentToken = await TokenStorage . getToken ( ) ;
60+ if ( currentToken ) {
61+ headers . Authorization = `Bearer ${ currentToken } ` ;
62+ }
63+
64+ return headers ;
65+ }
66+
2067 async login (
2168 email : string ,
2269 password : string ,
2370 deviceName : string
2471 ) : Promise < boolean > {
2572 try {
2673 if ( ! this . config ) {
27- throw new Error ( 'AuthConfig is null' ) ;
74+ throw new Error ( 'Authentication configuration is missing' ) ;
75+ }
76+
77+ if ( this . config . csrfTokenUrl ) {
78+ await this . fetchCSRFToken ( ) ;
2879 }
2980
3081 const response = await fetch ( this . config . loginUrl , {
3182 method : 'POST' ,
32- headers : {
33- 'Content-Type' : 'application/json' ,
34- } ,
83+ headers : await this . getRequestHeaders ( ) ,
3584 body : JSON . stringify ( {
3685 email,
3786 password,
@@ -51,15 +100,19 @@ class AuthService {
51100 return false ;
52101 }
53102 } catch ( error ) {
54- console . error ( 'Fehler beim Einloggen :' , error ) ;
103+ console . error ( 'Error during login :' , error ) ;
55104 throw error ;
56105 }
57106 }
58107
59108 async logout ( ) : Promise < boolean > {
60109 try {
61110 if ( ! this . config ) {
62- throw new Error ( 'AuthConfig is null' ) ;
111+ throw new Error ( 'Authentication configuration is missing' ) ;
112+ }
113+
114+ if ( this . config . csrfTokenUrl ) {
115+ await this . fetchCSRFToken ( ) ;
63116 }
64117
65118 const currentToken = await TokenStorage . getToken ( ) ;
@@ -70,25 +123,22 @@ class AuthService {
70123
71124 const response = await fetch ( this . config . logoutUrl , {
72125 method : 'POST' ,
73- headers : {
74- 'Content-Type' : 'application/json' ,
75- 'Authorization' : `Bearer ${ currentToken } ` ,
76- } ,
126+ headers : await this . getRequestHeaders ( ) ,
77127 } ) ;
78128
79129 await this . handleResponse ( response ) ;
80130 await TokenStorage . removeToken ( ) ;
81131 return true ;
82132 } catch ( error ) {
83- console . error ( 'Fehler beim Ausloggen :' , error ) ;
133+ console . error ( 'Error during logout :' , error ) ;
84134 throw error ;
85135 }
86136 }
87137
88138 async getUser ( ) : Promise < User | null > {
89139 try {
90140 if ( ! this . config ) {
91- throw new Error ( 'AuthConfig is null ' ) ;
141+ throw new Error ( 'Authentication configuration is missing ' ) ;
92142 }
93143
94144 const currentToken = await TokenStorage . getToken ( ) ;
@@ -97,25 +147,27 @@ class AuthService {
97147 return null ;
98148 }
99149
150+ if ( this . config . csrfTokenUrl ) {
151+ await this . fetchCSRFToken ( ) ;
152+ }
153+
100154 const response = await fetch ( this . config . userUrl , {
101155 method : 'GET' ,
102- headers : {
103- 'Content-Type' : 'application/json' ,
104- 'Authorization' : `Bearer ${ currentToken } ` ,
105- } ,
156+ headers : await this . getRequestHeaders ( ) ,
106157 } ) ;
107158
108159 await this . handleResponse ( response ) ;
109160
110161 const user = await response . json ( ) ;
111162
112163 if ( user ) {
164+ this . csrfToken = null ;
113165 return user ;
114166 } else {
115167 return null ;
116168 }
117169 } catch ( error ) {
118- console . error ( 'Fehler beim Abrufen des Benutzers :' , error ) ;
170+ console . error ( 'Error while fetching user :' , error ) ;
119171 throw error ;
120172 }
121173 }
0 commit comments