<?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>Forem: Marcelo</title>
    <description>The latest articles on Forem by Marcelo (@marcelomsc).</description>
    <link>https://forem.com/marcelomsc</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%2F1423060%2F9987ecae-7791-4a51-9af9-131197f4fa71.jpg</url>
      <title>Forem: Marcelo</title>
      <link>https://forem.com/marcelomsc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/marcelomsc"/>
    <language>en</language>
    <item>
      <title>Add a google maps on my reactjs resume</title>
      <dc:creator>Marcelo</dc:creator>
      <pubDate>Mon, 29 Apr 2024 13:54:55 +0000</pubDate>
      <link>https://forem.com/marcelomsc/add-a-google-maps-on-my-reactjs-resume-bc7</link>
      <guid>https://forem.com/marcelomsc/add-a-google-maps-on-my-reactjs-resume-bc7</guid>
      <description>&lt;h2&gt;
  
  
  I added a Google map and a marker to show my approximate location on my ReactJS resume.
&lt;/h2&gt;

&lt;p&gt;I have this ReactJS resume with Carbon, Sass, Styled Components, react-router app. It's an old app created with create-react-app that consume Gitconnected API to get my information and show to the user.&lt;/p&gt;

&lt;p&gt;It has components on which files, such as Sidebar, Navbar, UserHeader, Layout and GoogleMarker and a Mobile Nav.&lt;/p&gt;

&lt;p&gt;Furthermore, it's deployed on Netlify and you can access in this link:&lt;br&gt;
&lt;a href="https://marcelomsc-cv.netlify.app"&gt;marcelomsc-cv&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check it out on Github:&lt;br&gt;
&lt;a href="https://github.com/Marcel-MSC/portfolio-marcelo"&gt;portfolio-marcelo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have a snapshot of the google maps implementation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptzt58ebihr32xcrndqb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptzt58ebihr32xcrndqb.png" alt="snapshot google implementation" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main problem was deploying to Netlify that handles the environment variables in many ways.&lt;/p&gt;

&lt;p&gt;I use react-dotenv to set my environment variable on .env to use on development and using of window.env to access a variable on Netlify, because the window.env of react-dotenv get global variables that are setted.&lt;/p&gt;

&lt;p&gt;On Netlify UI, I set the variable to use on production/build.&lt;/p&gt;

