<?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: Yusuf Ginanjar</title>
    <description>The latest articles on Forem by Yusuf Ginanjar (@yusufginanjar).</description>
    <link>https://forem.com/yusufginanjar</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%2F955558%2F5167f94b-5de1-4bf4-bde0-98328354c91a.jpeg</url>
      <title>Forem: Yusuf Ginanjar</title>
      <link>https://forem.com/yusufginanjar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yusufginanjar"/>
    <language>en</language>
    <item>
      <title>Implementing Google Login with Redirect-Based Approach in ReactJS</title>
      <dc:creator>Yusuf Ginanjar</dc:creator>
      <pubDate>Sun, 19 May 2024 16:46:42 +0000</pubDate>
      <link>https://forem.com/yusufginanjar/implementing-google-login-with-redirect-based-approach-in-reactjs-3711</link>
      <guid>https://forem.com/yusufginanjar/implementing-google-login-with-redirect-based-approach-in-reactjs-3711</guid>
      <description>&lt;p&gt;Social logins, like login with Google, can significantly improve the user experience. By offering a simpler registration and login process, social logins reduce friction and make it easier for users to access your application. Many users, myself included, prefer social authorization over manual registration because it saves time and eliminates the need to remember yet another username and password.&lt;/p&gt;

&lt;p&gt;OAuth 2.0 is a protocol that enables applications to securely access resources (like user information) on a user's behalf with their consent. &lt;/p&gt;

