meet-my-ride 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,21 @@
1
1
  import { AxiosInstance } from "axios";
2
- import { UserLogin, AuthResponse, UserRegisterResponse, VerifyEmailResponse, UserRefreshResponse } from "../users/user.types";
2
+ import { UserLogin, AuthResponse, UserRegisterResponse, VerifyEmailResponse, UserRefreshResponse, User } from "../users/user.types";
3
3
  import { TokenStorage } from "../tokenstorage/tokenstorage";
4
4
  export declare class AuthService {
5
5
  private http;
6
6
  private storage;
7
+ private userSubject;
8
+ user$: import("rxjs").Observable<User | null>;
9
+ private isAuthenticatedSubject;
10
+ isAuthenticated$: import("rxjs").Observable<boolean>;
7
11
  constructor(http: AxiosInstance, storage: TokenStorage);
12
+ private setUser;
13
+ private loadFromStorage;
8
14
  login(data: UserLogin): Promise<AuthResponse>;
9
15
  register(data: any): Promise<UserRegisterResponse>;
10
16
  refresh(refreshToken: string): Promise<UserRefreshResponse>;
11
17
  verifyEmail(userId: string, token: string): Promise<VerifyEmailResponse>;
18
+ logout(): Promise<void>;
19
+ get currentUser(): User | null;
20
+ get isAuthenticated(): boolean;
12
21
  }
@@ -1,16 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AuthService = void 0;
4
+ const rxjs_1 = require("rxjs");
4
5
  class AuthService {
5
6
  constructor(http, storage) {
6
7
  this.http = http;
7
8
  this.storage = storage;
9
+ // Reactive state
10
+ this.userSubject = new rxjs_1.BehaviorSubject(null);
11
+ this.user$ = this.userSubject.asObservable();
12
+ this.isAuthenticatedSubject = new rxjs_1.BehaviorSubject(false);
13
+ this.isAuthenticated$ = this.isAuthenticatedSubject.asObservable();
14
+ this.loadFromStorage();
8
15
  }
16
+ // -------------------------
17
+ // INTERNAL HELPERS
18
+ // -------------------------
19
+ setUser(user) {
20
+ this.userSubject.next(user);
21
+ this.isAuthenticatedSubject.next(!!user);
22
+ }
23
+ async loadFromStorage() {
24
+ var _a, _b;
25
+ const token = await this.storage.getAccessToken();
26
+ const userJson = await ((_b = (_a = this.storage).getUser) === null || _b === void 0 ? void 0 : _b.call(_a)); // Optional in TokenStorage
27
+ if (token && userJson) {
28
+ this.setUser(JSON.parse(userJson));
29
+ }
30
+ else {
31
+ this.setUser(null);
32
+ }
33
+ }
34
+ // -------------------------
35
+ // AUTH METHODS
36
+ // -------------------------
9
37
  async login(data) {
10
38
  const res = await this.http.post("/users/login", data);
39
+ // Save tokens
11
40
  if (res.data.accessToken && res.data.refreshToken) {
12
41
  await this.storage.setTokens(res.data.accessToken, res.data.refreshToken);
13
42
  }
43
+ // Save user (only if your TokenStorage supports it)
44
+ if (this.storage.setUser) {
45
+ await this.storage.setUser(JSON.stringify(res.data.user));
46
+ }
47
+ this.setUser(res.data.user);
14
48
  return res.data;
15
49
  }
16
50
  async register(data) {
@@ -34,5 +68,18 @@ class AuthService {
34
68
  });
35
69
  return res.data;
36
70
  }
71
+ async logout() {
72
+ await this.storage.clear();
73
+ this.setUser(null);
74
+ }
75
+ // -------------------------
76
+ // GETTERS
77
+ // -------------------------
78
+ get currentUser() {
79
+ return this.userSubject.value;
80
+ }
81
+ get isAuthenticated() {
82
+ return this.isAuthenticatedSubject.value;
83
+ }
37
84
  }
38
85
  exports.AuthService = AuthService;
package/dist/client.d.ts CHANGED
@@ -1,8 +1,9 @@
1
+ import { AxiosInstance } from "axios";
1
2
  import { UserService } from "./users/user.service";
2
3
  export declare class MeetMyRideClient {
3
4
  private baseUrl;
4
5
  private apiKey?;
5
- private readonly http;
6
+ readonly http: AxiosInstance;
6
7
  user: UserService;
7
8
  constructor(baseUrl: string, apiKey?: string | undefined);
8
9
  /**
@@ -3,4 +3,6 @@ export interface TokenStorage {
3
3
  getRefreshToken(): Promise<string | null>;
4
4
  setTokens(access: string, refresh: string): Promise<void>;
5
5
  clear(): Promise<void>;
6
+ getUser?(): Promise<string | null>;
7
+ setUser?(user: string): Promise<void>;
6
8
  }
@@ -1,2 +1,3 @@
1
1
  "use strict";
2
+ // tokenstorage/tokenstorage.ts
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "meet-my-ride",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "Private library for the MeetMyRide applications",
5
5
  "repository": {
6
6
  "type": "git",