How to check if user session is active from backend (express)? - node.js

How can I check if the user is currently active from the backend(Express)?
I am using Auth0 react SDK for authentication on the frontend.
Use case:
The express server has a cron job that checks if the user session is active and runs some method.
Options:
The simplest option is probably to push logged-in timestamp to the database in intervals using an API from the react app and express cron checks the database timestamp. If the timestamp is in the ballpark of the current time, it executes the method.
However, I believe there could be a better way.
Like when the express cron executes, it can somehow get the users with currently active sessions/logged-in and performs the desired function.
Any tips or recommendations?

Related

Execute Laravel Task Schedule after a specific action

I have a function that requires Socialite - Google's token as an external API authentication.
My goal is that, after successfully authenticating with Socialite, the function should run in interval. So I decided to use Laravel Scheduler.
However, as I read the whole documentation. I assume that it works without human intervention. Basically setup your logic and schedule it in Consol\Kernel.php and that's it.
But in my case, I needed to authenticate with google Socialite, of course it will need the user to click first.
Question is, How do I run the cron job after authentication?

How to implement a Node.js service for checking an endpoint at regular intervals

I'm have built an app that the user save package tracking numbers and then when tapping on them the app sends a request to the parcel provider and gets back the tracking info. Pretty simple.
What i want to do is to create a backend server that stores the user's tracking codes and checks them at regular intervals. When the status change then the user gets a push notification.
My thought is to use a Nodes.js server with a MongoDB database but i'm stuck how to implement the tracking code monitoring on the server. Should i use a timer? Is there a better/simpler approach for this?

How to execute shell scripts on the backend of a nodejs web app?

I have a full stack MERN application running on a google compute engine, along with a local mongodb running on the same server. Its a CRUD application, however, I have a script that is stored in the server that I would like triggered everytime a user presses a button on the front end. (a use case would be, user enters some input that is logged into the database, and when its logged I would like a script triggered on the backend that creates a json file out of the mongodb table and uploads it to github/emails it out).
I'm not sure where to start learning this, a few google searches have led me to AJAX and child_processes, am I going in the right direction? Any resources or pointers would be great. Thank you
If I understood the question correctly then you want to accomplish the following things:
1. Export json data from a local MongoD instance.
2. Then send that data to github or email it somewhere.
In that case I would recommend to use child process(exec, spawn, execFile, fork) to execute the mongoexport command to get .json files.
But I don't recommend using shell script to upload that data to github or email it.
Use the github api for github and use node-mailer to email the data.
To learn more about child processes read the docs here Node.js v14.x Child process docs

Nodejs API architecture cron job

I'm building a REST API with Nodejs with MongoDB as database.
This API only have GET routes to retrieve data. This data is aggregated from others sources via cron job.
I'm not sure how to correctly implement this and what are the best practices.
Do i need to create POST/PUT route to put data in database ?
Do i need to just put data directly in the database ?
Edit : (more informations)
Only the cron jobs would use POST route
The cron jobs are getting data from others REST API and some web scraping.
Is it a good idea to have my cron in the same application with the API, or if I have to make another application to manage my cron jobs and populate my database ?
I suggest creating an API which can be called using an accessKey for updating the data because you would not want to write your mongo db username and password in a shell file.
But if the cron job is a k8s cron job or just a file written in some programming language which can access the db in secure way and is hosted in the same network then you can go ahead with doing it from cron job.
If you'd like to control the data flow entirely through API at this point, creating a POST route would be the way to go. I am not sure how are your GET routes secured - if not at all, consider implementing or at least hard-coding some sort of security for routes that modify your data (oAuth2 or similar).
If you can access the database directly and it's desired, you can just directly insert/update the data.
The second option would be probably quicker, but the first one offers more space for expansion in the future and could be more useful overall.
So in the end, both options are valid, it's up to preference and your use case.

Emberjs model not fully loaded before afterModel being fired

I have an action that retrieves some data from the DB and then I wanted to check that the data it have received is correct before it continues on with authentication. I am using invite codes to allow people to log into a public site that is for private corporate use only. I am able to get the data just fine, but the aftermodel is firing before the request is completed. I am making a call to an azure mobile service and the call is still in pending (according to chrome) when the aftermodel is firing off. Seems like it hasn't received the data at that point.
What is the best method to get this verification working properly? Once it verifies it would then allow them to log in with an external provider.
Please, look at this discussion: Ember authentication best practices?
If you don't need an Auth engine, then you could implement "verifying data" in beforeModel hook. Why beforeModel? Because if data is not correct, then app should redirect user to another page, and beforeModel is made for this logic: http://emberjs.com/guides/routing/preventing-and-retrying-transitions/#toc_aborting-transitions-within-code-model-code-code-beforemodel-code-code-aftermodel-code

Resources