Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haiyen-payment
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Giang Tran
haiyen-payment
Commits
1011a015
Commit
1011a015
authored
Aug 31, 2026
by
tdgiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add SobanleClient token cache and login
parent
60aa61c5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
0 deletions
+39
-0
SobanleClient.js
app/libs/SobanleClient.js
+39
-0
No files found.
app/libs/SobanleClient.js
0 → 100644
View file @
1011a015
"use strict"
;
var
config
=
require
(
__config_path
+
"/config"
);
var
ApiRequest
=
require
(
"./ApiRequest"
);
var
tokenCache
=
{
token
:
null
,
expiresAt
:
0
};
function
login
(
callback
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/login"
;
ApiRequest
.
postOtherUrl
(
url
,
{
login
:
config
.
sobanle
.
username
,
password
:
config
.
sobanle
.
password
},
function
(
err
,
body
)
{
if
(
err
)
{
return
callback
(
err
);
}
if
(
!
body
||
!
body
.
access_token
)
{
return
callback
(
new
Error
(
"SOBANLE_LOGIN_FAILED"
));
}
tokenCache
.
token
=
body
.
access_token
;
var
expiresInMs
=
(
body
.
expires_in
||
0
)
*
1000
;
// Refresh a bit early to avoid racing a near-expiry token. Headroom scales
// with the token's own TTL (capped at 60s) rather than a flat 60s, so a
// short-TTL token isn't already "expired" the instant it's cached.
var
earlyRefreshMs
=
Math
.
min
(
60000
,
expiresInMs
*
0.1
);
tokenCache
.
expiresAt
=
Date
.
now
()
+
expiresInMs
-
earlyRefreshMs
;
callback
(
null
,
body
.
access_token
);
});
}
exports
.
login
=
login
;
function
getToken
(
callback
)
{
if
(
tokenCache
.
token
&&
Date
.
now
()
<
tokenCache
.
expiresAt
)
{
return
callback
(
null
,
tokenCache
.
token
);
}
exports
.
login
(
callback
);
}
exports
.
getToken
=
getToken
;
exports
.
_resetTokenCacheForTest
=
function
()
{
tokenCache
.
token
=
null
;
tokenCache
.
expiresAt
=
0
;
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment