<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: webmasteradam</title>
    <description>The latest articles on DEV Community by webmasteradam (@webmasteradam).</description>
    <link>https://dev.to/webmasteradam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F343516%2F03327569-a065-475e-87e9-4a092275ddc6.jpeg</url>
      <title>DEV Community: webmasteradam</title>
      <link>https://dev.to/webmasteradam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/webmasteradam"/>
    <language>en</language>
    <item>
      <title>Send notifications from Node.JS</title>
      <dc:creator>webmasteradam</dc:creator>
      <pubDate>Sat, 14 Mar 2020 02:02:32 +0000</pubDate>
      <link>https://dev.to/webmasteradam/send-notifications-from-node-js-1f2f</link>
      <guid>https://dev.to/webmasteradam/send-notifications-from-node-js-1f2f</guid>
      <description>&lt;p&gt;Pushsafer make it easy and safe to get push-notifications in real time on your&lt;/p&gt;

&lt;p&gt;Android device&lt;br&gt;
iOS device (incl. iPhone, iPad, iPod Touch)&lt;br&gt;
Windows Phone &amp;amp; Desktop&lt;br&gt;
Browser (Chrome &amp;amp; Firefox)&lt;br&gt;
API description&lt;/p&gt;

&lt;p&gt;Usage&lt;br&gt;
Install&lt;br&gt;
npm install pushsafer-notifications&lt;br&gt;
Pushsafer API values&lt;br&gt;
Any API parameters, as found on &lt;a href="https://www.pushsafer.com/en/pushapi"&gt;https://www.pushsafer.com/en/pushapi&lt;/a&gt;, can be passed in the object. Here's an example with many different parameters.&lt;/p&gt;

&lt;p&gt;var msg = {&lt;br&gt;
    m: 'This is a Node.js test message',   // Message (required)&lt;br&gt;
    t: "Node.js Test",                     // Title (optional)&lt;br&gt;
    s: '8',                                // Sound (value 0-60) (optional)&lt;br&gt;
    v: '2',                                // Vibration (empty or value 1-3) (optional)&lt;br&gt;
    i: '5',                                // Icon (value 1-177) (optional)&lt;br&gt;
    c: '#FF0000',                          // Icon color hexadecimal color code (optional) &lt;br&gt;
    d: '221',                              // Device or Device group id (optional)&lt;br&gt;
    u: '&lt;a href="https://www.pushsafer.com"&gt;https://www.pushsafer.com&lt;/a&gt;',        // an URL (optional)&lt;br&gt;
    ut: 'Pushsafer.com',                   // URLs title (optional)&lt;br&gt;
    l: '10',                               // Time to Live (optional: 0-43200 minutes)&lt;br&gt;
    pr: '2',                               // Priority (optional: -2, -1, 0, 1, 2)&lt;br&gt;
    re: '60',                              // Retry (optional: 60-10800 seconds)&lt;br&gt;
    ex: '60',                              // Expire (optional: 60-10800 seconds)&lt;br&gt;
    a: '1',                    // Answer&lt;br&gt;
    p: '',                                 // Image converted to &amp;gt; Data URL with Base64-encoded string (optional)&lt;br&gt;
    p2: '',                                // Image 2 converted to &amp;gt; Data URL with Base64-encoded string (optional)&lt;br&gt;
    p3: ''                                 // Image 3 converted to &amp;gt; Data URL with Base64-encoded string (optional)&lt;br&gt;
};&lt;br&gt;
Examples&lt;br&gt;
Sending a message&lt;/p&gt;

&lt;p&gt;var push = require( 'pushsafer-notifications' );&lt;/p&gt;

