-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add the new auth service to prepare for the new sdk #1632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martin-trajanovski
merged 3 commits into
new-sdk-release
from
SWAP-4278-new-sdk-frotnend-code-refactor
Nov 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { Injectable, Inject } from "@angular/core"; | ||
| import { InternalStorage } from "./base.storage"; | ||
| import { User } from "shared/sdk"; | ||
|
|
||
| export interface AccessTokenInterface { | ||
| id?: string; | ||
| ttl?: number; | ||
| scopes?: [string]; | ||
| created?: Date; | ||
| userId?: string; | ||
| user?: User; | ||
| } | ||
|
|
||
| export class SDKToken implements AccessTokenInterface { | ||
| id: string = null; | ||
| ttl: number = null; | ||
| scopes: [string] = null; | ||
| created: Date = null; | ||
| userId: string = null; | ||
| user: User = null; | ||
| rememberMe: boolean = null; | ||
| constructor(data?: AccessTokenInterface) { | ||
| Object.assign(this, data); | ||
| } | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class AuthService { | ||
| private token = new SDKToken(); | ||
|
|
||
| protected prefix = ""; | ||
|
|
||
| constructor(@Inject(InternalStorage) protected storage: InternalStorage) { | ||
| // TODO: Test if all this works with the new changes and removal of any types | ||
| this.token.id = this.load("id"); | ||
| this.token.user = JSON.parse(this.load("user")); | ||
| this.token.userId = this.load("userId"); | ||
| this.token.created = new Date(this.load("created")); | ||
| this.token.ttl = parseInt(this.load("ttl")); | ||
| this.token.rememberMe = this.load("rememberMe") === "true"; | ||
| } | ||
|
|
||
| protected load(prop: string) { | ||
| return decodeURIComponent(this.storage.get(`${this.prefix}${prop}`)); | ||
| } | ||
|
|
||
| protected persist( | ||
| prop: string, | ||
| value: string | User | number | Date | boolean, | ||
| expires?: Date, | ||
| ): void { | ||
| try { | ||
| this.storage.set( | ||
| `${this.prefix}${prop}`, | ||
| typeof value === "object" ? JSON.stringify(value) : value, | ||
| this.token.rememberMe ? expires : null, | ||
| ); | ||
| } catch (err) { | ||
martin-trajanovski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.error("Cannot access local/session storage:", err); | ||
| } | ||
| } | ||
|
|
||
| public clear(): void { | ||
| Object.keys(this.token).forEach((prop: string) => | ||
| this.storage.remove(`${this.prefix}${prop}`), | ||
| ); | ||
| this.token = new SDKToken(); | ||
| } | ||
|
|
||
| public setRememberMe(value: boolean): void { | ||
| this.token.rememberMe = value; | ||
| } | ||
|
|
||
| public setUser(user: User) { | ||
| this.token.user = user; | ||
| this.save(); | ||
| } | ||
|
|
||
| public setToken(token: SDKToken): void { | ||
| this.token = Object.assign({}, this.token, token); | ||
| this.save(); | ||
| } | ||
|
|
||
| public getToken(): SDKToken { | ||
| return <SDKToken>this.token; | ||
| } | ||
|
|
||
| public getAccessTokenId(): string { | ||
| return this.token.id; | ||
| } | ||
|
|
||
| public getCurrentUserId() { | ||
| return this.token.userId; | ||
| } | ||
|
|
||
| public getCurrentUserData() { | ||
| return typeof this.token.user === "string" | ||
| ? JSON.parse(this.token.user) | ||
| : this.token.user; | ||
| } | ||
|
|
||
| public isAuthenticated() { | ||
| return !( | ||
| this.getCurrentUserId() === "" || | ||
| this.getCurrentUserId() == null || | ||
| this.getCurrentUserId() == "null" | ||
| ); | ||
| } | ||
|
|
||
| public save(): boolean { | ||
| const today = new Date(); | ||
| const expires = new Date(today.getTime() + this.token.ttl * 1000); | ||
| this.persist("id", this.token.id, expires); | ||
| this.persist("user", this.token.user, expires); | ||
| this.persist("userId", this.token.userId, expires); | ||
| this.persist("created", this.token.created, expires); | ||
| this.persist("ttl", this.token.ttl, expires); | ||
| this.persist("rememberMe", this.token.rememberMe, expires); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
|
||
| export class BaseStorage { | ||
| get(key: string): string | undefined { | ||
| return undefined; | ||
| } | ||
|
|
||
| set(key: string, value: string | number | boolean, expires?: Date): void {} | ||
|
|
||
| remove(key: string): void {} | ||
| } | ||
|
|
||
| export class InternalStorage extends BaseStorage {} | ||
|
|
||
| export class SDKStorage extends BaseStorage {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { Injectable } from "@angular/core"; | ||
| export interface CookieInterface { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| [key: string]: any; | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class CookieBrowser { | ||
| private cookies: CookieInterface = {}; | ||
|
|
||
| private parse(value: string) { | ||
| try { | ||
| return JSON.parse(decodeURI(value)); | ||
| } catch (e) { | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| get(key: string): string { | ||
| if (!this.cookies[key]) { | ||
| const cookie = window.document.cookie | ||
| .split("; ") | ||
| .filter((item) => item.split("=")[0] === key) | ||
| .pop(); | ||
| if (!cookie) { | ||
| return null; | ||
| } | ||
|
|
||
| this.cookies[key] = this.parse(cookie.split("=").slice(1).join("=")); | ||
| } | ||
|
|
||
| return this.cookies[key]; | ||
| } | ||
|
|
||
| set(key: string, value: string, expires?: Date): void { | ||
| this.cookies[key] = value; | ||
| const cookie = `${key}=${encodeURI(value)}; path=/${ | ||
| expires ? `; expires=${expires.toUTCString()}` : "" | ||
| }`; | ||
| window.document.cookie = cookie; | ||
| } | ||
|
|
||
| remove(key: string) { | ||
| document.cookie = key + "=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;"; | ||
| delete this.cookies[key]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { Injectable } from "@angular/core"; | ||
|
|
||
| @Injectable() | ||
| export class LocalStorageBrowser { | ||
| private parse(value: string) { | ||
| try { | ||
| return JSON.parse(value); | ||
| } catch (e) { | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| get(key: string) { | ||
| const data: string = localStorage.getItem(key); | ||
| return this.parse(data); | ||
| } | ||
|
|
||
| set(key: string, value: string): void { | ||
| localStorage.setItem( | ||
| key, | ||
| typeof value === "object" ? JSON.stringify(value) : value, | ||
| ); | ||
| } | ||
|
|
||
| remove(key: string): void { | ||
| if (localStorage[key]) { | ||
| localStorage.removeItem(key); | ||
| } else { | ||
| console.log("Trying to remove unexisting key: ", key); | ||
martin-trajanovski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.