&lt;p&gt;Here's how I used on my Map.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let googleMapsApi = '';
if (env.ENV_GOOGLE_MAPS_API_KEY !== undefined) {
   // dev environment
   googleMapsApi = env.ENV_GOOGLE_MAPS_API_KEY;
}
if (window.env.GOOGLE_MAPS_API_KEY !== undefined) {
   // production/build enviroment
   googleMapsApi = window.env.GOOGLE_MAPS_API_KEY;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use the API of Google Maps, it's simple:&lt;br&gt;
Install the google-map-react package.&lt;/p&gt;

&lt;p&gt;Setting some props:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const defaultProps = {
    center: {
      lat: -23.553646087646484,
      lng: -46.561336517333984
    },
    zoom: 14
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to handle the of map objects of Google Maps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleApiLoaded = (map, maps) =&amp;gt; {
    // use map and maps objects
    // todo - implement handles to users interactions
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the GoogleMapReact component of the package installed.&lt;br&gt;
Pass the API on bootstrapURLKeys. I'm using the variable googleMapsApi to set the api key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;GoogleMapReact
  bootstrapURLKeys={{ key: googleMapsApi }}
  defaultCenter={defaultProps.center}
  defaultZoom={defaultProps.zoom}
  yesIWantToUseGoogleMapApiInternals={true}
  onGoogleApiLoaded={({ map, maps }) =&amp;gt; handleApiLoaded(map, maps)}&amp;gt;
&amp;lt;/GoogleMapReact&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any questions, suggestions are welcome.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Maybe in the future I can explain more about this project&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>carbon</category>
      <category>netlify</category>
      <category>sass</category>
    </item>
    <item>
      <title>Simple auth server to authentication and create user</title>
      <dc:creator>Marcelo</dc:creator>
      <pubDate>Sat, 20 Apr 2024 20:03:42 +0000</pubDate>
      <link>https://forem.com/marcelomsc/simple-auth-server-to-authentication-and-create-user-2j87</link>
      <guid>https://forem.com/marcelomsc/simple-auth-server-to-authentication-and-create-user-2j87</guid>
      <description>&lt;h2&gt;
  
  
  Learning about authentication on Nodejs server I made this project to learn and explore.
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Check the Github repository to install and run&lt;/em&gt;:&lt;br&gt;
&lt;a href="https://github.com/Marcel-MSC/auth-server"&gt;auth-server&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Libraries that a use:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;bcryptjs&lt;/li&gt;
&lt;li&gt;cors&lt;/li&gt;
&lt;li&gt;dotenv&lt;/li&gt;
&lt;li&gt;express&lt;/li&gt;
&lt;li&gt;jsonwebtoken&lt;/li&gt;
&lt;li&gt;lowdb&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  End points:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;/auth -&amp;gt; The auth endpoint that creates a new user record or logs a user based on an existing record&lt;/li&gt;
&lt;li&gt;/verify -&amp;gt; The verify endpoint that checks if a given JWT token is valid&lt;/li&gt;
&lt;li&gt;/check-account -&amp;gt; An endpoint to see if there's an existing account for a given email address&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Explain the app.js
&lt;/h3&gt;

&lt;p&gt;ES6 stand imports. &lt;br&gt;
We have the imports to use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from "express"
import bcrypt from 'bcryptjs'
import cors from 'cors'
import jwt from "jsonwebtoken"
import { JSONFilePreset } from 'lowdb/node'
import 'dotenv/config'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialization of the lowdb, it's basically a JSON file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const defaultData = { users: [] }
let db
async function startLowdb() {
    db = await JSONFilePreset('datatable.json', defaultData)
}
startLowdb()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize Express app and Define a JWT secret key using .env file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const app = express()
const jwtSecretKey = process.env.JWT_SECRET_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A little about CORS:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cross-origin resource sharing (CORS) is a mechanism that allows a web page to access restricted resources from a server on a domain different than the domain that served the web page. &lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing"&gt;Cors Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Middleware:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Middleware is a type of computer software program that provides services to software applications beyond those available from the operating system. It can be described as "software glue".&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Middleware"&gt;Middleware cors&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Set up CORS and JSON middlewares:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basic home route for the API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get("/", (_req, res) =&amp;gt; {
    res.send("Auth API.\nPlease use POST /auth &amp;amp; POST /verify for authentication");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here things get a little more complex.&lt;br&gt;
The auth endpoint that creates a new user record or logs a user based on an existing record.&lt;br&gt;
Don't forget to pass email and password on the post auth.&lt;br&gt;
If authentication success will return a token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/auth", (req, res) =&amp;gt; {
    const { email, password } = req.body
    // Look up the user entry in the database
    const { users } = db.data
    const user = users.filter(user =&amp;gt; email === user.email)
    // If found, compare the hashed passwords and generate the JWT token for the user
    if (user.length === 1) {
        bcrypt.compare(password, user[0].password, function (_err, result) {
            console.log('_err', _err)
            console.log('compare result', result)
            if (!result) {
                console.log('invalid password')
                return res.status(401).json({ message: "Invalid password" });
            } else {
                let loginData = {
                    email,
                    signInTime: Date.now(),
                }
                const token = jwt.sign(loginData, jwtSecretKey)
                res.status(200).json({ message: "authentication success", token })
            }
        })
        // If no user is found, hash the given password and create a new entry in the auth db with the email and hashed password
    } else if (user.length === 0) {
        bcrypt.hash(password, 10, async function (_err, hash) {
            console.log('email e password', { email, password: hash })
            const { users } = db.data
            // db.get("users").push({ email, password: hash }).write()
            db.data.users.push({ email, password: hash })
            await db.write()
            console.log('autorizado com sucesso')
            let loginData = {
                email,
                signInTime: Date.now(),
            }
            const token = jwt.sign(loginData, jwtSecretKey)
            res.status(200).json({ message: "success", token })
        })
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using auth endpoint, you can get a token to be verified.&lt;br&gt;
The verify endpoint that checks if a given JWT token is valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/verify', (req, res) =&amp;gt; {
    const authToken = req.headers.tokenheaderkey;
    console.log('req', req.headers)
    try {
        const verified = jwt.verify(authToken, jwtSecretKey)
        return verified ? res.status(200).json({ status: "logged in", message: "verify with success" }) : res.status(401).json({ status: "invalid auth", message: "error" });
    } catch (error) {
        // Access Denied
        return res.status(401).json({ status: "invalid auth", message: "error" })
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An endpoint to see if there's an existing account for a given email address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/check-account', (req, res) =&amp;gt; {
    const { email } = req.body
    console.log(req.body)
    const { users } = db.data
    const user = users.filter(user =&amp;gt; email === user.email)
    console.log(user)
    res.status(200).json({
        status: user.length === 1 ? "User exists" : "User does not exist", userExists: user.length === 1
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An endpoint to delete a user.&lt;br&gt;
Lowdb don't give a delete/remove function.&lt;br&gt;
Using filer, if the user exists and then update the Users' database.json object with new object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/remove-user', async (req, res) =&amp;gt; {
    const { email } = req.body
    const { users } = db.data;
    const user = users.filter(user =&amp;gt; email === user.email);
    if (user) {
        const newObj = {};
        newObj.users = [];
        const newUsers = users.filter(userDB =&amp;gt; email == userDB.email ? null : userDB);
        newObj.users = newUsers;
        db.data.users = newUsers;
        db.write();
    }
    res.status(200).json({
        status: user.length === 1 ? "User removed" : "User not founded", userExists: user.length === 1
    });
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some application use process.env to set up the port.&lt;br&gt;
You can use what ever port that is free:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.listen(3080, function () {
    console.log('listen to port 3080')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a simple way to understand how to create a simple backend server for authentication and to control users flow on your app. You can check if the user that is logging and if he still has a valid token.&lt;/p&gt;

&lt;p&gt;Of course that more features are needed in a real application.&lt;br&gt;
Some examples to implementation: &lt;br&gt;
connect to another DB, create others validation to user creation, update and delete.&lt;br&gt;
Put a timer on the token to expire, and you can validate if the user's request has a token that is still valid.&lt;/p&gt;

&lt;p&gt;Maybe in the future I will some add more features.&lt;/p&gt;

&lt;p&gt;Feel free to clone, fork, issue or a pull request.&lt;/p&gt;

&lt;p&gt;Any suggestion are welcome.&lt;/p&gt;

</description>
      <category>node</category>
      <category>coding</category>
      <category>api</category>
      <category>jsonwebtoken</category>
    </item>
    <item>
      <title>2d Portfolio like zelda and pokemon old games of Nintendo</title>
      <dc:creator>Marcelo</dc:creator>
      <pubDate>Sat, 13 Apr 2024 16:29:04 +0000</pubDate>
      <link>https://forem.com/marcelomsc/2d-portfolio-like-zelda-and-pokemon-old-games-of-nintendo-4b02</link>
      <guid>https://forem.com/marcelomsc/2d-portfolio-like-zelda-and-pokemon-old-games-of-nintendo-4b02</guid>
      <description>&lt;h2&gt;
  
  
  Recently, I follow a tutorial to build my first game/resume with Kaboom lib.
&lt;/h2&gt;

&lt;p&gt;I follow this tutorial on YouTube:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=gwtfWORCN0U"&gt;https://www.youtube.com/watch?v=gwtfWORCN0U&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was created to understand how a game works and to prove my skills on jobs interview.&lt;/p&gt;

&lt;h2&gt;
  
  
  Feel free to explore:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://2dportfoliomarcelomsc.netlify.app/"&gt;https://2dportfoliomarcelomsc.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The tutorial use the Tiled free app to make a JSON map of the sprites:&lt;br&gt;
&lt;a href="https://www.mapeditor.org/"&gt;https://www.mapeditor.org/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  With this application, we can understand the core concepts of Kaboom.js.
&lt;/h2&gt;

&lt;p&gt;Make scene, player move, show dialog that has links to others websites, boundaries that the player can interactive with.&lt;br&gt;
Set camera, resize function to be mobile and browser compatible.&lt;/p&gt;

&lt;p&gt;Suggestion are welcome.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>kaboom</category>
      <category>vite</category>
      <category>react</category>
    </item>
  </channel>
</rss>