&lt;p&gt;Here are the steps involved in developing login with Google using the redirect-based approach:&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;If you want to see the full source code, you can visit this repository:&lt;br&gt;
&lt;a href="https://github.com/yusufginanjar/minimal-google-login" rel="noopener noreferrer"&gt;https://github.com/yusufginanjar/minimal-google-login&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. React Js Implementation
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Create route /login to access Login.jsx and /auth/success to access LoginSuccess.jsx
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  return (
    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;Layout /&amp;gt;}&amp;gt;
          &amp;lt;Route path="login" element={&amp;lt;Login /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="auth/success" element={&amp;lt;LoginSuccess /&amp;gt;} /&amp;gt;
        &amp;lt;/Route&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Install Packages
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-dom axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create a React component Login.jsx to handle the login functionality:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { Link } from 'react-router-dom';

const clientId = 'YOUR_GOOGLE_CLIENT_ID'; // Replace with your actual client ID
const redirectUri = 'http://localhost:5000/callback'; // Replace with your redirect URI

const Login = () =&amp;gt; {
  const handleLogin = () =&amp;gt; {
    const googleAuthUrl = `https://accounts.google.com/o/oauth2/v2/auth?client_id=&amp;lt;span class="math-inline"&amp;gt;\{clientId\}&amp;amp;redirect\_uri\=&amp;lt;/span&amp;gt;{redirectUri}&amp;amp;response_type=code&amp;amp;scope=openid%20profile%20email`;
    window.location.href = googleAuthUrl;
  };

  return (
    &amp;lt;div className="login-container"&amp;gt;
      &amp;lt;h1&amp;gt;Login with Google&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={handleLogin}&amp;gt;Sign in with Google&amp;lt;/button&amp;gt;
      &amp;lt;p&amp;gt;
        By clicking "Sign in with Google," you agree to our terms of service and privacy policy.
      &amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Login;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create a React component LoginSuccess.jsx to handle the login success and catch the token:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect } from "react";

const LoginSuccess = (props) =&amp;gt; {
    useEffect(() =&amp;gt; {
        const token = new URLSearchParams(window.location.search).get("token");
        if (!token) {
            window.location.href = "/login";
        }
        localStorage.setItem("TOKEN", token);
                console.log(token);
    }, []);

    return (
         &amp;lt;div&amp;gt;Login Successful&amp;lt;/div&amp;gt;
    );
};

export default LoginSuccess;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Backend Server Setup (Node.js with Express):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const axios = require('axios');
const app = express();

const clientId = 'YOUR_GOOGLE_CLIENT_ID';
const clientSecret = 'YOUR_GOOGLE_CLIENT_SECRET'; // Replace with your actual client secret
const redirectUri = 'http://localhost:5000/callback';

app.get('/callback', async (req, res) =&amp;gt; {
  const code = req.query.code;

  try {
    const response = await axios.post('https://oauth2.googleapis.com/token', {
      code,
      client_id: clientId,
      client_secret: clientSecret,
      redirect_uri: redirectUri,
      grant_type: 'authorization_code'
    });

    const tokens = response.data;
    res.redirect(`http://localhost:3000/auth/success?token=${tokens}`); // Replace with your success route if needed
  } catch (error) {
    console.error(error);
    res.status(500).send('Authentication failed');
  }
});


app.listen(5000, () =&amp;gt; {
  console.log('Server running on http://localhost:5000');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This approach offers a smooth integration with Google's authentication services, making it valuable tool for enhancing user experience in your ReactJs applications. Feel free to explore additional features to further enrich your application's functionality.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;This article was created by the assistance of Gemini and ChatGPT.&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Create a Fancy Carousel in ReactJs</title>
      <dc:creator>Yusuf Ginanjar</dc:creator>
      <pubDate>Sat, 18 May 2024 17:03:45 +0000</pubDate>
      <link>https://forem.com/yusufginanjar/how-to-create-a-fancy-sweeper-in-reactjs-e0m</link>
      <guid>https://forem.com/yusufginanjar/how-to-create-a-fancy-sweeper-in-reactjs-e0m</guid>
      <description>&lt;p&gt;Finding a non-boring carousel can sometimes be a challenge. Here, we'll introduce you to a fancy carousel with swiper library that you can integrate into your ReactJs application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Code
&lt;/h2&gt;

&lt;p&gt;We will break down the code step-by-step to guide you through the creation process.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install Swiper
&lt;/h3&gt;

&lt;p&gt;The first step involves installing the sweeper library using npm or yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i swiper
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Import component and styles
&lt;/h3&gt;

&lt;p&gt;Here, we import necessary components and styles from the swiper/react library and its CSS modules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import "swiper/css/effect-coverflow";
import "swiper/css/pagination";
import "swiper/css/navigation";
import "swiper/css/autoplay";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Create Sweeper Component
&lt;/h3&gt;

&lt;p&gt;This section defines SwiperComponent which holds the configuration and content for the carousel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SwiperComponent.jsx
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import "swiper/css/effect-coverflow";
import "swiper/css/pagination";
import "swiper/css/navigation";
import "swiper/css/autoplay";
import "./styles.css";
import {
    Autoplay,
    EffectCoverflow,
    Pagination,
    Navigation,
} from "swiper/modules";

const SwiperComponent = () =&amp;gt; {
    return (
        &amp;lt;Swiper
            effect={"coverflow"}
            grabCursor={true}
            centeredSlides={true}
            slidesPerView={"auto"}
            loop={true}
            coverflowEffect={{
                rotate: 50,
                stretch: 0,
                depth: 100,
                modifier: 1,
                slideShadows: true,
            }}
            autoplay={{
                delay: 3000,
                disableOnInteraction: false,
            }}
            pagination={{
                clickable: true,
            }}
            navigation={true}
            modules={[Autoplay, EffectCoverflow, Pagination, Navigation]}
            className="mySwiper"&amp;gt;
            &amp;lt;SwiperSlide&amp;gt;
                &amp;lt;img src="https://via.placeholder.com/300" alt="slide 1" /&amp;gt;
            &amp;lt;/SwiperSlide&amp;gt;
            &amp;lt;SwiperSlide&amp;gt;
                &amp;lt;img src="https://via.placeholder.com/300" alt="slide 2" /&amp;gt;
            &amp;lt;/SwiperSlide&amp;gt;
            &amp;lt;SwiperSlide&amp;gt;
                &amp;lt;img src="https://via.placeholder.com/300" alt="slide 3" /&amp;gt;
            &amp;lt;/SwiperSlide&amp;gt;
            &amp;lt;SwiperSlide&amp;gt;
                &amp;lt;img src="https://via.placeholder.com/300" alt="slide 4" /&amp;gt;
            &amp;lt;/SwiperSlide&amp;gt;
            &amp;lt;SwiperSlide&amp;gt;
                &amp;lt;img src="https://via.placeholder.com/300" alt="slide 5" /&amp;gt;
            &amp;lt;/SwiperSlide&amp;gt;
        &amp;lt;/Swiper&amp;gt;
    );
};

export default SwiperComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Add style.css
&lt;/h3&gt;

&lt;p&gt;This section defines custom CSS styles for the carousel. You can customize the appearance to match your application's design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#app {
    height: 100%
}

html,
body {
    position: relative;
    height: 100%;
}

body {
    background: #eee;
    font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
    font-size: 14px;
    color: #000;
    margin: 0;
    padding: 0;
}

.swiper {
    width: 100%;
    padding-top: 50px;
    padding-bottom: 50px;
}

.swiper-slide {
    background-position: center;
    background-size: cover;
    width: 300px;
}

.swiper-slide img {
    display: block;
    width: 100%;
    border-radius: 13px;
}

.mySwiper .swiper-slide-active img {
    border: 10px solid #E4E4E4;
    position: relative;
    right: 10px;
}

.swiper-button-prev,
.swiper-button-next {
    color: #333;
    background-color: rgba(255, 255, 255, 0.5);
    padding: 10px;
    height: 50px;
    width: 50px;
    border-radius: 50%;
    box-shadow: 2px 2px 5px #c8c8c8;
}

.mySwiper .swiper-button-prev {
    left: 50%;
    transform: translate(-150px);
}

.mySwiper .swiper-button-next {
    right: 50%;
    transform: translate(150px);
}


.swiper-button-prev::after,
.swiper-button-next::after {
    font-size: 20px;
}

/* md screen */
@media (min-width: 768px) {
    .swiper-button-prev {
        transform: translate(-200px);
    }

    .swiper-button-next {
        transform: translate(200px);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Use the Sweeper component in your App
&lt;/h3&gt;

&lt;p&gt;Finally, we integrate the SwiperComponent into your main React application component (usually App.jsx):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.jsx
import React from 'react';
import SwiperComponent from './SwiperComponent';

const App = () =&amp;gt; {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;h1&amp;gt;React Swiper with Coverflow Effect&amp;lt;/h1&amp;gt;
      &amp;lt;SwiperComponent /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following these steps, you'll be able to create a visually appealing and interactive carousel with a coverflow effect in your ReactJs application. Remember to replace the placeholder images with your desired content and customize style further to match your application's design.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was created with the assistance of Gemini and ChatGPT.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>carousel</category>
      <category>ui</category>
    </item>
    <item>
      <title>How to Implement QR Scanner in ReactJs</title>
      <dc:creator>Yusuf Ginanjar</dc:creator>
      <pubDate>Fri, 17 May 2024 16:24:36 +0000</pubDate>
      <link>https://forem.com/yusufginanjar/how-to-implement-qr-scanner-in-reactjs-ao7</link>
      <guid>https://forem.com/yusufginanjar/how-to-implement-qr-scanner-in-reactjs-ao7</guid>
      <description>&lt;p&gt;Imagine a QR code as a fancy barcode that holds information like website addresses or secret messages. When you scan a QR code with your phone or a scanner app, it's like using a special translator. The scanner app reads the squiggles and lines of the QR code and turns them back into regular words and numbers that your phone can understand.&lt;/p&gt;

&lt;p&gt;Here's a step-by-step breakdown:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Secret message in a picture: Information gets squeezed into a special picture called a QR code.&lt;/li&gt;
&lt;li&gt;Scanning with your camera: You point your phone's camera at the QR code, like taking a picture.&lt;/li&gt;
&lt;li&gt;Translating the squiggles: A scanner app on your phone reads the lines and squares of the QR code.&lt;/li&gt;
&lt;li&gt;Getting the message: The app figures out what information is hidden in the QR code.&lt;/li&gt;
&lt;li&gt;Taking action: Your React app receives the information and does something cool with it, like showing a website or logging you into something.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;In this tutorial, we'll walk you through building a QR code scanner using ReactJS.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Install qr-scanner
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i qr-scanner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn i qr-scanner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Create Component QrReader and write code below
&lt;/h2&gt;

&lt;p&gt;Header&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useRef, useState } from "react";
import "./QrStyles.css";
import QrScanner from "qr-scanner";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const QrReader = () =&amp;gt; {
    // QR States
    const scanner = useRef&amp;lt;QrScanner&amp;gt;();
    const videoEl = useRef&amp;lt;HTMLVideoElement&amp;gt;(null);
    const qrBoxEl = useRef&amp;lt;HTMLDivElement&amp;gt;(null);
    const [qrOn, setQrOn] = useState&amp;lt;boolean&amp;gt;(true);

    // Result
    const [scannedResult, setScannedResult] = useState&amp;lt;string | undefined&amp;gt;("");

    // Success
    const onScanSuccess = (result: QrScanner.ScanResult) =&amp;gt; {
        // 🖨 Print the "result" to browser console.
        console.log(result);
        // ✅ Handle success.
        // 😎 You can do whatever you want with the scanned result.
        setScannedResult(result?.data);
    };

    // Fail
    const onScanFail = (err: string | Error) =&amp;gt; {
        // 🖨 Print the "err" to browser console.
        console.log(err);
    };

    useEffect(() =&amp;gt; {
        if (videoEl?.current &amp;amp;&amp;amp; !scanner.current) {
            // 👉 Instantiate the QR Scanner
            scanner.current = new QrScanner(videoEl?.current, onScanSuccess, {
                onDecodeError: onScanFail,
                // 📷 This is the camera facing mode. In mobile devices, "environment" means back camera and "user" means front camera.
                preferredCamera: "environment",
                // 🖼 This will help us position our "QrFrame.svg" so that user can only scan when qr code is put in between our QrFrame.svg.
                highlightScanRegion: true,
                // 🔥 This will produce a yellow (default color) outline around the qr code that we scan, showing a proof that our qr-scanner is scanning that qr code.
                highlightCodeOutline: true,
                // 📦 A custom div which will pair with "highlightScanRegion" option above 👆. This gives us full control over our scan region.
                overlay: qrBoxEl?.current || undefined,
            });

            // 🚀 Start QR Scanner
            scanner?.current
                ?.start()
                .then(() =&amp;gt; setQrOn(true))
                .catch((err) =&amp;gt; {
                    if (err) setQrOn(false);
                });
        }

        // 🧹 Clean up on unmount.
        // 🚨 This removes the QR Scanner from rendering and using camera when it is closed or removed from the UI.
        return () =&amp;gt; {
            if (!videoEl?.current) {
                scanner?.current?.stop();
            }
        };
    }, []);

    // ❌ If "camera" is not allowed in browser permissions, show an alert.
    useEffect(() =&amp;gt; {
        if (!qrOn)
            alert(
                "Camera is blocked or not accessible. Please allow camera in your browser permissions and Reload."
            );
    }, [qrOn]);

    return (
        &amp;lt;div className="qr-reader"&amp;gt;
            {/* QR */}
            &amp;lt;video ref={videoEl}&amp;gt;&amp;lt;/video&amp;gt;
            &amp;lt;div ref={qrBoxEl} className="qr-box"&amp;gt;
                {!videoEl?.current &amp;amp;&amp;amp; (
                    &amp;lt;img
                        src="/static/images/icons/scan_qr1.svg"
                        alt="Qr Frame"
                        width={256}
                        height={256}
                        className="qr-frame"
                    /&amp;gt;
                )}
            &amp;lt;/div&amp;gt;

            {/* Show Data Result if scan is success */}
            {scannedResult &amp;amp;&amp;amp; (
                &amp;lt;p
                    style={{
                        position: "absolute",
                        top: 0,
                        left: 0,
                        zIndex: 99999,
                        color: "white",
                    }}&amp;gt;
                    Scanned Result: {scannedResult}
                &amp;lt;/p&amp;gt;
            )}
        &amp;lt;/div&amp;gt;
    );
};

export default QrReader;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Style: Create QrStyles.css on the same folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.qr-reader {
    width: 430px;
    height: 430px;
    margin: 0 auto;
    position: relative;
}

.qr-reader video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.qr-reader .qr-box {
    width: 100% !important;
    left: 0 !important;
}

.qr-box.scan-region-highlight {
    height: 0px !important;
}

.qr-reader .qr-frame {
    position: absolute;
    fill: none;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(0);
}

/* Media Queries for mobile screens */
@media (max-width: 426px) {
    .qr-reader {
        width: 100%;
        height: 76vw;
    }

    .qr-reader .qr-frame {
        transform: translateX(-50%) translateY(-10%);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations! You've built a basic QR code scanner using ReactJS. This is a powerful tool that you can integrate into your React applications to enable features like attendance tracking, product verification, or even hidden message decoding in games. With some additional development, you can customize the scanner's appearance, handle different scan results, and integrate it seamlessly into your React projects. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was created with the assistance of Gemini.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
