Skip to content

Instantly share code, notes, and snippets.

View mirsahib's full-sized avatar
🎯
Focusing

Mir Habib Ul Latif 🇧🇩 🇵🇸 mirsahib

🎯
Focusing
View GitHub Profile
@mirsahib
mirsahib / jwt-helpers.js
Created November 29, 2020 12:55
React_Serverless
import jwt from "jsonwebtoken";
import cookie from "cookie";
function createJwtCookie(userId, email) {
const token = jwt.sign({ userId, email }, process.env.REACT_APP_JWT_TOKEN, {
expiresIn: "10 days",
});
const jwtCookie = cookie.serialize("jwt", token, {
secure: process.env.NETLIFY_DEV !== "true",
httpOnly: true,
@mirsahib
mirsahib / db-helper.js
Created November 29, 2020 04:20
React_Serverless
import { MongoClient } from "mongodb";
function createClient() {
const client = new MongoClient(
`mongodb+srv://admin:${process.env.REACT_APP_DB_PASSWORD}@cluster0.si7bk.mongodb.net/test?retryWrites=true&w=majority`,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err) => {
@mirsahib
mirsahib / user.js
Created November 28, 2020 15:15
React_Serverless
import cookie from "cookie";
import jwt from "jsonwebtoken";
export async function handler(event) {
const cookies = event.headers.cookie && cookie.parse(event.headers.cookie);
if (!cookies || !cookies.jwt) {
return {
statusCode: 401,
body: JSON.stringify({
msg: "Unauthorized,JWT cookie is missing",
@mirsahib
mirsahib / auth.js
Last active November 28, 2020 15:13
React_Serverless
import cookie from "cookie";
import jwt from "jsonwebtoken";
export async function handler(event) {
const cookies = event.headers.cookie && cookie.parse(event.headers.cookie);
if (!cookies || !cookies.jwt) {
return {
statusCode: 401,
body: JSON.stringify({
auth: false,
@mirsahib
mirsahib / logout.js
Created November 28, 2020 15:10
React_Serverless
import { clearCookie } from "../helpers/jwt-helper";
export async function handler() {
return {
statusCode: 200,
headers: {
"Set-Cookie": clearCookie(),
"Content-Type": "application/json",
},
body: JSON.stringify({ message: "Logged out successfully" }),
@mirsahib
mirsahib / login.js
Created November 28, 2020 15:06
React_Serverless
import bcrypt from "bcryptjs";
import { JsonWebTokenError } from "jsonwebtoken";
import { createClient } from "../helpers/db-helper";
import { createJwtCookie } from "../helpers/jwt-helper";
export async function handler(event) {
const dbClient = createClient();
let errorStatusCode = 500;
try {
@mirsahib
mirsahib / signup.js
Created November 28, 2020 15:03
React_Serverless
import bcrypt from "bcryptjs";
import { createClient } from "../helpers/db-helper";
import { createJwtCookie } from "../helpers/jwt-helper";
export async function handler(event) {
const dbClient = createClient();
let errorStatusCode = 500;
try {
await dbClient.connect();
import React, { useState } from "react";
import Header from "./components/header";
import SearchBar from "./components/SearchBar";
import Home from "./components/Home";
import Footer from "./components/footer";
function App() {
const [cityName, setCityName] = useState("");
//function to track change in the search bar
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.