-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspreadsheet.js
73 lines (55 loc) · 1.83 KB
/
spreadsheet.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
const { GoogleSpreadsheet } = require('google-spreadsheet');
const bcrypt = require('bcrypt');
require('dotenv').config();
const spreadsheetId = process.env.SHEET_ID;
//user input data
const emailId = '[email protected]';
const password = 'hashthispassword';
executeMain(spreadsheetId, emailId, password);
async function executeMain(spreadsheetId, emailId, password) {
try {
// Initialize spreadsheet
const doc = await initSpreadsheet(spreadsheetId);
//select a sheet
const sheet = doc.sheetsByTitle['login_credentials'];
// hash password
const rounds = 10;
const hashedpassowrd = await hashPassword(password, rounds);
await addData(sheet, emailId, hashedpassowrd)
} catch (error) {
console.log(error)
}
}
async function initSpreadsheet(docId) {
try {
// Initialize the sheet - doc ID is the long id in the sheets URL
const document = new GoogleSpreadsheet(docId);
// Initialize Auth - see more available options at https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
await document.useServiceAccountAuth({
client_email: process.env.SHEET_EMAIL,
private_key: process.env.SHEET_API_KEY.replace(/\\n/gm, '\n')
});
await document.loadInfo(); // loads document properties and worksheets
return document
} catch (error) {
console.log('sheet initialization error: \n', error)
}
}
async function addData(sheet, emailId, password) {
try {
const response = await sheet.addRow({ Email: emailId, Password: password });
return response;
} catch (error) {
console.log('add data error: \n', error)
}
}
async function hashPassword(password, rounds) {
try {
const hash = await bcrypt.hash(password, rounds);
return hash;
// compare hash and password
// console.log(await bcrypt.compare(password, 'hash'));
} catch (error) {
console.log('password hashing error', error)
}
}