Newby Coder header banner

Flutter Android/iOS client application for NodeJs Jwt Authentication Server

Following provides some operational info about connecting a Flutter Android/iOS client application and a NodeJs Jwt Authentication Server

Code links for an example Nodejs server and a corresponding Flutter application are also provided

Summary

To summarise, the server has to be reachable by the mobile application

Code

Local/Development server

During development, a server is typically run in some custom port other than a generic http (80) port or ssl (443) port

Local server can be run for it to be accessible over a local network like wifi

This allows an emulator/simulator or a device connected to the local network to be able to send requests to the server and receive response

Find ip address of System

Use ifconfig command in Linux/MacOs to get the ip address for local network

An ipv4 address is shown after inet in the commands output for an adapter

If connected to wifi, look for an adapter with prefix wlp like wlp2s0 or wlp4s0

cl-ip-address-linux

ip address used in this example is 192.168.43.12 which is returned by ifconfig command

Server side settings

The server is to run on the ip address retrieved i.e 192.168.43.12 here

When run from the command line, the ip address is added as a parameter to the run command

nodejs index.js 192.168.43.12

This starts the Nodejs server which listens for any client in the address 192.168.43.12:4500

Assign a specific port, during server initialisation (in index.js)

const port = process.env.NODE_ENV === 'production' ? 80 : 4500;

// start server
const server = app.listen(port, function () {
    console.log('Server listening on port ' + port);
});

This can be a random unused port (most ports should be unused in a typical home computer)

Use netstat -tupln command to check for active ports

Client (Mobile app)

The corresponding Flutter application uses the ip address in a Http request in its code along with the port and specific path for an Api

Since provided example runs without ssl, the requests have to be sent using http protocol

final url = 'http://192.168.43.12:4500/users/authenticate';
await http.get(url, headers: headers)

Check Flutter Application to connect to the server for mobile application code which works in Android and iOS

cl-flutter-jwt-authcm-flutter-jwt-authentication-as2