@bagelink/auth 1.1.43 → 1.1.47

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.
package/dist/index.cjs CHANGED
@@ -1,11 +1,22 @@
1
1
  'use strict';
2
2
 
3
3
  const axios = require('axios');
4
+ const vue = require('vue');
4
5
 
5
6
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
7
 
7
8
  const axios__default = /*#__PURE__*/_interopDefaultCompat(axios);
8
9
 
10
+ function createAxiosInstance(baseURL = "") {
11
+ return axios__default.create({
12
+ baseURL: baseURL || "",
13
+ headers: {
14
+ "Content-Type": "application/json",
15
+ "withCredentials": true
16
+ }
17
+ });
18
+ }
19
+
9
20
  var __defProp = Object.defineProperty;
10
21
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
22
  var __publicField = (obj, key, value) => {
@@ -13,14 +24,9 @@ var __publicField = (obj, key, value) => {
13
24
  return value;
14
25
  };
15
26
  class AuthApi {
16
- constructor(baseURL) {
27
+ constructor(axiosInstance, baseURL = "") {
17
28
  __publicField(this, "api");
18
- this.api = axios__default.create({
19
- baseURL,
20
- headers: {
21
- "Content-Type": "application/json"
22
- }
23
- });
29
+ this.api = axiosInstance || createAxiosInstance(baseURL);
24
30
  this.setupInterceptors();
25
31
  }
26
32
  setupInterceptors() {
@@ -76,13 +82,17 @@ class AuthApi {
76
82
  return this.api.patch("/users/me", user);
77
83
  }
78
84
  async setUserStatus(userId, isActive) {
79
- return this.api.patch(`/users/${userId}`, { is_active: isActive });
85
+ return this.api.patch(`/users/${userId}`, {
86
+ is_active: isActive
87
+ });
80
88
  }
81
89
  async deleteUser(userId) {
82
90
  return this.api.delete(`/users/${userId}`);
83
91
  }
84
92
  async getUsers(limit = 100, skip) {
85
- return this.api.get("/users/", { params: { skip, limit } });
93
+ return this.api.get("/users/", {
94
+ params: { skip, limit }
95
+ });
86
96
  }
87
97
  async createUser(user) {
88
98
  return this.api.post("/users/", user);
@@ -93,22 +103,20 @@ class AuthApi {
93
103
  }
94
104
 
95
105
  let authApi = null;
96
- const currentUser = {
97
- value: {
98
- id: "",
99
- email: "",
100
- first_name: "",
101
- last_name: "",
102
- is_superuser: false,
103
- is_active: false
104
- },
105
- set: (newValue) => {
106
- currentUser.value = newValue;
107
- }
108
- };
109
- function initAuth(baseURL) {
106
+ const currentUser = vue.ref({
107
+ id: "",
108
+ email: "",
109
+ first_name: "",
110
+ last_name: "",
111
+ is_superuser: false,
112
+ is_active: false
113
+ });
114
+ function initAuth({
115
+ axios,
116
+ baseURL
117
+ }) {
110
118
  if (!authApi) {
111
- authApi = new AuthApi(baseURL);
119
+ authApi = new AuthApi(axios, baseURL);
112
120
  }
113
121
  return {
114
122
  install(app) {
@@ -131,7 +139,10 @@ function useAuth() {
131
139
  }
132
140
  async function login(credentials) {
133
141
  try {
134
- await authApi.login(credentials.email.toLowerCase(), credentials.password);
142
+ await authApi.login(
143
+ credentials.email.toLowerCase(),
144
+ credentials.password
145
+ );
135
146
  await checkAuth();
136
147
  } catch (error) {
137
148
  throw error;
@@ -141,7 +152,7 @@ function useAuth() {
141
152
  try {
142
153
  if (!getIsLoggedIn()) {
143
154
  const { data } = await authApi.getCurrentUser();
144
- currentUser.set(data);
155
+ currentUser.value = data;
145
156
  }
146
157
  } catch (error) {
147
158
  return false;
@@ -154,7 +165,7 @@ function useAuth() {
154
165
  throw new Error("Passwords do not match");
155
166
  }
156
167
  const { data } = await authApi.signup(user);
157
- currentUser.set(data);
168
+ currentUser.value = data;
158
169
  } catch (error) {
159
170
  throw error;
160
171
  }
@@ -186,7 +197,7 @@ function useAuth() {
186
197
  async function updateProfile(user) {
187
198
  try {
188
199
  const { data } = await authApi.updateUserProfile(user);
189
- currentUser.set({ ...currentUser.value, ...data });
200
+ currentUser.value = { ...currentUser.value, ...data };
190
201
  } catch (error) {
191
202
  throw error;
192
203
  }
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
- import { AxiosResponse } from 'axios';
1
+ import { AxiosResponse, AxiosInstance } from 'axios';
2
+ import * as vue from 'vue';
2
3
  import { App } from 'vue';
3
4
 
4
5
  interface User {
@@ -23,12 +24,6 @@ interface UpdatePasswordForm {
23
24
  new_password: string;
24
25
  confirmNewPassword: string;
25
26
  }
26
- interface ReactiveFactory {
27
- <T>(initial: T): {
28
- value: T;
29
- set: (newValue: T) => void;
30
- };
31
- }
32
27
  interface Token {
33
28
  access_token: string;
34
29
  }
@@ -89,7 +84,7 @@ type SignupResponse = AxiosResponse<SanitizedUserOut>;
89
84
 
90
85
  declare class AuthApi {
91
86
  private api;
92
- constructor(baseURL?: string);
87
+ constructor(axiosInstance?: AxiosInstance, baseURL?: string);
93
88
  private setupInterceptors;
94
89
  login(username: string, password: string): Promise<LoginResponse>;
95
90
  logout(): void;
@@ -106,14 +101,28 @@ declare class AuthApi {
106
101
  getUser(userId: string): Promise<GetUserResponse>;
107
102
  }
108
103
 
109
- declare function initAuth(baseURL?: string): {
104
+ declare function initAuth({ axios, baseURL, }: {
105
+ axios: AxiosInstance;
106
+ baseURL?: string;
107
+ }): {
110
108
  install(app: App): void;
111
109
  };
112
110
  declare function useAuth(): {
113
- currentUser: {
114
- value: User;
115
- set: (newValue: User) => void;
116
- };
111
+ currentUser: vue.Ref<{
112
+ id: string;
113
+ email: string;
114
+ first_name?: string | undefined;
115
+ last_name?: string | undefined;
116
+ is_superuser?: boolean | undefined;
117
+ is_active?: boolean | undefined;
118
+ }, User | {
119
+ id: string;
120
+ email: string;
121
+ first_name?: string | undefined;
122
+ last_name?: string | undefined;
123
+ is_superuser?: boolean | undefined;
124
+ is_active?: boolean | undefined;
125
+ }>;
117
126
  getFullName: () => string;
118
127
  getIsLoggedIn: () => boolean;
119
128
  logout: () => Promise<void>;
@@ -131,4 +140,4 @@ declare function useAuth(): {
131
140
  deleteUser: (userId: string) => Promise<void>;
132
141
  };
133
142
 
134
- export { AuthApi, type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ReactiveFactory, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, initAuth, useAuth };
143
+ export { AuthApi, type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, initAuth, useAuth };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { AxiosResponse } from 'axios';
1
+ import { AxiosResponse, AxiosInstance } from 'axios';
2
+ import * as vue from 'vue';
2
3
  import { App } from 'vue';
3
4
 
4
5
  interface User {
@@ -23,12 +24,6 @@ interface UpdatePasswordForm {
23
24
  new_password: string;
24
25
  confirmNewPassword: string;
25
26
  }
26
- interface ReactiveFactory {
27
- <T>(initial: T): {
28
- value: T;
29
- set: (newValue: T) => void;
30
- };
31
- }
32
27
  interface Token {
33
28
  access_token: string;
34
29
  }
@@ -89,7 +84,7 @@ type SignupResponse = AxiosResponse<SanitizedUserOut>;
89
84
 
90
85
  declare class AuthApi {
91
86
  private api;
92
- constructor(baseURL?: string);
87
+ constructor(axiosInstance?: AxiosInstance, baseURL?: string);
93
88
  private setupInterceptors;
94
89
  login(username: string, password: string): Promise<LoginResponse>;
95
90
  logout(): void;
@@ -106,14 +101,28 @@ declare class AuthApi {
106
101
  getUser(userId: string): Promise<GetUserResponse>;
107
102
  }
108
103
 
109
- declare function initAuth(baseURL?: string): {
104
+ declare function initAuth({ axios, baseURL, }: {
105
+ axios: AxiosInstance;
106
+ baseURL?: string;
107
+ }): {
110
108
  install(app: App): void;
111
109
  };
112
110
  declare function useAuth(): {
113
- currentUser: {
114
- value: User;
115
- set: (newValue: User) => void;
116
- };
111
+ currentUser: vue.Ref<{
112
+ id: string;
113
+ email: string;
114
+ first_name?: string | undefined;
115
+ last_name?: string | undefined;
116
+ is_superuser?: boolean | undefined;
117
+ is_active?: boolean | undefined;
118
+ }, User | {
119
+ id: string;
120
+ email: string;
121
+ first_name?: string | undefined;
122
+ last_name?: string | undefined;
123
+ is_superuser?: boolean | undefined;
124
+ is_active?: boolean | undefined;
125
+ }>;
117
126
  getFullName: () => string;
118
127
  getIsLoggedIn: () => boolean;
119
128
  logout: () => Promise<void>;
@@ -131,4 +140,4 @@ declare function useAuth(): {
131
140
  deleteUser: (userId: string) => Promise<void>;
132
141
  };
133
142
 
134
- export { AuthApi, type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ReactiveFactory, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, initAuth, useAuth };
143
+ export { AuthApi, type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, initAuth, useAuth };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { AxiosResponse } from 'axios';
1
+ import { AxiosResponse, AxiosInstance } from 'axios';
2
+ import * as vue from 'vue';
2
3
  import { App } from 'vue';
3
4
 
4
5
  interface User {
@@ -23,12 +24,6 @@ interface UpdatePasswordForm {
23
24
  new_password: string;
24
25
  confirmNewPassword: string;
25
26
  }
26
- interface ReactiveFactory {
27
- <T>(initial: T): {
28
- value: T;
29
- set: (newValue: T) => void;
30
- };
31
- }
32
27
  interface Token {
33
28
  access_token: string;
34
29
  }
@@ -89,7 +84,7 @@ type SignupResponse = AxiosResponse<SanitizedUserOut>;
89
84
 
90
85
  declare class AuthApi {
91
86
  private api;
92
- constructor(baseURL?: string);
87
+ constructor(axiosInstance?: AxiosInstance, baseURL?: string);
93
88
  private setupInterceptors;
94
89
  login(username: string, password: string): Promise<LoginResponse>;
95
90
  logout(): void;
@@ -106,14 +101,28 @@ declare class AuthApi {
106
101
  getUser(userId: string): Promise<GetUserResponse>;
107
102
  }
108
103
 
109
- declare function initAuth(baseURL?: string): {
104
+ declare function initAuth({ axios, baseURL, }: {
105
+ axios: AxiosInstance;
106
+ baseURL?: string;
107
+ }): {
110
108
  install(app: App): void;
111
109
  };
112
110
  declare function useAuth(): {
113
- currentUser: {
114
- value: User;
115
- set: (newValue: User) => void;
116
- };
111
+ currentUser: vue.Ref<{
112
+ id: string;
113
+ email: string;
114
+ first_name?: string | undefined;
115
+ last_name?: string | undefined;
116
+ is_superuser?: boolean | undefined;
117
+ is_active?: boolean | undefined;
118
+ }, User | {
119
+ id: string;
120
+ email: string;
121
+ first_name?: string | undefined;
122
+ last_name?: string | undefined;
123
+ is_superuser?: boolean | undefined;
124
+ is_active?: boolean | undefined;
125
+ }>;
117
126
  getFullName: () => string;
118
127
  getIsLoggedIn: () => boolean;
119
128
  logout: () => Promise<void>;
@@ -131,4 +140,4 @@ declare function useAuth(): {
131
140
  deleteUser: (userId: string) => Promise<void>;
132
141
  };
133
142
 
134
- export { AuthApi, type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ReactiveFactory, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, initAuth, useAuth };
143
+ export { AuthApi, type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, initAuth, useAuth };
package/dist/index.mjs CHANGED
@@ -1,4 +1,15 @@
1
1
  import axios from 'axios';
2
+ import { ref } from 'vue';
3
+
4
+ function createAxiosInstance(baseURL = "") {
5
+ return axios.create({
6
+ baseURL: baseURL || "",
7
+ headers: {
8
+ "Content-Type": "application/json",
9
+ "withCredentials": true
10
+ }
11
+ });
12
+ }
2
13
 
3
14
  var __defProp = Object.defineProperty;
4
15
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -7,14 +18,9 @@ var __publicField = (obj, key, value) => {
7
18
  return value;
8
19
  };
9
20
  class AuthApi {
10
- constructor(baseURL) {
21
+ constructor(axiosInstance, baseURL = "") {
11
22
  __publicField(this, "api");
12
- this.api = axios.create({
13
- baseURL,
14
- headers: {
15
- "Content-Type": "application/json"
16
- }
17
- });
23
+ this.api = axiosInstance || createAxiosInstance(baseURL);
18
24
  this.setupInterceptors();
19
25
  }
20
26
  setupInterceptors() {
@@ -70,13 +76,17 @@ class AuthApi {
70
76
  return this.api.patch("/users/me", user);
71
77
  }
72
78
  async setUserStatus(userId, isActive) {
73
- return this.api.patch(`/users/${userId}`, { is_active: isActive });
79
+ return this.api.patch(`/users/${userId}`, {
80
+ is_active: isActive
81
+ });
74
82
  }
75
83
  async deleteUser(userId) {
76
84
  return this.api.delete(`/users/${userId}`);
77
85
  }
78
86
  async getUsers(limit = 100, skip) {
79
- return this.api.get("/users/", { params: { skip, limit } });
87
+ return this.api.get("/users/", {
88
+ params: { skip, limit }
89
+ });
80
90
  }
81
91
  async createUser(user) {
82
92
  return this.api.post("/users/", user);
@@ -87,22 +97,20 @@ class AuthApi {
87
97
  }
88
98
 
89
99
  let authApi = null;
90
- const currentUser = {
91
- value: {
92
- id: "",
93
- email: "",
94
- first_name: "",
95
- last_name: "",
96
- is_superuser: false,
97
- is_active: false
98
- },
99
- set: (newValue) => {
100
- currentUser.value = newValue;
101
- }
102
- };
103
- function initAuth(baseURL) {
100
+ const currentUser = ref({
101
+ id: "",
102
+ email: "",
103
+ first_name: "",
104
+ last_name: "",
105
+ is_superuser: false,
106
+ is_active: false
107
+ });
108
+ function initAuth({
109
+ axios,
110
+ baseURL
111
+ }) {
104
112
  if (!authApi) {
105
- authApi = new AuthApi(baseURL);
113
+ authApi = new AuthApi(axios, baseURL);
106
114
  }
107
115
  return {
108
116
  install(app) {
@@ -125,7 +133,10 @@ function useAuth() {
125
133
  }
126
134
  async function login(credentials) {
127
135
  try {
128
- await authApi.login(credentials.email.toLowerCase(), credentials.password);
136
+ await authApi.login(
137
+ credentials.email.toLowerCase(),
138
+ credentials.password
139
+ );
129
140
  await checkAuth();
130
141
  } catch (error) {
131
142
  throw error;
@@ -135,7 +146,7 @@ function useAuth() {
135
146
  try {
136
147
  if (!getIsLoggedIn()) {
137
148
  const { data } = await authApi.getCurrentUser();
138
- currentUser.set(data);
149
+ currentUser.value = data;
139
150
  }
140
151
  } catch (error) {
141
152
  return false;
@@ -148,7 +159,7 @@ function useAuth() {
148
159
  throw new Error("Passwords do not match");
149
160
  }
150
161
  const { data } = await authApi.signup(user);
151
- currentUser.set(data);
162
+ currentUser.value = data;
152
163
  } catch (error) {
153
164
  throw error;
154
165
  }
@@ -180,7 +191,7 @@ function useAuth() {
180
191
  async function updateProfile(user) {
181
192
  try {
182
193
  const { data } = await authApi.updateUserProfile(user);
183
- currentUser.set({ ...currentUser.value, ...data });
194
+ currentUser.value = { ...currentUser.value, ...data };
184
195
  } catch (error) {
185
196
  throw error;
186
197
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/auth",
3
3
  "type": "module",
4
- "version": "1.1.43",
4
+ "version": "1.1.47",
5
5
  "description": "Bagelink auth package",
6
6
  "author": {
7
7
  "name": "Bagel Studio",
@@ -1,4 +1,4 @@
1
- import type { InternalAxiosRequestConfig } from 'axios'
1
+ import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
2
2
  import type {
3
3
  User,
4
4
  NewUser,
@@ -19,20 +19,15 @@ import type {
19
19
  GetMeResponse,
20
20
  UpdateMeResponse,
21
21
  UpdatePasswordResponse,
22
- SignupResponse
23
- } from '../types'
24
- import axios from 'axios'
22
+ SignupResponse,
23
+ } from './types'
24
+ import { createAxiosInstance } from './utils'
25
25
 
26
26
  export class AuthApi {
27
- private api: ReturnType<typeof axios.create>
28
-
29
- constructor(baseURL?: string) {
30
- this.api = axios.create({
31
- baseURL,
32
- headers: {
33
- 'Content-Type': 'application/json',
34
- },
35
- })
27
+ private api: AxiosInstance
28
+
29
+ constructor(axiosInstance?: AxiosInstance, baseURL: string = '') {
30
+ this.api = axiosInstance || createAxiosInstance(baseURL)
36
31
  this.setupInterceptors()
37
32
  }
38
33
 
@@ -71,7 +66,9 @@ export class AuthApi {
71
66
  return this.api.post('/auth/password-recovery', { email })
72
67
  }
73
68
 
74
- async resetPassword(newPassword: NewPassword['new_password']): Promise<ResetPasswordResponse> {
69
+ async resetPassword(
70
+ newPassword: NewPassword['new_password']
71
+ ): Promise<ResetPasswordResponse> {
75
72
  return this.api.post('/auth/reset-password', { new_password: newPassword })
76
73
  }
77
74
 
@@ -88,7 +85,9 @@ export class AuthApi {
88
85
  })
89
86
  }
90
87
 
91
- async updatePassword(form: UpdatePasswordForm): Promise<UpdatePasswordResponse> {
88
+ async updatePassword(
89
+ form: UpdatePasswordForm
90
+ ): Promise<UpdatePasswordResponse> {
92
91
  return this.api.patch('/users/me/password', {
93
92
  current_password: form.current_password,
94
93
  new_password: form.new_password,
@@ -99,16 +98,26 @@ export class AuthApi {
99
98
  return this.api.patch<SanitizedUserOut>('/users/me', user)
100
99
  }
101
100
 
102
- async setUserStatus(userId: string, isActive: boolean): Promise<UpdateUserResponse> {
103
- return this.api.patch<SanitizedUserOut>(`/users/${userId}`, { is_active: isActive })
101
+ async setUserStatus(
102
+ userId: string,
103
+ isActive: boolean
104
+ ): Promise<UpdateUserResponse> {
105
+ return this.api.patch<SanitizedUserOut>(`/users/${userId}`, {
106
+ is_active: isActive,
107
+ })
104
108
  }
105
109
 
106
110
  async deleteUser(userId: string): Promise<DeleteUserResponse> {
107
111
  return this.api.delete(`/users/${userId}`)
108
112
  }
109
113
 
110
- async getUsers(limit: number = 100, skip?: number): Promise<GetUsersResponse> {
111
- return this.api.get<SanitizedUserList>('/users/', { params: { skip, limit } })
114
+ async getUsers(
115
+ limit: number = 100,
116
+ skip?: number
117
+ ): Promise<GetUsersResponse> {
118
+ return this.api.get<SanitizedUserList>('/users/', {
119
+ params: { skip, limit },
120
+ })
112
121
  }
113
122
 
114
123
  async createUser(user: UserCreate): Promise<CreateUserResponse> {
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './api/auth'
2
- export * from './composable/useAuth'
1
+ export * from './api'
3
2
  export * from './types'
3
+ export * from './useAuth'
package/src/types.ts CHANGED
@@ -26,13 +26,6 @@ export interface UpdatePasswordForm {
26
26
  confirmNewPassword: string
27
27
  }
28
28
 
29
- export interface ReactiveFactory {
30
- <T>(initial: T): {
31
- value: T
32
- set: (newValue: T) => void
33
- }
34
- }
35
-
36
29
  // API Response Types
37
30
  export interface Token {
38
31
  access_token: string
@@ -1,34 +1,37 @@
1
+ import type { AxiosInstance } from 'axios'
1
2
  import type { App } from 'vue'
2
- import type { User, NewUser, UpdatePasswordForm } from '../types'
3
- import { AuthApi } from '../api/auth'
3
+ import type { User, NewUser, UpdatePasswordForm } from './types'
4
+ import { ref } from 'vue'
5
+ import { AuthApi } from './api'
4
6
 
5
7
  // Global state
6
8
  let authApi: AuthApi | null = null
7
- const currentUser = {
8
- value: {
9
- id: '',
10
- email: '',
11
- first_name: '',
12
- last_name: '',
13
- is_superuser: false,
14
- is_active: false,
15
- } as User,
16
- set: (newValue: User) => {
17
- currentUser.value = newValue
18
- }
19
- }
9
+ const currentUser = ref<User>({
10
+ id: '',
11
+ email: '',
12
+ first_name: '',
13
+ last_name: '',
14
+ is_superuser: false,
15
+ is_active: false,
16
+ })
20
17
 
21
18
  // Initialize auth
22
- export function initAuth(baseURL?: string) {
19
+ export function initAuth({
20
+ axios,
21
+ baseURL,
22
+ }: {
23
+ axios: AxiosInstance
24
+ baseURL?: string
25
+ }) {
23
26
  if (!authApi) {
24
- authApi = new AuthApi(baseURL)
27
+ authApi = new AuthApi(axios, baseURL)
25
28
  }
26
29
 
27
30
  return {
28
31
  install(app: App) {
29
32
  // Make auth available globally
30
33
  app.config.globalProperties.$auth = useAuth()
31
- }
34
+ },
32
35
  }
33
36
  }
34
37
 
@@ -53,7 +56,10 @@ export function useAuth() {
53
56
 
54
57
  async function login(credentials: { email: string, password: string }) {
55
58
  try {
56
- await authApi!.login(credentials.email.toLowerCase(), credentials.password)
59
+ await authApi!.login(
60
+ credentials.email.toLowerCase(),
61
+ credentials.password
62
+ )
57
63
  await checkAuth()
58
64
  } catch (error) {
59
65
  throw error
@@ -64,7 +70,7 @@ export function useAuth() {
64
70
  try {
65
71
  if (!getIsLoggedIn()) {
66
72
  const { data } = await authApi!.getCurrentUser()
67
- currentUser.set(data)
73
+ currentUser.value = data
68
74
  }
69
75
  } catch (error) {
70
76
  return false
@@ -78,7 +84,7 @@ export function useAuth() {
78
84
  throw new Error('Passwords do not match')
79
85
  }
80
86
  const { data } = await authApi!.signup(user)
81
- currentUser.set(data)
87
+ currentUser.value = data
82
88
  } catch (error) {
83
89
  throw error
84
90
  }
@@ -114,7 +120,7 @@ export function useAuth() {
114
120
  async function updateProfile(user: Partial<User>) {
115
121
  try {
116
122
  const { data } = await authApi!.updateUserProfile(user)
117
- currentUser.set({ ...currentUser.value, ...data })
123
+ currentUser.value = { ...currentUser.value, ...data }
118
124
  } catch (error) {
119
125
  throw error
120
126
  }
package/src/utils.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { AxiosInstance } from 'axios'
2
+ import axios from 'axios'
3
+
4
+ export function createAxiosInstance(baseURL: string = ''): AxiosInstance {
5
+ return axios.create({
6
+ baseURL: baseURL || '',
7
+ headers: {
8
+ 'Content-Type': 'application/json',
9
+ 'withCredentials': true,
10
+ },
11
+ })
12
+ }