0

I tried to implement a simple login system with Mongodb and Node.js, but I found it to be too much of a hassle (everyone is building things on a hundred of frameworks and not explaining half of the code). I probably could do it, but it would require too much work and so on, as I only want to make a simple multiplayer game.

My alternative to databases are plaintext files. I do plan to hash the passwords, of course. The first problem that came to my mind was that the file could potentially get too big, and that the server would slow down completely trying to read it (in a hypothetical situations, I doubt my game will have that many users). The solution would be to create a users/ folder on the server, and split the big file into many smaller ones. Their names would correspond with the usernames, and there hashed passwords and emails would be held. This solves the speed issue, and I can't see how I still need databases!

Am I missing something? Are there other advantages to using databases? Also, I would be very pleased if someone answered with a login Mongodb example, that would solve all my problems. Just make it as simple as possible.

Community
  • 1
  • 1
corazza
  • 31,222
  • 37
  • 115
  • 186
  • 1
    A database is useful when you want to start querying your data. Suppose every user's file stores his high score as well, displaying the top 10 high scores in your system would be a nightmare. – Ayush Jun 13 '12 at 17:22
  • I think the main reason ppl use a database is for the scalability issue you describe. If you can keep that file away from web users, you should be in decent shape. – Jeremy Holovacs Jun 13 '12 at 17:23
  • 7
    Why would you try to emulate a database with plaintext files? Databases are explicitly made for that purpose... – Bergi Jun 13 '12 at 17:24

4 Answers4

4

There are a ton of reasons to use a real database instead of some hacked, half-baked file implementation. Some big ones:

  • ACID (this is really 4 reasons)
  • Performance & stability (databases are some of the most mature pieces of software still in use)
  • Standardization

If you want "lightweight," I'd recommend that you start with SQLite.


Related:

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

Let's say you have an authentication information as: UserName(10), Password(32). These makes 42 characters. Let's say each character is 4 bytes (consisting all characters in the universe), each user consumes 42 * 4 = 168 bytes. Even if you have 100.000 users, it would only require 16 MBs of memory. It can fit the whole RAM, you only need to wirte changes to disk, and read them all in the boot of the program. Of course it would have a litle overhead, but not a big deal.

However, handling files may become an issue, you do not have to re invent the wheel. This is what databases are for. I agree, using Mongo, MySQL can be a overkill sometiems, however If you only plan to use database for authentication, you may go with more lightweight solution, you can use SQLite, which has only a file, a ~270KB client and a nice NodeJS library. Have a look at this nice post

Mustafa
  • 10,013
  • 10
  • 70
  • 116
1

If you are working with mongo I would strongly recommend working with the Mongoose library. It provides an excellent ORM for Mongo. If you do end up deciding to work with that ORM there is an established authentication library called mongoose-auth. I used the default password example for my own site.

thedjpetersen
  • 27,857
  • 4
  • 26
  • 28
1

If you are using NodeJS mongoDB would work well for what you want, there is also the option of redis,couch etc... MongoDB is as good a place as any to start and f you have been playing with it a little might as well stick with it.

Have a look to this repo which using a cloudfoundry or Mongolab (both free) mongoDb server.

https://github.com/daithiw44/Node_Authenticated_MongoDB

The demo has a button which when clicks hits the db to get a value. Substitute the hardcoded value for values in username and password textfields and one could authenticated a user and set up an expressjs session.

This was written with an older version of node but a tweak to the package.json should give you a good place to get started.

Hope that helps.

daithi44
  • 36
  • 2