Skip to content

Instantly share code, notes, and snippets.

@alexvbush
Last active April 24, 2023 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexvbush/373db51131105be3554783710649b3d4 to your computer and use it in GitHub Desktop.
Save alexvbush/373db51131105be3554783710649b3d4 to your computer and use it in GitHub Desktop.
import { AxiosInstance } from "axios";
import RxAxios from 'axios-observable';
import { delay, map, Observable, of, Subscription } from "rxjs";
export class Session {
constructor(
readonly accessToken: string,
readonly client: string,
readonly uid: string,
readonly expiry: string
) { }
}
namespace Authentication {
export enum API {
V1 = "api/v1/",
Auth = "auth/",
sign_in = "sign_in",
sign_out = "sign_out"
}
}
export interface AuthenticationServiceInterface {
signIn(email: string, password: string): Observable<Session>
signOut(uid: string, client: string, accessToken: string): Observable<any>
}
export class AuthenticationService implements AuthenticationServiceInterface {
constructor(private readonly rxHttpClient: RxAxios) {
}
signIn(email: string, password: string): Observable<Session> {
const signInURL = Authentication.API.V1 + Authentication.API.Auth + Authentication.API.sign_in// + ".json"
const something = "123"
// return this.rxHttpClient.post<string>(signInURL, { email: email, password: password }, {
return this.rxHttpClient.post<string>(signInURL, { email, something }, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
}).pipe(map(response => {
console.log(response);
const accessToken = response.headers["access-token"]
const client = response.headers["client"]
const uid = response.headers["uid"]
const expiry = response.headers["expiry"]
return new Session(accessToken, client, uid, expiry)
}))
}
signOut(uid: string, client: string, accessToken: string): Observable<any> {
const signOutURL =
Authentication.API.V1 + Authentication.API.Auth + Authentication.API.sign_out
return this.rxHttpClient.delete(signOutURL, {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"access-token": accessToken,
client: client,
uid: uid,
},
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment