mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-09 05:20:05 +00:00
feat: login for web with server type SASJS
This commit is contained in:
@@ -48,7 +48,9 @@ export interface HttpClient {
|
||||
): Promise<{ result: T; etag: string }>
|
||||
|
||||
getCsrfToken(type: 'general' | 'file'): CsrfToken | undefined
|
||||
saveLocalStorageToken(accessToken: string, refreshToken: string): void
|
||||
clearCsrfTokens(): void
|
||||
clearLocalStorageTokens(): void
|
||||
getBaseUrl(): string
|
||||
}
|
||||
|
||||
@@ -70,6 +72,11 @@ export class RequestClient implements HttpClient {
|
||||
this.createHttpClient(baseUrl, httpsAgentOptions)
|
||||
}
|
||||
|
||||
public saveLocalStorageToken(accessToken: string, refreshToken: string) {
|
||||
localStorage.setItem('accessToken', accessToken)
|
||||
localStorage.setItem('refreshToken', refreshToken)
|
||||
}
|
||||
|
||||
public getCsrfToken(type: 'general' | 'file' = 'general') {
|
||||
return type === 'file' ? this.fileUploadCsrfToken : this.csrfToken
|
||||
}
|
||||
@@ -78,6 +85,10 @@ export class RequestClient implements HttpClient {
|
||||
this.csrfToken = { headerName: '', value: '' }
|
||||
this.fileUploadCsrfToken = { headerName: '', value: '' }
|
||||
}
|
||||
public clearLocalStorageTokens() {
|
||||
localStorage.setItem('accessToken', '')
|
||||
localStorage.setItem('refreshToken', '')
|
||||
}
|
||||
|
||||
public getBaseUrl() {
|
||||
return this.httpClient.defaults.baseURL || ''
|
||||
|
||||
@@ -87,7 +87,7 @@ export class Sas9RequestClient extends RequestClient {
|
||||
})
|
||||
}
|
||||
|
||||
public post<T>(
|
||||
public async post<T>(
|
||||
url: string,
|
||||
data: any,
|
||||
accessToken: string | undefined,
|
||||
|
||||
23
src/request/SasjsRequestClient.ts
Normal file
23
src/request/SasjsRequestClient.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { RequestClient } from './RequestClient'
|
||||
|
||||
/**
|
||||
* Specific request client for SASJS.
|
||||
* Append tokens in headers.
|
||||
*/
|
||||
export class SasjsRequestClient extends RequestClient {
|
||||
getHeaders = (accessToken: string | undefined, contentType: string) => {
|
||||
const headers: any = {}
|
||||
|
||||
if (contentType !== 'application/x-www-form-urlencoded')
|
||||
headers['Content-Type'] = contentType
|
||||
|
||||
headers.Accept = contentType === 'application/json' ? contentType : '*/*'
|
||||
|
||||
if (!accessToken)
|
||||
accessToken = localStorage.getItem('accessToken') ?? undefined
|
||||
|
||||
if (accessToken) headers.Authorization = `Bearer ${accessToken}`
|
||||
|
||||
return headers
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user