-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverFunctionWithSqlite.js
More file actions
52 lines (48 loc) · 1.78 KB
/
Copy pathserverFunctionWithSqlite.js
File metadata and controls
52 lines (48 loc) · 1.78 KB
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
let db = require("./sqlite_conn");
function refreshToken( { account, ser_token, sdk_token } ) {
account = parseInt( account, 10 );
return new Promise( ( resolve, reject ) => {
getToken( account ).then( data => {
if ( !data || !data.account || data.account !== account ) {
db.run( "INSERT INTO tokens ( account, ser_token, sdk_token ) VALUES (?,?,?)", [ account, ser_token, sdk_token ],
function ( err, row ) {
if ( !err ) {
resolve( row );//undefined
} else{
reject( err );
}
} );
} else {
if ( ser_token === data.ser_token && sdk_token === data.sdk_token ) {
resolve( true );
} else {
db.run( "UPDATE tokens SET ser_token = ?, sdk_token = ? where account = ?;", [ ser_token, sdk_token, account ],
function ( err, row ) {
if ( !err ) {
resolve( row );//undefined
} else {
reject( err );
}
} );
}
}
} ).catch( e => {
reject( e );
} );
} );
}
function getToken( account ) {
return new Promise( ( resolve, reject ) => {
db.get( "SELECT * FROM tokens WHERE account = ?;", parseInt( account, 10 ), function ( err, row ) {
if ( !err ) {
resolve( row );
} else {
console.log( err );
reject( err );
}
} )
} )
}
module.exports = {
refreshToken, getToken
}