· 6 years ago · Apr 30, 2020, 05:30 AM
1
2/**************************** IN REGISTER API ****************************/
3const QuickBloxUser = await new QuickBlox(Users).createQuickBloxUser(newUser);
4var quickBloxId = QuickBloxUser.data.quickbloxId
5/******************************* UPTO HERE ******************************/
6
7/******************************* SEPERATE FILE CALLED QUICKBLOXCONTRLLER.JS ******************************/
8var crypto = require('crypto');
9time = () => { return Math.floor(new Date().getTime() / 1000) }
10var request = require('request');
11
12
13const CONFIG = {
14 "appId": "",
15 "authKey": "",
16 "authSecret": "",
17};
18
19class QuickBlox extends Controller {
20 constructor() {
21 super();
22 }
23
24 generateQuickBlockToken(userToken, login, password) {
25 let _this = this;
26 return new Promise(async (resolve, reject) => {
27 try {
28 var n = require('nonce')();
29 var nonce = n();
30 var timestamp = time();
31 // console.log(timestamp)
32 var signature_string = "application_id=" + CONFIG.appId + "&auth_key=" + CONFIG.authKey + "&nonce=" + nonce + "×tamp=" + timestamp;
33 if (userToken == true) {
34 // ABOVE IF BLOCK IS USED TO UPDATE THE USERS QUICKBLOX ID (userName in the QuickBlox Database)
35 var signature_string = "application_id=" + CONFIG.appId + "&auth_key=" + CONFIG.authKey + "&nonce=" + nonce + "×tamp=" + timestamp + "&user[login]=" + login + "&user[password]=" + password;
36 }
37 const text = signature_string;
38 const key = CONFIG.authSecret;
39 var hash = crypto.createHmac('sha1', key).update(text).digest('hex');
40 var userData = { "login": login, "password": password };
41 var options = {
42 method: 'POST',
43 url: 'https://api.quickblox.com/session.json',
44 headers:
45 {
46 'cache-control': 'no-cache',
47 'quickblox-rest-api-version': '0.1.0',
48 'content-type': 'application/json',
49 },
50 body:
51 {
52 application_id: CONFIG.appId,
53 auth_key: CONFIG.authKey,
54 timestamp: timestamp,
55 nonce: nonce,
56 signature: hash,
57 },
58 json: true
59 };
60 if (userToken == true) {
61 options.body.user = { "login": login, "password": password }
62 }
63 request(options, async (error, response, body) => {
64 if (error) { console.log("error", error); }
65 return resolve(body.session);
66 });
67 }
68 catch (err) {
69 console.log("Get token", err);
70 return reject({ message: err, status: 0 });
71 }
72 })
73 }
74 async createQuickBloxUser(userData) {
75 return new Promise(async (resolve, reject) => {
76 var password = "Realm6@123"
77 try {
78 var sessionData = await new QuickBlox().generateQuickBlockToken();
79 var token = sessionData.token;
80 var data = {
81 "user": {
82 "login": userData.email,
83 "password": password,
84 "username": userData.fullName,
85 }
86 }
87 var options = {
88 method: 'POST',
89 url: 'https://api.quickblox.com/users.json',
90 headers:
91 {
92 'cache-control': "no-cache",
93 "Content-Type": "application/json",
94 "QuickBlox-REST-API-Version": "0.1.0",
95 "QB-Token": token
96 },
97 body: data,
98 json: true
99 };
100 request(options, async (error, response, body) => {
101 // console.log("response :::: ", response);
102 if (error) {
103 console.log(error);
104 return false;
105 }
106 if (body.errors) {
107 var options1 = {
108 method: 'GET',
109 // url: 'https://api.quickblox.com/users.json',
110 url: 'https://api.quickblox.com/users/by_login.json?login=' + userData.email,
111 headers:
112 {
113 'cache-control': "no-cache",
114 "Content-Type": "application/json",
115 "QuickBlox-REST-API-Version": "0.1.0",
116 "QB-Token": token
117 },
118 body1: data,
119 json: true
120 };
121 request(options1, async (error, response1, body1) => {
122 if (error) {
123 console.log("error in Body Errors ::: ", error)
124 }
125
126 var demoObj = {
127 data: {}
128 };
129 demoObj.data.quickbloxId = body1.user.id;
130 return resolve(demoObj);
131
132 })
133 }
134 if (body.user) {
135 var saveObject = {};
136 saveObject.login = body.user.login;
137 saveObject.password = userData.password;
138 saveObject.fullName = userData.fullName;
139 saveObject.quickbloxId = body.user.id;
140
141 const saveQuickBloxUser = new Users(saveObject);
142 saveQuickBloxUser.save((err, data) => {
143 return resolve({ status: 1, data: saveObject });
144 });
145 }
146 });
147 }
148 catch (err) {
149 console.log("Get token in err block of QBCntrllrs is ::::", err);
150 return reject({ message: err, status: 0 });
151 }
152
153 });
154 }
155}
156
157module.exports = QuickBlox;