@bagelink/auth 1.4.176 → 1.4.180
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/README.md +11 -8
- package/dist/index.cjs +461 -34
- package/dist/index.d.cts +291 -8
- package/dist/index.d.mts +291 -8
- package/dist/index.d.ts +291 -8
- package/dist/index.mjs +450 -35
- package/package.json +1 -1
- package/src/api.ts +54 -36
- package/src/index.ts +1 -0
- package/src/sso.ts +565 -0
- package/src/types.ts +33 -2
- package/src/useAuth.ts +87 -5
- package/src/utils.ts +3 -3
package/src/api.ts
CHANGED
|
@@ -29,31 +29,33 @@ import type {
|
|
|
29
29
|
DeleteAllSessionsResponse,
|
|
30
30
|
CleanupSessionsResponse,
|
|
31
31
|
GetMethodsResponse,
|
|
32
|
+
SSOProvider,
|
|
33
|
+
SSOInitiateRequest,
|
|
34
|
+
SSOCallbackRequest,
|
|
35
|
+
SSOLinkRequest,
|
|
36
|
+
SSOInitiateResponse,
|
|
37
|
+
SSOCallbackResponse,
|
|
38
|
+
SSOLinkResponse,
|
|
39
|
+
SSOUnlinkResponse,
|
|
32
40
|
} from './types'
|
|
33
41
|
import { createAxiosInstance } from './utils'
|
|
34
42
|
|
|
35
43
|
export class AuthApi {
|
|
36
44
|
private api: AxiosInstance
|
|
37
45
|
|
|
38
|
-
constructor(
|
|
39
|
-
this.api =
|
|
46
|
+
constructor(baseURL: string = '') {
|
|
47
|
+
this.api = createAxiosInstance(baseURL)
|
|
40
48
|
this.setupInterceptors()
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
private setupInterceptors() {
|
|
44
52
|
this.api.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
45
|
-
const sessionToken = localStorage.getItem('session_token')
|
|
46
|
-
if (sessionToken !== null && config.headers) {
|
|
47
|
-
config.headers.Authorization = `Bearer ${sessionToken}`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
53
|
// Handle password reset token from URL
|
|
51
54
|
const urlParams = new URLSearchParams(window.location.search)
|
|
52
55
|
const resetToken = urlParams.get('token')
|
|
53
56
|
if (resetToken !== null && config.headers) {
|
|
54
57
|
config.headers['X-Reset-Token'] = resetToken
|
|
55
58
|
}
|
|
56
|
-
|
|
57
59
|
return config
|
|
58
60
|
})
|
|
59
61
|
}
|
|
@@ -73,57 +75,75 @@ export class AuthApi {
|
|
|
73
75
|
* Register a new account
|
|
74
76
|
*/
|
|
75
77
|
async register(data: RegisterRequest): Promise<RegisterResponse> {
|
|
76
|
-
|
|
78
|
+
return this.api.post('/authentication/register', {
|
|
77
79
|
...data,
|
|
78
80
|
email: data.email.toLowerCase(),
|
|
79
81
|
})
|
|
80
|
-
|
|
81
|
-
// Store session token if provided
|
|
82
|
-
if (response.data.session_token) {
|
|
83
|
-
localStorage.setItem('session_token', response.data.session_token)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return response
|
|
87
82
|
}
|
|
88
83
|
|
|
89
84
|
/**
|
|
90
85
|
* Login with password
|
|
91
86
|
*/
|
|
92
87
|
async login(email: string, password: string): Promise<LoginResponse> {
|
|
93
|
-
|
|
88
|
+
return this.api.post('/authentication/login/password', {
|
|
94
89
|
email: email.toLowerCase(),
|
|
95
90
|
password,
|
|
96
91
|
})
|
|
97
|
-
|
|
98
|
-
// Store session token if provided
|
|
99
|
-
if (response.data.session_token) {
|
|
100
|
-
localStorage.setItem('session_token', response.data.session_token)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return response
|
|
104
92
|
}
|
|
105
93
|
|
|
106
94
|
/**
|
|
107
95
|
* Logout and clear session
|
|
108
96
|
*/
|
|
109
97
|
async logout(): Promise<LogoutResponse> {
|
|
110
|
-
|
|
111
|
-
localStorage.removeItem('session_token')
|
|
112
|
-
return response
|
|
98
|
+
return this.api.post('/authentication/logout', {})
|
|
113
99
|
}
|
|
114
100
|
|
|
115
101
|
/**
|
|
116
102
|
* Refresh current session
|
|
117
103
|
*/
|
|
118
104
|
async refreshSession(): Promise<RefreshSessionResponse> {
|
|
119
|
-
|
|
105
|
+
return this.api.post('/authentication/refresh', {})
|
|
106
|
+
}
|
|
120
107
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
108
|
+
// ============================================
|
|
109
|
+
// SSO Authentication Methods
|
|
110
|
+
// ============================================
|
|
125
111
|
|
|
126
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Initiate SSO login flow
|
|
114
|
+
* Returns authorization URL to redirect user to
|
|
115
|
+
*/
|
|
116
|
+
async initiateSSO(data: SSOInitiateRequest): Promise<SSOInitiateResponse> {
|
|
117
|
+
return this.api.post(`/authentication/sso/${data.provider}/initiate`, {
|
|
118
|
+
redirect_uri: data.redirect_uri,
|
|
119
|
+
state: data.state,
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Complete SSO login after callback from provider
|
|
125
|
+
*/
|
|
126
|
+
async ssoCallback(data: SSOCallbackRequest): Promise<SSOCallbackResponse> {
|
|
127
|
+
return this.api.post(`/authentication/sso/${data.provider}/callback`, {
|
|
128
|
+
code: data.code,
|
|
129
|
+
state: data.state,
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Link an SSO provider to existing account
|
|
135
|
+
*/
|
|
136
|
+
async linkSSOProvider(data: SSOLinkRequest): Promise<SSOLinkResponse> {
|
|
137
|
+
return this.api.post(`/authentication/sso/${data.provider}/link`, {
|
|
138
|
+
code: data.code,
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Unlink an SSO provider from account
|
|
144
|
+
*/
|
|
145
|
+
async unlinkSSOProvider(provider: SSOProvider): Promise<SSOUnlinkResponse> {
|
|
146
|
+
return this.api.delete(`/authentication/sso/${provider}/unlink`)
|
|
127
147
|
}
|
|
128
148
|
|
|
129
149
|
// ============================================
|
|
@@ -148,9 +168,7 @@ export class AuthApi {
|
|
|
148
168
|
* Delete current user account
|
|
149
169
|
*/
|
|
150
170
|
async deleteCurrentUser(): Promise<DeleteMeResponse> {
|
|
151
|
-
|
|
152
|
-
localStorage.removeItem('session_token')
|
|
153
|
-
return response
|
|
171
|
+
return this.api.delete('/authentication/me')
|
|
154
172
|
}
|
|
155
173
|
|
|
156
174
|
// ============================================
|