I currently have this as my xbasic function:
function xb1 as c (e as p)
'this is a sample Xbasic function to handle an Ajax callback. It is used to demonstrate
'how the server-side canAjaxCallback event is used to authorize all Ajax callbacks.
dim js as c
dim msg as c
msg = "Ajax callback at: " + now()
js = "$('div1').innerHTML = '" + js_escape(msg) + "';"
xb1 = js
end function
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 = '';"
'jsOut = jsOut + "completePersistLogin('" + e.tmpl.guid + "','" + js_escape(token) + "','" + userName + "');"
jsOut = "completePersistLogin();"
xbLogin = jsOut
e._state.token = token
e._state.userName = userName
e._state.guid = e.tmpl.guid
end function
function xbLogout as c (e as p)
'log the user out of the Alpha Anywhere security framework
A5WS_LogoutUser()
dim jsOut as c
'Create the Javascript response to send to the client. The Javascript must:
'return focus to the login panel
'delete the token from localStorage
'delete the token from the UX state object so that it does not get sent to server on subsequent Ajax callbacks
jsOut = "{dialog.object}.panelSetActive('PANEL_LOGIN');" + crlf()+\
"var key = '" + e.tmpl.guid + "';" + crlf()+\
"localStorage.setItem(key,''); " + crlf()+\
"delete {dialog.object}.stateInfo['token'];"
xbLogout = jsOut
end function
And I have this as my javascript function:
function completePersistLogin(){
//function completePersistLogin(guid,token,userName){
//e._state.token = token
//e._state.userName = userName
//e._state.guid = e.tmpl.guid
debugger;
var guid = {dialog.object}.stateInfo['guid'];
var userName = {dialog.object}.stateInfo['userName'];
var token = {dialog.object}.stateInfo['token'];
var friendlyname = {dialog.object}.stateInfo['friendlyName']
{dialog.object}.panelSetActive('PANEL_APPLICATION');
localStorage.setItem(guid,token);
localStorage.setItem('userName',userName);
//child
var cObj = {dialog.Object}.getChildObject('SNAP_MOBILE');
var cEle = cObj.getPointer('logged_in_as');
cEle.innerHTML = userName;
var ele = {dialog.object}.getPointer('LOGIN_ERRORS');
ele.innerHTML = '';
}
I have a SQL table called 'usersregistered'. I want to open that table, grab a field called first_name, and turn that into a state variable that can be used in the app. I started writing the code for this, and came up with this so far:
dim cn as sql::Connection
dim args as SQL::Arguments
args.set("whatuser", e.datasubmitted.username)
flag = cn.open("::Name::snap-sql")
dim sqlCommand as c
sqlCommand = "Select first_name from usersregistered where username = :whatuser"
flag = cn.execute(sqlCommands,args)
friendlyname
But that is as far as I got. I'm not exactly sure what to do after this. The end goal is that in the Javascript, I've got this line:
cEle.innerHTML = userName, which puts an email address in a field in my app called 'logged_in_as'. I basically want to change that out so that it displays a first name instead of an email address. So a friendlyname used in conjunction with persistent login. Any ideas around how to complete this would be much appreciated. Thanks.
function xb1 as c (e as p)
'this is a sample Xbasic function to handle an Ajax callback. It is used to demonstrate
'how the server-side canAjaxCallback event is used to authorize all Ajax callbacks.
dim js as c
dim msg as c
msg = "Ajax callback at: " + now()
js = "$('div1').innerHTML = '" + js_escape(msg) + "';"
xb1 = js
end function
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 = '';"
'jsOut = jsOut + "completePersistLogin('" + e.tmpl.guid + "','" + js_escape(token) + "','" + userName + "');"
jsOut = "completePersistLogin();"
xbLogin = jsOut
e._state.token = token
e._state.userName = userName
e._state.guid = e.tmpl.guid
end function
function xbLogout as c (e as p)
'log the user out of the Alpha Anywhere security framework
A5WS_LogoutUser()
dim jsOut as c
'Create the Javascript response to send to the client. The Javascript must:
'return focus to the login panel
'delete the token from localStorage
'delete the token from the UX state object so that it does not get sent to server on subsequent Ajax callbacks
jsOut = "{dialog.object}.panelSetActive('PANEL_LOGIN');" + crlf()+\
"var key = '" + e.tmpl.guid + "';" + crlf()+\
"localStorage.setItem(key,''); " + crlf()+\
"delete {dialog.object}.stateInfo['token'];"
xbLogout = jsOut
end function
And I have this as my javascript function:
function completePersistLogin(){
//function completePersistLogin(guid,token,userName){
//e._state.token = token
//e._state.userName = userName
//e._state.guid = e.tmpl.guid
debugger;
var guid = {dialog.object}.stateInfo['guid'];
var userName = {dialog.object}.stateInfo['userName'];
var token = {dialog.object}.stateInfo['token'];
var friendlyname = {dialog.object}.stateInfo['friendlyName']
{dialog.object}.panelSetActive('PANEL_APPLICATION');
localStorage.setItem(guid,token);
localStorage.setItem('userName',userName);
//child
var cObj = {dialog.Object}.getChildObject('SNAP_MOBILE');
var cEle = cObj.getPointer('logged_in_as');
cEle.innerHTML = userName;
var ele = {dialog.object}.getPointer('LOGIN_ERRORS');
ele.innerHTML = '';
}
I have a SQL table called 'usersregistered'. I want to open that table, grab a field called first_name, and turn that into a state variable that can be used in the app. I started writing the code for this, and came up with this so far:
dim cn as sql::Connection
dim args as SQL::Arguments
args.set("whatuser", e.datasubmitted.username)
flag = cn.open("::Name::snap-sql")
dim sqlCommand as c
sqlCommand = "Select first_name from usersregistered where username = :whatuser"
flag = cn.execute(sqlCommands,args)
friendlyname
But that is as far as I got. I'm not exactly sure what to do after this. The end goal is that in the Javascript, I've got this line:
cEle.innerHTML = userName, which puts an email address in a field in my app called 'logged_in_as'. I basically want to change that out so that it displays a first name instead of an email address. So a friendlyname used in conjunction with persistent login. Any ideas around how to complete this would be much appreciated. Thanks.
Comment