@bagelink/auth 1.4.178 → 1.4.182
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 +297 -8
- package/dist/index.d.mts +297 -8
- package/dist/index.d.ts +297 -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 +35 -2
- package/src/useAuth.ts +87 -5
- package/src/utils.ts +3 -3
package/src/useAuth.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { AxiosInstance } from 'axios'
|
|
2
1
|
import type { App } from 'vue'
|
|
3
2
|
import type {
|
|
4
3
|
AccountInfo,
|
|
@@ -7,9 +6,14 @@ import type {
|
|
|
7
6
|
UpdatePasswordForm,
|
|
8
7
|
UpdateAccountRequest,
|
|
9
8
|
AuthEventMap,
|
|
9
|
+
SSOProvider,
|
|
10
|
+
SSOInitiateRequest,
|
|
11
|
+
SSOCallbackRequest,
|
|
12
|
+
SSOLinkRequest,
|
|
10
13
|
} from './types'
|
|
11
14
|
import { ref, computed } from 'vue'
|
|
12
15
|
import { AuthApi } from './api'
|
|
16
|
+
import { setAuthContext, sso } from './sso'
|
|
13
17
|
import { AuthState, accountToUser } from './types'
|
|
14
18
|
import { EventEmitter } from './utils'
|
|
15
19
|
|
|
@@ -20,14 +24,12 @@ const accountInfo = ref<AccountInfo | null>(null)
|
|
|
20
24
|
|
|
21
25
|
// Initialize auth
|
|
22
26
|
export function initAuth({
|
|
23
|
-
axios,
|
|
24
27
|
baseURL,
|
|
25
28
|
}: {
|
|
26
|
-
|
|
27
|
-
baseURL?: string
|
|
29
|
+
baseURL: string
|
|
28
30
|
}) {
|
|
29
31
|
if (authApi === null) {
|
|
30
|
-
authApi = new AuthApi(
|
|
32
|
+
authApi = new AuthApi(baseURL)
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
if (eventEmitter === null) {
|
|
@@ -74,6 +76,31 @@ export function useAuth() {
|
|
|
74
76
|
const api = authApi
|
|
75
77
|
const emitter = eventEmitter
|
|
76
78
|
|
|
79
|
+
// Set SSO auth context (allows sso.google.redirect() etc. to work)
|
|
80
|
+
const authMethods = {
|
|
81
|
+
initiateSSO: async (params: SSOInitiateRequest) => {
|
|
82
|
+
const { data } = await api.initiateSSO(params)
|
|
83
|
+
return data.authorization_url
|
|
84
|
+
},
|
|
85
|
+
loginWithSSO: async (params: SSOCallbackRequest) => {
|
|
86
|
+
const { data } = await api.ssoCallback(params)
|
|
87
|
+
if (data.success === true && data.requires_verification !== true) {
|
|
88
|
+
await checkAuth()
|
|
89
|
+
}
|
|
90
|
+
emitter.emit(AuthState.LOGIN)
|
|
91
|
+
return data
|
|
92
|
+
},
|
|
93
|
+
linkSSOProvider: async (params: SSOLinkRequest) => {
|
|
94
|
+
await api.linkSSOProvider(params)
|
|
95
|
+
await checkAuth()
|
|
96
|
+
},
|
|
97
|
+
unlinkSSOProvider: async (provider: SSOProvider) => {
|
|
98
|
+
await api.unlinkSSOProvider(provider)
|
|
99
|
+
await checkAuth()
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
setAuthContext(authMethods)
|
|
103
|
+
|
|
77
104
|
// Computed user - the primary interface for accessing user data
|
|
78
105
|
const user = computed<User | null>(() => accountToUser(accountInfo.value))
|
|
79
106
|
|
|
@@ -255,6 +282,52 @@ export function useAuth() {
|
|
|
255
282
|
await api.revokeAllSessions(id)
|
|
256
283
|
}
|
|
257
284
|
|
|
285
|
+
// ============================================
|
|
286
|
+
// SSO Authentication Methods
|
|
287
|
+
// ============================================
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Initiate SSO login flow
|
|
291
|
+
* @returns Authorization URL to redirect user to
|
|
292
|
+
*/
|
|
293
|
+
async function initiateSSO(params: SSOInitiateRequest) {
|
|
294
|
+
const { data } = await api.initiateSSO(params)
|
|
295
|
+
return data.authorization_url
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Complete SSO login after callback from provider
|
|
300
|
+
*/
|
|
301
|
+
async function loginWithSSO(params: SSOCallbackRequest) {
|
|
302
|
+
const { data } = await api.ssoCallback(params)
|
|
303
|
+
|
|
304
|
+
// If successful and not requiring verification, fetch user data
|
|
305
|
+
if (data.success === true && data.requires_verification !== true) {
|
|
306
|
+
await checkAuth()
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
emitter.emit(AuthState.LOGIN)
|
|
310
|
+
return data
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Link an SSO provider to the current account
|
|
315
|
+
*/
|
|
316
|
+
async function linkSSOProvider(params: SSOLinkRequest) {
|
|
317
|
+
await api.linkSSOProvider(params)
|
|
318
|
+
// Refresh user data to get updated authentication methods
|
|
319
|
+
await checkAuth()
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Unlink an SSO provider from the current account
|
|
324
|
+
*/
|
|
325
|
+
async function unlinkSSOProvider(provider: SSOProvider) {
|
|
326
|
+
await api.unlinkSSOProvider(provider)
|
|
327
|
+
// Refresh user data to get updated authentication methods
|
|
328
|
+
await checkAuth()
|
|
329
|
+
}
|
|
330
|
+
|
|
258
331
|
return {
|
|
259
332
|
// Primary State (use this!)
|
|
260
333
|
user,
|
|
@@ -262,6 +335,9 @@ export function useAuth() {
|
|
|
262
335
|
// Full account info (for advanced use cases)
|
|
263
336
|
accountInfo,
|
|
264
337
|
|
|
338
|
+
// SSO Providers (ready to use!)
|
|
339
|
+
sso,
|
|
340
|
+
|
|
265
341
|
// Getters
|
|
266
342
|
getFullName,
|
|
267
343
|
getIsLoggedIn,
|
|
@@ -278,6 +354,12 @@ export function useAuth() {
|
|
|
278
354
|
checkAuth,
|
|
279
355
|
refreshSession,
|
|
280
356
|
|
|
357
|
+
// SSO Authentication (lower-level - prefer using sso.google.redirect())
|
|
358
|
+
initiateSSO,
|
|
359
|
+
loginWithSSO,
|
|
360
|
+
linkSSOProvider,
|
|
361
|
+
unlinkSSOProvider,
|
|
362
|
+
|
|
281
363
|
// Profile Actions
|
|
282
364
|
updateProfile,
|
|
283
365
|
deleteCurrentUser,
|
package/src/utils.ts
CHANGED
|
@@ -2,12 +2,12 @@ import type { AxiosInstance } from 'axios'
|
|
|
2
2
|
import type { AuthEventMap, AuthState } from './types'
|
|
3
3
|
import axios from 'axios'
|
|
4
4
|
|
|
5
|
-
export function createAxiosInstance(baseURL: string
|
|
5
|
+
export function createAxiosInstance(baseURL: string): AxiosInstance {
|
|
6
6
|
return axios.create({
|
|
7
|
-
baseURL
|
|
7
|
+
baseURL,
|
|
8
|
+
withCredentials: true,
|
|
8
9
|
headers: {
|
|
9
10
|
'Content-Type': 'application/json',
|
|
10
|
-
'withCredentials': true,
|
|
11
11
|
},
|
|
12
12
|
})
|
|
13
13
|
}
|