DEV Community

loading...
Cover image for Stop coding, dad!

Stop coding, dad!

Vlad Dekhanov
Building https://juggler.dev in private. Frontend developer. Father. Space dreamer.
Updated on ・3 min read

Introduction

Hi, I'm Vladislav Dekhanov with a brilliant wife and lovely 10 months baby (yes, she is on this header:-)). I'm happy with my family in the self-isolation period till I would need to code something for any of my pet projects :) My baby loves a lot of attention from me and I'm glad to give me all to her, but the lack of time doesn't give me a chance to do anything with the coding :/ I understand that I don't have any templates for my project and backend services in particular. I know this is not rocket science and I need to think about that early, but I'm telling you how things are, pal.

Don't let the dreams be dreams!

I decided to implement a common template for a backend service. I chose the NodeJS platform due to my specialty is a frontend developer, so javascript or typescript would be much more appreciated by myself. I found NestJS the most perspective framework to build my backend applications and I use it all the time. Accordingly, in my template will be configured typescript from the box (and I'm happy about it, cause I'm sure typings save our souls :) So for now we just need to take any database for this template. In my opinion the best solution for MVP (minimum viable product) projects is MongoDB. It's easy to connect, it has official cloud storage for free till you reach 500mb, and NestJS has an adapter package to work easily with MongoDB and NestJS API.

Nothing is impossible!

With this technical stack I created a template with important for each MVP project a common feature. This is a user authentication and authorization module. In this template endpoints for registration (/auth/register) and authentication (/auth/login) functionality are implemented. So you don't need to care about how to create and login users. Authorization logic is based on the JWT token exchange. After login action the user on the client-side will receive a signed JWT token, which will be used to authorize him on another protected endpoints. For this logic I added a guard (This is interceptor of the endpoint in the NestJS) with code for checking jwt token and his expiration state. I hope it would help anyone to don't waste the time in the implementation of the users' module for pet projects again, and again, and again...

Just do it! Let the dreams come true!

So, you decide to try this template and how to start? Click the link to generate your repository on the Github from this template or simply clone it from the Github repository page. When you pull this project on your pc you need to configure the project once. First of all go to /config/.env.example and rename it to /config/.env file. Here is your configuration of the project. Let me explain each variable:

  • JWT_TOKEN - this is the key for signing your jwt token, strongly recommend to change it to keep your app more secure.
  • JWT_TOKEN_EXPIRES_IN - duration of validity jwt token signed by application. 1 hour by default. This means that the user won't be authorized on any jwt guarded route even with a signed token 1 hour later. All supported values you can take at the ms utility
  • MONGO_DB_CONNECTION - connection to your MongoDB database. For pet projects I recommend using free MongoDB Atlas.

That's all! Now you just need to open a terminal, go to the project folder, type npm start and go to localhost:3000 to see Hello World! message. It means that everything is ok and you can implement your new awesome product. Happy hacking!

If you like my article don't forget to subscribe me on the twitter :)

Discussion (1)

Collapse
adam_cyclones profile image
Adam Crockett

Hey! I get you I do but we both know what's important 😉