This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-cognito.js
180 lines (155 loc) · 4.76 KB
/
vue-cognito.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Config, CognitoIdentityCredentials } from 'aws-sdk/global'
import { CognitoUserPool, CognitoUser, AuthenticationDetails, CognitoUserAttribute } from 'amazon-cognito-identity-js'
export default class VueCognito {
constructor (options) {
this.apps = []
this.options = options
this.userSession = null
this.userPool = new CognitoUserPool({
UserPoolId: options.UserPoolId,
ClientId: options.ClientId
})
Config.region = options.region
Config.credentials = new CognitoIdentityCredentials({
IdentityPoolId: options.IdentityPoolId
})
}
isAuthenticated () {
return new Promise((resolve, reject) => {
const cognitoUser = this.getCurrentUser()
if (cognitoUser != null) {
cognitoUser.getSession((err, session) => {
if (err) return reject(err)
return resolve(true)
})
}
return resolve(false)
})
}
register (username, email, password) {
return new Promise((resolve, reject) => {
const attributes = [
new CognitoUserAttribute({
Name: 'email',
Value: email
})
]
this.userPool.signUp(username, password, attributes, null, (err, result) => {
if (err) return reject(err)
return resolve(result)
})
})
}
confirmRegistration (username, code) {
return new Promise((resolve, reject) => {
const cognitoUser = new CognitoUser({
Username: username,
Pool: this.userPool
})
cognitoUser.confirmRegistration(code, true, (err, result) => {
if (err) return reject(err)
return resolve(result)
})
})
}
completeNewPassword (newPassword, data) {
return new Promise((resolve, reject) => {
const cognitoUser = new CognitoUser({
Username: data.attributes.username,
Pool: new CognitoUserPool({
UserPoolId: this.options.UserPoolId,
ClientId: this.options.ClientId
})
})
cognitoUser.Session = data.session
delete data.attributes.email
delete data.attributes.username
cognitoUser.completeNewPasswordChallenge(newPassword, data.attributes, {
onSuccess: result => {
return resolve(result)
},
onFailure: err => {
return reject(err)
}
})
})
}
resendCode (username, code) {
return new Promise((resolve, reject) => {
const cognitoUser = new CognitoUser({
Username: username,
Pool: this.userPool
})
cognitoUser.resendConfirmationCode((err, result) => {
if (err) return reject(err)
return resolve(result)
})
})
}
login (username, password) {
return new Promise((resolve, reject) => {
const authenticationDetails = new AuthenticationDetails({
Username: username,
Password: password
})
const cognitoUser = new CognitoUser({
Username: username,
Pool: this.userPool
})
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: result => {
let logins = {}
logins['cognito-idp.' + this.options.region + '.amazonaws.com/' + this.options.UserPoolId] = result.getIdToken().getJwtToken()
Config.credentials = new CognitoIdentityCredentials({
IdentityPoolId: this.options.UserPoolId,
Logins: logins
})
this.onChange(true)
return resolve({newPasswordRequired: false, ...result})
},
onFailure: err => {
return reject(err)
},
newPasswordRequired: (userAttributes, requiredAttributes) => {
userAttributes.username = cognitoUser.username
delete userAttributes.email_verified
return resolve({newPasswordRequired: true, attributes: userAttributes, session: cognitoUser.Session})
}
})
})
}
logout () {
this.getCurrentUser().signOut()
this.onChange(false)
}
getIdToken () {
return new Promise((resolve, reject) => {
if (this.userPool.getCurrentUser() == null) return reject(new Error('No user is currently logged in.'))
this.userPool.getCurrentUser().getSession((err, session) => {
if (err) return reject(err)
if (session.isValid()) return resolve(session.getIdToken().getJwtToken())
return reject(new Error('Session is invalid'))
})
})
}
getCurrentUser () {
return this.userPool.getCurrentUser()
}
init (app) {
this.apps.push(app)
}
onChange () {}
}
VueCognito.install = (Vue, options) => {
Object.defineProperty(Vue.prototype, '$cognito', {
get () { return this.$root._vueCognito }
})
Vue.mixin({
beforeCreate () {
if (this.$options.cognito) {
this._vueCognito = this.$options.cognito
this._vueCognito.init(this)
}
}
})
}