0

I am working on my first Node/Express-application and I was able to create a functioning app with user authentication. But what I don't understand is, how to connect a user to its stuff (e.g. the settings) that he/she has made after the login and how/where to save it?
So in case, another user is logged in, that he/she gets their selected device with the settings that they made.

My goal of the app is to control sensors on a raspberry, like to set them on and off or take photo's from a connected webcam. So can someone explain how to handle the data/setting etc. from a user?

I am sorry if this is a stupid question. But I am new this, like to understand what I am doing and before, I have only done HTML (S)CSS and simple JS, where I didn't have to worry about the backend part.

Cursor
  • 111
  • 1
  • 1
  • 13
bulb
  • 25
  • 2
  • 8

2 Answers2

3

For Authentication its better to use web token.

https://github.com/auth0/express-jwt

reference :

Node js, JWT token and logic behind

I think this is the one npm module which is used for authentication.You can get better documentation in there github repo.

implementation example link:

https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens

Community
  • 1
  • 1
Akshath Kumar
  • 489
  • 2
  • 6
  • 15
1

For some of my Node applications running on an RPi or BONE, I have used MongoDB with the Mongoose ODM.

More resources for both can be found here and here

You should then create a User entity/model and store your user with an identifier.

Example:

user-schema.js

module.exports = {
user: {
userId: Number,
name: String,
otherSettings: String
}
}

schema-builder.js

var userSchema = require('./user-schema');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

module.exports = {
    userSchema: new Schema(userSchema.user)
}

user-repo.js

var schemaBuilder = require('./schema-builder');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb');

var User = mongoose.model('User', schemaBuilder.userSchema);

module.exports = {
getUserById: function(id, callback){
User.findById(id, callback);
},
addNewUser: function(userJson, callback){
console.log('Adding new user');
var newUser = new User(userJson);
newUser.save();
}
};

server/app.js

var userRepo = require('./repositories/user-repo');

// Let's say this logic is to look for an existing user
var user;
userRepo.getUserById(userId, function(user){

// If no user found, proceed with registration
// Else, proceed with auth or login. Then use and/or update user
//..settings as required
});

Hope this helps!

Vaibhav
  • 2,527
  • 1
  • 27
  • 31