mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 01:14:36 +00:00
fix(server): csrf cookie is created explicitly
This commit is contained in:
@@ -223,9 +223,17 @@ export class AuthManager {
|
||||
|
||||
private async getNewLoginForm() {
|
||||
if (this.serverType === ServerType.Sasjs) {
|
||||
// server will be sending CSRF cookie,
|
||||
// server will be sending CSRF token in response,
|
||||
// need to save in cookie so that,
|
||||
// http client will use it automatically
|
||||
return this.requestClient.get('/', undefined)
|
||||
return this.requestClient.get('/', undefined).then(({ result }) => {
|
||||
const cookie =
|
||||
/<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(
|
||||
result as string
|
||||
)?.[1]
|
||||
|
||||
if (cookie) document.cookie = cookie
|
||||
})
|
||||
}
|
||||
|
||||
const { result: formResponse } = await this.requestClient.get<string>(
|
||||
|
||||
@@ -500,11 +500,19 @@ export class RequestClient implements HttpClient {
|
||||
}
|
||||
|
||||
if (e instanceof InvalidCsrfError) {
|
||||
// Fetching root will inject CSRF token in cookie
|
||||
// Fetching root and creating CSRF cookie
|
||||
await this.httpClient
|
||||
.get('/', {
|
||||
withCredentials: true
|
||||
})
|
||||
.then((response) => {
|
||||
const cookie =
|
||||
/<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(
|
||||
response.data
|
||||
)?.[1]
|
||||
|
||||
if (cookie) document.cookie = cookie
|
||||
})
|
||||
.catch((err) => {
|
||||
throw prefixMessage(err, 'Error while re-fetching CSRF token.')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user