Skip to content

Commit

Permalink
Merge pull request #38 from olafsh/develop
Browse files Browse the repository at this point in the history
feat: add get access token method
  • Loading branch information
danieljausovec authored Oct 1, 2024
2 parents 2a66c45 + 9e9c064 commit 156003e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ await sdk.handleRedirectCallback()
// Check current authentication status
console.log(await sdk.isAuthenticated);

// Get access token
const accessToken = sdk.accessToken;

// Perform a logout
await sdk.logout();
```
Expand Down
51 changes: 25 additions & 26 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ class OLAFSDK {
return this._DEFAULT_LANGUAGE;
}

get accessToken(): string {
const auth = this.getAuthFromLocalStorage();
if (auth !== undefined) {
return auth.access_token;
}
return "";
}

public fetchConfig(): Promise<any> {
const headers = { "X-APP-HOST": getHost() ?? "" };
return fetchData("GET", `${this.OLAF_PUBLIC_ENDPOINT}${this.CONFIG_PATH}`, null, headers)
Expand Down Expand Up @@ -108,12 +116,7 @@ class OLAFSDK {

public logout(): Promise<void> | undefined {
const config = this.config;
const auth = this.getAuthFromLocalStorage();
if (!auth || !auth.access_token) {
return Promise.reject("No access token");
}

const headers = { Authorization: `Bearer ${auth.access_token}` };
const headers = { Authorization: `Bearer ${this.accessToken}` };
return fetchData("POST", `${config?.api_endpoint}${this.LOGOUT_PATH}`, null, headers, true).then(() => {
localStorage.removeItem(this.ACCESS_TOKEN_STORAGE_KEY);
window.location.href = window.location.origin;
Expand All @@ -123,11 +126,7 @@ class OLAFSDK {
}

public async verifyToken(): Promise<boolean> {
const auth = this.getAuthFromLocalStorage();
if (!auth || !auth.access_token) {
return Promise.resolve(false);
}
const headers = { Authorization: `Bearer ${auth.access_token}` };
const headers = { Authorization: `Bearer ${this.accessToken}` };
return await fetchData("POST", `${this.config?.api_endpoint}${this.VERIFY_TOKEN_PATH}`, null, headers)
.then(() => {
this.setIsAuthenticated(true);
Expand Down Expand Up @@ -174,7 +173,21 @@ class OLAFSDK {
});
}

public getAccessToken(code_verifier: string, code: string | undefined) {
// public getRefreshToken(token: string): Promise<any> {
// const config = this.config;
// const body = {
// refresh_token: token,
// client_id: config?.client_id,
// grant_type: "refresh_token",
// };
// return fetchData("POST", `${config?.api_endpoint}${this.ACCESS_TOKEN_PATH}`, JSON.stringify(body));
// }

public setLanguage(language: string): void {
this._language = language;
}

private getAccessToken(code_verifier: string, code: string | undefined) {
const config = this.config;
const headers = new Headers({
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
Expand All @@ -196,20 +209,6 @@ class OLAFSDK {
return fetchData("POST", `${config?.api_endpoint}${this.ACCESS_TOKEN_PATH}`, formBody, headers);
}

public getRefreshToken(token: string): Promise<any> {
const config = this.config;
const body = {
refresh_token: token,
client_id: config?.client_id,
grant_type: "refresh_token",
};
return fetchData("POST", `${config?.api_endpoint}${this.ACCESS_TOKEN_PATH}`, JSON.stringify(body));
}

public setLanguage(language: string): void {
this._language = language;
}

private setIsAuthenticated(isAuthenticated: boolean): void {
this._isAuthenticated = isAuthenticated;
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@olafsh/olaf-sdk-js",
"version": "1.0.2",
"version": "1.0.3",
"description": "OLAF SDK for JavaScript",
"files": [
"dist/",
Expand Down

0 comments on commit 156003e

Please sign in to comment.