AWS APIGateway & Lambda - How to call function right before iOS App terminates? - swift

I have been working with AWS for a little while now and I am starting to get the hang of APIGateway and Lambda. I have just made a Lambda function that receives the username entered by a user and then uses the AdminDeleteUser function to delete that user from the user pool, and it works fine. I even have it set up with APIGateway to be used in XCode and it works fine with that as well.
Problem
My problem is, is that I am trying to have the Lambda function run right as the user terminates the iOS app. I am using a NotificationCenter observer to watch when the App terminates, that all works well except the fact that I believe the Lambda function's call is cancelled once the app fully terminates, meaning it can't complete the full request. My lambda function essentially checks if the username that is being received is a user who is unconfirmed, and if they are unconfirmed then they are deleted from the user pool. I am doing this on the screen where the user has to confirm a confirmation code that was sent via SMS
Question
So I guess the questions I have are,
Is it possible to call a Lambda function while the App is terminating?
If not, is it possible to delete the current user from the user pool if they are not confirmed, before the App exits?
Thank you in advanced.

It sounds like what is happening is exactly what you have described above. The app is exiting before the connection can be made to the service.
A better design might be to have a process run at X interval (using a Lambda scheduled event) to automatically remove any unconfirmed users from your data store.
Edit:
When searching for unconfirmed users, you should be able to use the UserCreateDate field to check if the user has registered within your specified amount of time. If 'createDate' falls within that time, ignore that user.
ListUsers API

Related

Postgres - Locking insertion of non-existing row until transaction is committed

I have an application in which there are users, issuers, certificates and issues. users are the basic account, and issuers are the upgraded accounts who are able to distribute certificates to users. And distributing of certificates are called issues. The app uses postgres 14.4.
There is something called 'passive issue' in the application, which refers to issue of a certificate to a user that is not yet registered. Passive issue executes a transaction as follows:
check if user exists, if it does ignore it, if it does not, continue
create a passive issue which has awaiting register status
and then when that user actually registers, a trigger fires and updates the issue status.
The problem is that, after checking if user exists, and it does not, but right at that moment the user creates the account, and we try to passive issue, it will throw an error saying user already exists.
There are 2 possible approaches to solve this you might have thought so far:
just catch and check the error and redirect it to normal issuing path rather than passive issue within the api itself
return the error and let client retry the request by sending a request to normal issue path
The problem with above solutions:
It can not be done. Simply because this application uses blockchain, and normal issue endpoint requires the signature of the issuer. Passive issue is specifically implemented to be sent without a signature, and signature is generated on the client-side. And sending the private key to api is NOT even a possibility due to security concerns.
This is possible. This way the client will be noticed that this account registered, and it can prepare the signature and directly send a request to normal issue api. But it requires a longer and more complex implementation with retry logic etc.
And what I think would be the most neat solution in my case is such a scenario of passive issue:
check if user exists, and if it doesn't, lock the insert into the user table for that specific email ONLY
create passive issues for those accounts with await registration status (tx committed at this point and lock is released)
now even if the user tried to register in the middle, it will wait until issues are created and then user will be registered, and then trigger will fire, and since there are passive issues on this user now, it will update them.
So... Long story short: is there a way to put a lock on insertion of non-existing rows? And if there is, is it more feasible than the 2nd or any other possible solution?

askForPermission not working on Actions on Google simulator

I am trying to get the current location of the user and I am using the Api.AI web tool to create my actions/intent.
Deployed using -
firebase --only functions
API.AI Action -
Simulator -
Full index.js - link
But when my action is triggered I get this message -
Sorry, this action is not available in simulation
Can't we ask for permission on a simulation ? Also how do I test my app on a real device ?
EDIT:
The permission function is stuck in a loop -
Your problem is in this block of code:
//Action business logic
function welcomeMessage(app){
app.tell('Welcome !, Do you want to book a ride ?'); // Todo: Insert proper messages.
}
The app.tell() method sends the message and then closes the conversation. If you want to send the message to the user and keep the conversation going (ie - you're expecting a response) you need to use app.ask().
This is what is causing the "Sorry" message when you reply "Yeah" - your Action is no longer listening.
You can ask for permission in the simulation.
Once you have run it once in the simulator, you should be able to access it on any device that is linked to the same account you used to develop the Action, or to other accounts you've permitted (once they have run the simulator).

AWS Lambda & Cognito - Updating user phone number attribute without sending an SMS

I am working on an iOS app using Amazon Web Services and I am setting up a user data base using the Cognito Userpool. During the sign up process, if a user enters the wrong phone number by mistake and in result isn't receiving a verification code, I am trying to allow them to then enter a new phone number, and update their phone number user attribute. Right now I am using a Lambda function which uses the AdminUpdateUserAttribute function, which is then connected to a APIGateway which allows me to run it from XCode. The function itself works and it successfully updates the phone number attribute.
Problem
The problem that I am running into though, is that after the phone number attribute has been updated with the Lambda function, a verification code is automatically sent to the newly updated phone number via SMS. The verification code is weird though because when I use that code to confirm the user, it doesn't work. Meaning that code is invalid for confirmation purposes. But if I use the Resend Confirmation Code function it will then send a valid confirmation code to the newly updated phone number.
Question
So I guess the questions I have are:
How can I prevent the automatic SMS from sending after I update the user's phone number attribute?
Or, is there a way I can use the verification code that is automatically sent as a confirmation code?
Thank you in advanced.

How to call Web service Continuously in ios

I have an ios app.
In which i have multi user account.So multiple user can Login.
When one user see other user profile and if that user is also online i want to notify him.
I can't get how to implement it.
If i have to continuously check some flag value on server if someone has checked their profile or how can i handle it?
Remote notifications is the way to do it. The logic on when and who has to be notified can be handled at the server.
You may have seen lots of apps asking for permissions to send notification. The downside being if the user denies that permission.
Polling from client side can also be one of the methods which can be used and but would require the client to do most of the computation.
So all in all, its a call you got to take considering what you are building.

Can I de-authorize a Facebook app and log the user out at the same time?

I want to log the user out of my app and remove their permissions
I'm currently using the JS-SDK and have no Server Side Logic.
I know I can log a user out with FB.logout()
I can also revoke their permissions using FB.api('/me/permissions', 'DELETE') to "de-authorize" the app
The problem is, I cannot seem to do both. Each seems to prevents the other from working.
EXAMPLES:
Calling FB.api('/me/permissions', 'DELETE', function(){FB.logout()});
results in the following error:
"Refused to display document because display forbidden by X-Frame-Options."
Calling: FB.logout(function(){FB.api('/me/permissions', 'DELETE');})
results in the user being logged out, but the permissions remain.
Answering my own question:
After doing a fair amount of research and experimentation, I've concluded that there is no reliable way to do this.
I was able to determine that the following code works - though I wouldn't recommend its use in a production app.
FB.logout();
FB.api('/me/permissions', 'DELETE');
Apparently calling them one after another works (vs. doing it with callbacks). Once either of them finishes it prevents the other from starting, but if I make the asynchronous calls in quick succession it seems to let both succeed.