/
index.js
65 lines (53 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { inject, ref, readonly } from 'vue'
import { AuthStatusCode, SimpleOTP } from '@simpleotp/core'
const SIMPLEOTP_PLUGIN_KEY = 'simpleotp'
class VueSimpleOTP extends SimpleOTP {
constructor(siteID, apiURL=null) {
super(siteID, apiURL)
this._userRef = ref(super.getUser())
this._isAuthenticatedRef = ref(Boolean(this._userRef.value))
}
_setAuthDetailsIfSuccessful(resp) {
if (resp.code === AuthStatusCode.OK.description) {
this._isAuthenticatedRef.value = true
this._userRef.value = this.getUser()
}
}
async auth(code) {
const resp = await super.auth(code)
this._setAuthDetailsIfSuccessful(resp)
return resp
}
async authWithWebAuthnCredentials(email) {
const resp = await super.authWithWebAuthnCredentials(email)
this._setAuthDetailsIfSuccessful(resp)
return resp
}
async authWithURLCode() {
const resp = await super.authWithURLCode()
this._setAuthDetailsIfSuccessful(resp)
return resp
}
isAuthenticatedRef() {
return readonly(this._isAuthenticatedRef)
}
getUserRef() {
return readonly(this._userRef)
}
signOut() {
super.signOut()
this._isAuthenticatedRef.value = false
this._userRef.value = null
}
}
/**
* @returns {VueSimpleOTP}
*/
export function useSimpleOTP() {
return inject(SIMPLEOTP_PLUGIN_KEY)
}
export default {
install(app, options) {
app.provide(SIMPLEOTP_PLUGIN_KEY, new VueSimpleOTP(options.siteID, options.apiURL))
}
}