-
Notifications
You must be signed in to change notification settings - Fork 1
/
hive.js
103 lines (88 loc) · 3.5 KB
/
hive.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
const { Client } = require('hivesigner');
const session = require('express-session');
const {handlenewUser} = require('./user');
const client = new Client({
app: 'ag8350961', // Replace 'your-app-name' with your actual app name
callbackURL: 'https://intelli-hire.vercel.app/', // Replace with your callback URL
accessToken: '',
scope: ['custom_json', 'comment', 'vote']
});
async function login(req,res){
const loginURL = client.getLoginURL(); // Get the login URL
console.log(loginURL)
res.json({
url: loginURL
}); // Redirect the user to the loginURL
}
async function logout(req,res){
console.log("logout")
client.revokeToken(function (err, res) {
console.log(err, res)
});
res.send("logout sucessfully")
}
async function callback(req,res){
const { access_token, username } = req.query; // Get the authorization code from the query parameters
client.setAccessToken(access_token)
try {
// Exchange the authorization code for an access token
// Store the access token in session (you can store it in a database as well)
req.session.accessToken = access_token;
req.session.username = username;
// console.log(req.session.accessToken, req.session.username)
client.me()
.then((res)=>{
console.log(res)
handlenewUser(username, res.user_metadata.profile.profile_image)
});
// Redirect the user to a logged-in page or perform other actions
// res.send(req.sessionID);
res.send("welcome to intellihire");
} catch (error) {
// Handle errors appropriately
console.error('Error during login:', error);
res.status(500).send('Error during login');
}
}
async function submitCustomJson(req,res){
// Check if the user is logged in (e.g., by checking for the presence of the access token)
// if (!req.session.accessToken) {
// console.log("not logged in")
// return res.redirect('/login');
// }
const {token, username} = req.query;
if(!token){
return res.send("not logged in");
}
// client.me(function (err, res) {
// console.log(err, res)
// });
// Check if the account has the required authority
// const accountInfo = await client.me();
// console.log(accountInfo.account.auths)
// const hasAuthority = accountInfo.account.auths.some(auth => auth[0] === 'custom_json');
// if (!hasAuthority) {
// throw new Error('Account does not have required authority to perform custom JSON operations');
// }
console.log("custom json")
const requiredAuths = []; // Accounts required to authorize the operation
const requiredPostingAuths = [username]; // Accounts required to post the operation
const id = 'this is first to go'; // Unique identifier for the operation
const data = { message: 'Hello, Hive!' }; // JSON data to submit to the blockchain
const json = JSON.stringify(data);
let tid = 'ghis';
await client.customJson(requiredAuths, requiredPostingAuths, id, json, function (err, res) {
if (err) {
console.error('Error broadcasting custom JSON operation:', err, req.session.username);
} else {
console.log('Custom JSON operation broadcasted successfully:', res.result.id);
tid = res.result.id;
}
});
// client.vote("ag8350961", "david-doran", "vibes-web3-music-contest-week", 10000, function (err, res) {
// console.log(err, res)
// });
console.log(tid)
res.send(tid);
}
module.exports = {login, logout, callback, submitCustomJson};