1
0
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:
Saad Jutt
2021-12-09 17:13:47 +05:00
parent ebe9c2ffeb
commit c56874fe00
8 changed files with 168 additions and 20 deletions

View File

@@ -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 || ''

View File

@@ -87,7 +87,7 @@ export class Sas9RequestClient extends RequestClient {
})
}
public post<T>(
public async post<T>(
url: string,
data: any,
accessToken: string | undefined,

View 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
}
}