Passing JWT in node js from backend to frontend - node.js

How do I pass the value of JWT from my node js backend to android/ios frontend?
Should it be scored in my mongoDB database to be picked up each time by the frontend?

I am not getting your problem when user will login JWT is sent from the node backend and your android device will store it and for every request, it will send the JWT to authenticate.

Typically for mobile apps, if you're trying to get a JWT from your node.js server, it is passed as a response to an API request.
One pattern if you're using JWT for authentication is the client will make a request to the node.js API at /authenticate for example, which will send back the JWT in the response body if authentication was successful. The client can then pass this token with subsequent requests to show that it has been authenticated- the server will parse the JWT to determine whether it is valid.
There's a great article on this here: https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens

Related

How to set auth token in React and retrieve in Node.js

I am new to React and node. I am using React.js and Nodejs. I am doing an Axios POST request where I am sending data and in Node.js, I am creating a JWT token.
Now I want to use this token in auth middleware with the code I have made.
I get data in React.js from Node.js, using res.data. I can't save the data in localStorage or sessionStorage as it is not accessible by Node.js and gives error ReferenceError: localStorage is not defined
How to save the token so that I can get in Node.js?
You can send it via the request header or even part of the request url, both of which are accessible in nodejs

Authenticating between Front and Back ends via Azure

Hello I currently have two azure app services running, one being my front end and the other being my backend. I would like my backend to require Microsoft authentication (AD), but whenever doing so any api calls from the front end are blocked.
How and what do I send to my backend from my front end to do this?
I am thinking of of using axios to export something to the backend whenever certain api calls are made. I am using Nodejs by the way. Thanks.
The flow that you should implement is the following:
On your frontend, you authenticate your users with AAD
During the authentication phase, you will need to acquire a proper accessToken that can be used to consume your API
After successful authentication, you will then call your API from the frontend, passing the acquire accessToken as an Authorization header
When the request hits the API, you will need to validate the accessToken - this is normally done by implementing a middleware component on your API that does this validation
If the accessToken is valid, you allow the request to hit the controller logic and you return the corresponding response
If the accessToken is not valid, you send a 401 or 403 back
For node.js, you can use passport to handle the token validation for you.
The below example uses a React app as the frontend and calls a node.js API, protected with AAD:
https://github.com/Azure-Samples/ms-identity-javascript-react-tutorial/tree/main/3-Authorization-II/1-call-api

Authenticating Angular Universal With JWT

I have an Angular 10 Universal project that uses JWT retrieved from localhost in order to authenticate requests for private routes.
I currently use this project for the authentication process:
https://www.npmjs.com/package/#auth0/angular-jwt
The problem I believe I am facing with Angular Universal is that when rendering pages that require authentication my requests are unable to retrieve the JWT token from localStorage. Note, this is what I think the issue is because I get a 401 unauthorized on these requests and have done some reading.
Is there any way to authenticate these requests using the JWT token that is returned when a user logs in to the website?
Backend is .Net Core 2.1
Can you save JWT to cookies after receiving it? In that case JWT will be sent along with the request, so it can be processed on server side.
Also, i think it would be nice to set this cookie with secure flag, so it can't be accessed from js in case of xss vulnerability (so it cannot be sent to 3rd party service)

Check if user logged in from backend Firebase Authentication

In my frontend, the user logs in using Firebase Authentication Browser. That part works perfectly fine. In addition to Firebase backend, I also have NodeJS backend that serves additional content. However, I need to serve the content to only Authenticated user.
My question is: Is there a way for my NodeJS backend to know that a user has been authenticated when they make a request?
An authenticated client is issued an ID token that uniquely identifies the user. The client can get this this token using the provided API. Then, it can pass that token to external APIs, which is verify the token using the Admin SDK.

How to do social auth with a decoupled backend and frontend (Passport / Express / React)

I am trying to use passportJS / Express backend, and a react JS frontend, for social auth. However, I am not certain about how to go about doing so.
I have did some reading and implemented social auth, which returns a JWT token signed by the express app when logged in with Google Auth. This allows me to access protected endpoints at the backend using the Authorization header.
However, how does one trigger this via React, especially since a call to Google should be made directly via the frontend (and hence backend does not do the authentication trigger using Passport JS)? Specifically, how should this flow look like when both backend and frontend are decoupled?
Actually, lol shortly after posting, I just found this:
https://medium.com/#alexanderleon/implement-social-authentication-with-react-restful-api-9b44f4714fa
The skinny:
auth client-side via Google
use Google's token and send over to Express server; verify that tokens are authed and match
If both match, issue a JWT token to be used in for Express JS API calls. This should be stored in localStorage.

Resources