&lt;p&gt;var p = new push( {&lt;br&gt;
    k: 'Your20CharPrivateKey',             // your 20 chars long private key or 15 chars long alias key  (required)&lt;br&gt;
    debug: true&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;var msg = {&lt;br&gt;
    m: 'This is a Node.js test message',   // Message (required)&lt;br&gt;
    t: "Node.js Test",                     // Title (optional)&lt;br&gt;
    s: '8',                                // Sound (value 0-28) (optional)&lt;br&gt;
    v: '2',                                // Vibration (empty or value 1-3) (optional)&lt;br&gt;
    i: '5',                                // Icon (value 1-98) (optional)&lt;br&gt;
    c: '#FF0000',                          // Icon color hexadecimal color code (optional)&lt;br&gt;
    d: '221',                              // Device or Device Group id (optional)&lt;br&gt;
    u: '&lt;a href="https://www.pushsafer.com"&gt;https://www.pushsafer.com&lt;/a&gt;',        // an URL (optional)&lt;br&gt;
    ut: 'Pushsafer.com',                   // URLs title (optional)&lt;br&gt;
    l: '10',                               // Time to Live (optional)&lt;br&gt;
    pr: '2',                               // Priority (optional: -2, -1, 0, 1, 2)&lt;br&gt;
    re: '60',                              // Retry (optional: 60-10800 seconds)&lt;br&gt;
    ex: '60',                              // Expire (optional: 60-10800 seconds)&lt;br&gt;
    a: '1',                                // Answer&lt;br&gt;
    p: '',                                 // Image converted to &amp;gt; Data URL with Base64-encoded string (optional)&lt;br&gt;
    p2: '',                                // Image 2 converted to &amp;gt; Data URL with Base64-encoded string (optional)&lt;br&gt;
    p3: ''                                 // Image 3 converted to &amp;gt; Data URL with Base64-encoded string (optional)&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;// console.log( p );&lt;/p&gt;

&lt;p&gt;p.send( msg, function( err, result ) {&lt;br&gt;
    //console.log( 'ERROR:', err );&lt;br&gt;
    console.log( 'RESULT', result );&lt;br&gt;
    // process.exit(0);&lt;br&gt;
});&lt;/p&gt;

</description>
      <category>node</category>
      <category>notification</category>
      <category>javascript</category>
      <category>corona</category>
    </item>
    <item>
      <title>Node.js and PASSPORT JS Strategies</title>
      <dc:creator>webmasteradam</dc:creator>
      <pubDate>Tue, 10 Mar 2020 08:40:49 +0000</pubDate>
      <link>https://dev.to/webmasteradam/node-js-and-passport-js-strategiee-p80</link>
      <guid>https://dev.to/webmasteradam/node-js-and-passport-js-strategiee-p80</guid>
      <description>&lt;p&gt;In this article you will learn how to handle authentication for your Node server using Passport.js. This article does not cover Frontend authentication. Use this to configure your Backend authentication (Generate token for each user &amp;amp; protect routes).&lt;/p&gt;

&lt;p&gt;Keep in mind that if you get stuck on any step, you can refer to this GitHub repo.&lt;/p&gt;

&lt;p&gt;In this article I will teach you the following:&lt;br&gt;
Handling protected routes&lt;br&gt;
Handling JWT tokens&lt;br&gt;
Handling unauthorised responses&lt;br&gt;
Creating a basic API&lt;br&gt;
Creating models &amp;amp; schemas&lt;br&gt;
Introduction&lt;br&gt;
What is Passport.js?&lt;br&gt;
Passport is authentication middleware for Node.js. As it’s extremely flexible and modular, Passport can be unobtrusively dropped into any Express-based web application. A comprehensive set of strategies supports authentication using a username and password, Facebook, Twitter, and more. Find out more about Passport here.&lt;/p&gt;

&lt;p&gt;Tutorial&lt;br&gt;
Creating our node server from scratch&lt;br&gt;
Create a new directory with this “app.js” file inside:&lt;/p&gt;

&lt;p&gt;We will install nodemon for easier development.&lt;/p&gt;

&lt;p&gt;and then we will run our “app.js” with it.&lt;/p&gt;

&lt;p&gt;$ nodemon app.js&lt;/p&gt;

&lt;p&gt;Expected result after running the command above&lt;br&gt;
Creating the user model&lt;br&gt;
Create a new folder called “models”, and create the “Users.js” file inside that folder. This is where we will define our “UsersSchema”. We are going to use JWT and Crypto to generate hash and salt from the received password string. This will later be used to validate the user.&lt;/p&gt;

&lt;p&gt;You should now have this structure&lt;br&gt;
Let’s add our newly created model to “app.js”.&lt;/p&gt;

&lt;p&gt;Add the following line to your “app.js” file after configuring Mongoose:&lt;/p&gt;

&lt;p&gt;require('./models/Users');&lt;/p&gt;

&lt;p&gt;Configure Passport&lt;br&gt;
Create a new folder “config” with the “passport.js” file inside it:&lt;/p&gt;

&lt;p&gt;In this file, we use the method validatePassword that we defined in the User model . Based on the result, we return a different output from Passport’s LocalStrategy.&lt;/p&gt;

&lt;p&gt;You should now have this structure&lt;br&gt;
Let’s connect “passport.js” to our “app.js” file. Add the following line below all models:&lt;/p&gt;

&lt;p&gt;require('./config/passport');&lt;/p&gt;

&lt;p&gt;The Passport require must be below all models&lt;br&gt;
Routes and authentication options&lt;br&gt;
Create a new folder called “routes” with the file “auth.js” inside it.&lt;/p&gt;

&lt;p&gt;In this file we use the function getTokenFromHeaders to get a JWT token that will be sent from the client side in the request’s headers. We also create an auth object with optional and required properties. We will use these later in our routes.&lt;/p&gt;

&lt;p&gt;In the same “routes” folder create an “index.js” file:&lt;/p&gt;

&lt;p&gt;We now need an “api” folder inside the “routes” folder, with another “index.js” file inside it.&lt;/p&gt;

&lt;p&gt;You should now have this structure&lt;br&gt;
Now, let’s create the “users.js” file that we require in “api/index.js”.&lt;/p&gt;

&lt;p&gt;First, we are going to create an optional auth route ‘/’ which will be used for new model creation (register).&lt;/p&gt;

&lt;p&gt;router.post('/', auth.optional, (req, res, next) ...&lt;br&gt;
After that, we are going to create another optional auth route ‘/login’ . This will be used to activate our passport configuration and validate a received password with email.&lt;/p&gt;

&lt;p&gt;router.post('/login', auth.optional, (req, res, next) ...&lt;br&gt;
Lastly, we will create a required auth route, which will be used to return the currently logged in user. Only logged in users (users that have their token successfully sent through request’s headers) have access to this route.&lt;/p&gt;

&lt;p&gt;router.get('/current', auth.required, (req, res, next) ...&lt;/p&gt;

&lt;p&gt;You should now have this structure&lt;br&gt;
Let’s add our “routes” folder to “app.js”. Add the following line below our passport require:&lt;/p&gt;

&lt;p&gt;app.use(require('./routes'));&lt;/p&gt;

&lt;p&gt;Route testing&lt;br&gt;
I will be using Postman to send requests to our server.&lt;/p&gt;

&lt;p&gt;Our server accepts the following body:&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
  "user": {&lt;br&gt;
    "email": String,&lt;br&gt;
    "password": String&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
Creating a POST request to create a user&lt;br&gt;
Test body:&lt;/p&gt;

&lt;p&gt;Response:&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
    "user": {&lt;br&gt;
        "_id": "5b0f38772c46910f16a058c5",&lt;br&gt;
        "email": "&lt;a href="mailto:arguc.mertcan@gmail.com"&gt;arguc.mertcan@gmail.com&lt;/a&gt;",&lt;br&gt;
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImVyZGVsamFjLmFudG9uaW9AZ21haWwuY29tIiwiaWQiOiI1YjBmMzg3NzJjNDY5MTBmMTZhMDU4YzUiLCJleHAiOjE1MzI5MDgxNTEsImlhdCI6MTUyNzcyNDE1MX0.4TWc1TzY6zToHx_O1Dl2I9Hf9krFTqPkNLHI5U9rn8c"&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
We will now use this token and add it to our “Headers” in Postman’s configuration.&lt;/p&gt;

&lt;p&gt;And now let’s test our auth only route.&lt;/p&gt;

&lt;p&gt;Creating a GET request to return the currently logged in user&lt;br&gt;
Request URL:&lt;/p&gt;

&lt;p&gt;GET &lt;a href="http://localhost:8000/api/users/current"&gt;http://localhost:8000/api/users/current&lt;/a&gt;&lt;br&gt;
Response:&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
    "user": {&lt;br&gt;
        "_id": "5b0f38772c46910f16a058c5",&lt;br&gt;
        "email": "&lt;a href="mailto:arguc.mertcan@gmail.com"&gt;arguc.mertcan@gmail.com&lt;/a&gt;",&lt;br&gt;
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImVyZGVsamFjLmFudG9uaW9AZ21haWwuY29tIiwiaWQiOiI1YjBmMzg3NzJjNDY5MTBmMTZhMDU4YzUiLCJleHAiOjE1MzI5MDgzMTgsImlhdCI6MTUyNzcyNDMxOH0.5UnA2mpS-_puPwwxZEb4VxRGFHX6qJ_Fn3pytgGaJT0"&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

</description>
      <category>passport</category>
      <category>node</category>
      <category>javascript</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Why I'm using Nodejs, Express, NPM</title>
      <dc:creator>webmasteradam</dc:creator>
      <pubDate>Sun, 08 Mar 2020 00:14:48 +0000</pubDate>
      <link>https://dev.to/webmasteradam/why-i-m-using-nodejs-express-npm-5402</link>
      <guid>https://dev.to/webmasteradam/why-i-m-using-nodejs-express-npm-5402</guid>
      <description>&lt;p&gt;Nodejs is the programming language of our age. We can also say that the Javascript language is ServerSide. With the power it receives from the Nodejs NPM library, it will enable you to overcome most jobs in a fast, quality and professional manner. It is fun to write both FrontEnd and BackEnd with one language.&lt;/p&gt;

&lt;p&gt;In my simple and OnePage projects, I can produce boutique websites within 1 hour by using ExpressJS and Express Framework. In these projects, there are some standard tools that I use from the NPM library, so I can do very fast and high quality works.&lt;/p&gt;

&lt;p&gt;In order to deal with enormous projects, I only pass this language (NODEJS).&lt;br&gt;
I have used frameworks like PHP and Laravel, Codeigniter for years. I said goodbye to all of them with Nodejs.&lt;/p&gt;

&lt;p&gt;For example: “PassportJS” for Login Register operations, “Mongoose” as Orm for jobs to be done with Mongodb, “Express Session” for jobs that require Session management, “Async” for operations that need to be asynchronous, and file management (Image uploading) I use “Multer” for file uploads, “Momentjs” for jobs that require date and time management. We can increase these examples to 100 or even 1000. So let’s take a breath and get back to the subject.&lt;/p&gt;

&lt;p&gt;As I explained above, with the power it takes from the NPM library, you can push the boundaries with this language that we can easily overcome every problem.&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>npm</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
