I'm trying to embed a Javascript function into my xbasic, while still maintaining the xbasic functions. I have this defined in Xbasic for persistent login:
--------------------------------------------------------------------------------------------------------------------------------------------------
function xbLogin as c (e as p)
'this is the Xbasic function that handles login. When the user clicks the 'login' button
'this function is called. If the user has supplied valid credentials and can be logged into the
'Alpha Anywhere security framework, a JWT is created and sent back to the client where it is stored in
'localStorage.
'The JWT is sent back to the server on all subsequent Ajax callbacks. If the user's session has expired
'and the user is therefore no longer logged in, the JWT is decoded and the user is automatically logged
'in to the Alpha Anywhere security framework using the username/password in the decoded JWT.
dim jsOut as c
dim logMessages as c = ""
dim username as c
dim password as c
username = e.datasubmitted.username
password = e.datasubmitted.password
'try to login programmatically using
dim pResult as p
pResult = a5ws_Login_User(userName,password)
if pResult.error then
'if login fails, display an error message in the 'LOGIN_ERRORS' placeholder.
logMessages = "console.log('Login FAILED.');" + crlf()
dim js as c
dim message as c = "Login failed."
if e.tmpl.embeddedMode = .t. then
message = message + crlf() + "Login not supported in Working Preview mode."
else
message = pResult.error_text
end if
message = stritran(message,crlf(),"<br>")
message = "<span style="color:red;">" + message + "</span>"
js = "var ele = {dialog.object}.getPointer('LOGIN_ERRORS'); ele.innerHTML = '"+js_escape(message)+"';"
jsOut = logMessages + js + "alert('Login failed. Please login again.'); {dialog.object}.panelSetActive('PANEL_LOGIN');"
xbLogin = jsOUt
exit function
end if
'If you get here, login has succeeded.
'generate a JWT
dim options.algorithm as c = "HS512" 'specify the encryption algorithm for the JWT. See www.documention.alphasoftware.com for info
dim options.expiresin as n = 60000 'token is set to expire after 1,000 minutes
dim optionsjson as c = json_generate(options)
delete obj
dim obj as p
obj.userName = userName
obj.password = password
dim json as c
json = json_generate(obj)
dim token as c = extension::JSON::JWTSign(json,"shhhh!",optionsjson)
'Create the Javascript response to send to the client. The Javascript must:
'set focus to the application Panel (PANEL_APPLICATION)
'stores the JWT in localStorage
'store the JWT in the UX state object so that the JWT is submitted to the server on every Ajax callback
'NOTE: e._state.token gets translated into Javascript to set the token value in the UX state object.
logMessages = "console.log('New token:"+js_escape(token)+" generated for user: "+js_escape(userName)+"');" + crlf()
jsOut= logMessages + "{dialog.object}.panelSetActive('PANEL_APPLICATION');"
jsOut = jsOut + "localStorage.setItem('" + e.tmpl.guid + "','" + js_escape(token) + "');"
jsOut = jsOUt + "var ele = {dialog.object}.getPointer('LOGIN_ERRORS'); ele.innerHTML = '';"
xbLogin = jsOut
e._state.token = token
end function
--------------------------------------------------------------------------------------------------------------------------------------------------
I've created a separate javascript function as follows:
function control() {
var cbObj = {dialog.object}.getControl('CONTROLBAR_1');
if(cbObj) cbObj.setLayout('Layout2');
}
This javascript is to change the control bar to a different layout once login is complete. What I am trying to do is figure out how to embed this into the xbasic.
If at the end of my xbasic function, I set xblogin = control(), that triggers my javascript to change the control bar layout, but it ignores everything else in the xblogin function. How do I rewrite the xblogin function so that it does the login and the javascript together?
I have also tried adding this to my xblogin function:
dim control as c
control = control()
But that doesn't seem to fire the javaqscript. I'm sure this is easy to someone out there, I am just a newb when it comes to xbasic. Also, is there any xbasic training anywhere? I would love to study up on it.
Thanks.
--------------------------------------------------------------------------------------------------------------------------------------------------
function xbLogin as c (e as p)
'this is the Xbasic function that handles login. When the user clicks the 'login' button
'this function is called. If the user has supplied valid credentials and can be logged into the
'Alpha Anywhere security framework, a JWT is created and sent back to the client where it is stored in
'localStorage.
'The JWT is sent back to the server on all subsequent Ajax callbacks. If the user's session has expired
'and the user is therefore no longer logged in, the JWT is decoded and the user is automatically logged
'in to the Alpha Anywhere security framework using the username/password in the decoded JWT.
dim jsOut as c
dim logMessages as c = ""
dim username as c
dim password as c
username = e.datasubmitted.username
password = e.datasubmitted.password
'try to login programmatically using
dim pResult as p
pResult = a5ws_Login_User(userName,password)
if pResult.error then
'if login fails, display an error message in the 'LOGIN_ERRORS' placeholder.
logMessages = "console.log('Login FAILED.');" + crlf()
dim js as c
dim message as c = "Login failed."
if e.tmpl.embeddedMode = .t. then
message = message + crlf() + "Login not supported in Working Preview mode."
else
message = pResult.error_text
end if
message = stritran(message,crlf(),"<br>")
message = "<span style="color:red;">" + message + "</span>"
js = "var ele = {dialog.object}.getPointer('LOGIN_ERRORS'); ele.innerHTML = '"+js_escape(message)+"';"
jsOut = logMessages + js + "alert('Login failed. Please login again.'); {dialog.object}.panelSetActive('PANEL_LOGIN');"
xbLogin = jsOUt
exit function
end if
'If you get here, login has succeeded.
'generate a JWT
dim options.algorithm as c = "HS512" 'specify the encryption algorithm for the JWT. See www.documention.alphasoftware.com for info
dim options.expiresin as n = 60000 'token is set to expire after 1,000 minutes
dim optionsjson as c = json_generate(options)
delete obj
dim obj as p
obj.userName = userName
obj.password = password
dim json as c
json = json_generate(obj)
dim token as c = extension::JSON::JWTSign(json,"shhhh!",optionsjson)
'Create the Javascript response to send to the client. The Javascript must:
'set focus to the application Panel (PANEL_APPLICATION)
'stores the JWT in localStorage
'store the JWT in the UX state object so that the JWT is submitted to the server on every Ajax callback
'NOTE: e._state.token gets translated into Javascript to set the token value in the UX state object.
logMessages = "console.log('New token:"+js_escape(token)+" generated for user: "+js_escape(userName)+"');" + crlf()
jsOut= logMessages + "{dialog.object}.panelSetActive('PANEL_APPLICATION');"
jsOut = jsOut + "localStorage.setItem('" + e.tmpl.guid + "','" + js_escape(token) + "');"
jsOut = jsOUt + "var ele = {dialog.object}.getPointer('LOGIN_ERRORS'); ele.innerHTML = '';"
xbLogin = jsOut
e._state.token = token
end function
--------------------------------------------------------------------------------------------------------------------------------------------------
I've created a separate javascript function as follows:
function control() {
var cbObj = {dialog.object}.getControl('CONTROLBAR_1');
if(cbObj) cbObj.setLayout('Layout2');
}
This javascript is to change the control bar to a different layout once login is complete. What I am trying to do is figure out how to embed this into the xbasic.
If at the end of my xbasic function, I set xblogin = control(), that triggers my javascript to change the control bar layout, but it ignores everything else in the xblogin function. How do I rewrite the xblogin function so that it does the login and the javascript together?
I have also tried adding this to my xblogin function:
dim control as c
control = control()
But that doesn't seem to fire the javaqscript. I'm sure this is easy to someone out there, I am just a newb when it comes to xbasic. Also, is there any xbasic training anywhere? I would love to study up on it.
Thanks.
Comment