@cocreate/users
Advanced tools
Comparing version 1.27.5 to 1.27.6
{ | ||
"name": "@cocreate/users", | ||
"version": "1.27.5", | ||
"version": "1.27.6", | ||
"description": "A simple users component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
/*globals CustomEvent, btoa*/ | ||
import Crud from '@cocreate/crud-client'; | ||
import Action from '@cocreate/actions'; | ||
import Actions from '@cocreate/actions'; | ||
import Elements from '@cocreate/elements'; | ||
import Render from '@cocreate/render'; | ||
import { render } from '@cocreate/render'; | ||
import '@cocreate/element-prototype'; | ||
import './index.css'; | ||
// TODO: Replace with @cocreate/config | ||
import localStorage from '@cocreate/local-storage'; | ||
@@ -25,25 +27,17 @@ | ||
signUp: async function (btn) { | ||
let formEl = btn.closest("form"); | ||
if (!formEl) return; | ||
signUp: async function (action) { | ||
if (!action.form) return; | ||
let array = formEl.getAttribute('array') | ||
if (!array) { | ||
for (let el of formEl) { | ||
array = el.getAttribute('array'); | ||
if (array) | ||
break; | ||
} | ||
} | ||
let data = await Elements.save(action.form) | ||
let user = data[0] | ||
// data.method = 'create.object' | ||
// data['array'] = array | ||
let data = Elements.getFormData(formEl, array) | ||
data.method = 'create.object' | ||
data['array'] = array | ||
data.organization_id = organization_id; | ||
// data.organization_id = organization_id; | ||
if (!data.object[0]._id) | ||
data.object[0]._id = Crud.ObjectId(); | ||
// if (!data.object[0]._id) | ||
// data.object[0]._id = Crud.ObjectId(); | ||
let user = await Crud.send(data) | ||
Elements.setTypeValue(formEl, user) | ||
// let user = await Crud.send(data) | ||
// Elements.setTypeValue(formEl, user) | ||
@@ -53,2 +47,3 @@ // const socket = Crud.socket.getSockets() | ||
let key = { | ||
status: 'await', | ||
method: 'create.object', | ||
@@ -60,7 +55,6 @@ array: 'keys', | ||
roles: ['user'], | ||
email: user.object.email, | ||
password: user.object.password || btoa('0000'), | ||
array | ||
}, | ||
organization_id | ||
email: user.object[0].email, | ||
password: user.object[0].password || btoa('0000'), | ||
array: data.array | ||
} | ||
} | ||
@@ -70,5 +64,5 @@ | ||
if (response && response.object && response.object[0]) { | ||
Crud.socket.send({ method: 'signUp', user, userKey }) | ||
// Crud.socket.send({ method: 'signUp', user, userKey }) | ||
Render.data({ | ||
render({ | ||
selector: "[template='signUp']", | ||
@@ -89,12 +83,10 @@ data: { | ||
signIn: async function (btn) { | ||
let form = btn.closest('form'); | ||
let array = form.getAttribute('array'); | ||
signIn: async function (action) { | ||
if (!action.form) return; | ||
let query = []; | ||
const inputs = form.querySelectorAll('input[key="email"], input[key="password"], input[key="username"]'); | ||
const inputs = action.form.querySelectorAll('input[key="email"], input[key="password"], input[key="username"]'); | ||
for (let i = 0; i < inputs.length; i++) { | ||
const key = inputs[i].getAttribute('key'); | ||
const value = await inputs[i].getValue(); | ||
array = 'keys'; | ||
query.push({ key, value, operator: '$eq' }) | ||
@@ -105,4 +97,3 @@ } | ||
method: 'read.object', | ||
db: 'indexeddb', | ||
array, | ||
array: 'keys', | ||
$filter: { | ||
@@ -113,3 +104,3 @@ query | ||
const socket = Crud.socket.getSockets() | ||
const socket = await Crud.socket.getSockets() | ||
if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine || Crud.socket.serverOrganization == false) { | ||
@@ -139,3 +130,3 @@ Crud.send(request).then((response) => { | ||
signInResponse: function (data) { | ||
let { success, status, message, user_id, token } = data; | ||
let { success, status, message, organization_id, user_id, token } = data; | ||
if (success) { | ||
@@ -147,3 +138,2 @@ localStorage.setItem('organization_id', organization_id); | ||
localStorage.setItem("token", token); | ||
// document.cookie = `token=${token};path=/`; | ||
message = "Succesful signIn"; | ||
@@ -157,3 +147,3 @@ document.dispatchEvent(new CustomEvent('signIn', { | ||
Render.data({ | ||
render({ | ||
selector: "[template='signIn']", | ||
@@ -169,3 +159,3 @@ data: { | ||
signOut: (btn) => { | ||
signOut: () => { | ||
self = this; | ||
@@ -175,9 +165,3 @@ localStorage.removeItem("user_id"); | ||
// let allCookies = document.cookie.split(';'); | ||
// for (var i = 0; i < allCookies.length; i++) | ||
// document.cookie = allCookies[i] + "=;expires=" + | ||
// new Date(0).toUTCString(); | ||
Render.data({ | ||
render({ | ||
selector: "[template='signOut']", | ||
@@ -229,4 +213,2 @@ data: { | ||
localStorage.removeItem("token"); | ||
// this.deleteCookie(); | ||
document.location.href = redirectLink; | ||
@@ -244,4 +226,3 @@ } | ||
Crud.socket.send({ | ||
method: 'sendMessage', | ||
message: 'checkSession', | ||
method: 'checkSession', | ||
broadcast: false, | ||
@@ -261,29 +242,28 @@ broadcastSender: false, | ||
Action.init({ | ||
name: "signUp", | ||
endEvent: "signUp", | ||
callback: (data) => { | ||
CoCreateUser.signUp(data.element); | ||
Actions.init([ | ||
{ | ||
name: "signUp", | ||
endEvent: "signUp", | ||
callback: (action) => { | ||
CoCreateUser.signUp(action); | ||
} | ||
}, | ||
}); | ||
Action.init({ | ||
name: "signIn", | ||
endEvent: "signIn", | ||
callback: (data) => { | ||
CoCreateUser.signIn(data.element); | ||
{ | ||
name: "signIn", | ||
endEvent: "signIn", | ||
callback: (action) => { | ||
CoCreateUser.signIn(action); | ||
} | ||
}, | ||
}); | ||
{ | ||
name: "signOut", | ||
endEvent: "signOut", | ||
callback: (action) => { | ||
CoCreateUser.signOut(action); | ||
} | ||
} | ||
]); | ||
Action.init({ | ||
name: "signOut", | ||
endEvent: "signOut", | ||
callback: (data) => { | ||
CoCreateUser.signOut(data.element); | ||
}, | ||
}); | ||
CoCreateUser.init(); | ||
export default CoCreateUser; |
@@ -17,5 +17,3 @@ class CoCreateUser { | ||
async signUp(data) { | ||
const self = this; | ||
try { | ||
if (data.user) { | ||
@@ -33,6 +31,6 @@ data.user.method = 'create.object' | ||
self.wsManager.send(data); | ||
this.wsManager.send(data); | ||
} catch (error) { | ||
console.log('create.object error', error); | ||
console.log('signup error', error); | ||
} | ||
@@ -56,9 +54,13 @@ } | ||
data.method = 'read.object' | ||
let socket = data.socket | ||
delete data.socket | ||
this.crud.send(data).then(async (data) => { | ||
let response = { | ||
socket, | ||
method: 'signIn', | ||
success: false, | ||
message: "signIn failed", | ||
status: "failed", | ||
userStatus: 'off', | ||
organization_id: data.organization_id, | ||
uid: data.uid | ||
@@ -69,22 +71,12 @@ } | ||
const user_id = data.object[0].key | ||
const token = await self.wsManager.authenticate.generateToken({ user_id }); | ||
const token = await self.wsManager.authenticate.encodeToken({ user_id }); | ||
if (token && token != 'null') { | ||
response = { | ||
success: true, | ||
message: "signIn successful", | ||
status: "success", | ||
userStatus: 'on', | ||
user_id, | ||
token, | ||
uid: data.uid | ||
}; | ||
// if (data.organization_id != process.env.organization_id) { | ||
// let Data = { organization_id: process.env.organization_id } | ||
// Data.object['_id'] = data.object[0]._id | ||
// Data.object['lastsignIn'] = data.object[0].lastsignIn | ||
// Data.object['organization_id'] = process.env.organization_id | ||
// crud.send(Data) | ||
// } | ||
socket.user_id = user_id | ||
response.success = true | ||
response.message = "signIn successful" | ||
response.userStatus = 'on' | ||
response.user_id = user_id | ||
response.token = token | ||
response.userStatus = 'on' | ||
} | ||
@@ -94,2 +86,3 @@ } | ||
self.wsManager.send({ | ||
socket, | ||
method: 'updateUserStatus', | ||
@@ -114,20 +107,30 @@ user_id: response.user_id, | ||
try { | ||
if (!data.user_id || !data.userStatus) | ||
return | ||
data.array = 'users' | ||
data['object'] = { | ||
_id: data.user_id, | ||
userStatus: data.userStatus | ||
} | ||
if (data.user_id && data.userStatus) { | ||
data.array = 'users' | ||
data['object'] = { | ||
_id: data.user_id, | ||
userStatus: data.userStatus | ||
} | ||
data.method = 'update.object' | ||
this.crud.send(data).then((data) => { | ||
self.wsManager.send({ | ||
data.method = 'update.object' | ||
data = await this.crud.send(data) | ||
if (data.socket) | ||
self.wsManager.send({ | ||
socket: data.socket, | ||
method: 'updateUserStatus', | ||
user_id: data.user_id, | ||
userStatus: data.userStatus, | ||
token: data.token, | ||
organization_id: data.organization_id || socket.organization_id | ||
}) | ||
} else if (data.socket) | ||
data.socket.send(JSON.stringify({ | ||
method: 'updateUserStatus', | ||
user_id: data.user_id, | ||
userStatus: data.userStatus, | ||
token: data.token, | ||
organization_id: data.organization_id || socket.organization_id | ||
}) | ||
})) | ||
}) | ||
@@ -134,0 +137,0 @@ } catch (error) { |
Sorry, the diff of this file is too big to display
148198
484