Skip to content

Commit

Permalink
Merge pull request #37 from Pritam0077/main
Browse files Browse the repository at this point in the history
uploaded the files
  • Loading branch information
saniya3 authored Oct 17, 2021
2 parents 41931a5 + 603387e commit 1beae49
Show file tree
Hide file tree
Showing 5 changed files with 2,000 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Url Shortener/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Url Shortener</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css" integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm" crossorigin="anonymous">
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
/>
<script
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"
></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"
></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
function getShort(event){
event.preventDefault();

const email=document.getElementById("email").value;
const password=document.getElementById("password").value;
const short=document.getElementById("short").value;
const Url=document.getElementById("Url").value;

const data={email,password,short,Url}



axios.post("/admin/urls/", data).then(response=>{
alert("Generated");
}).catch(err =>{
alert("Failed");
})

// console.log(data);
}
</script>
</head>
<body>
<form onsubmit="genShort(event)">
<div class="form-group">
<label for="email">Email address</label>
<input
type="email"
class="form-control"
id="email"
aria-describedby="emailHelp"
placeholder="Enter email"
/>
<small id="emailHelp" class="form-text text-muted"
>We'll never share your email with anyone else.</small
>
</div>
<div class="form-group">
<label for="password">Password</label>
<input
type="password"
class="form-control"
id="password"
placeholder="Enter password"
/>
</div>
<div class="form-group">
<label for="short">Short</label>
<input
type="text"
class="form-control"
id="short"
placeholder="Enter short url"
/>
</div>
<div class="form-group">
<label for="Url">Complete Url</label>
<input
type="text"
class="form-control"
id="Url"
placeholder="Enter complete url"
/>
</div>

<button type="button" class="btn btn-warning"><i class="fas fa-heart"></i> Submit</button>
</form>
</body>
</html>
67 changes: 67 additions & 0 deletions Url Shortener/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const express = require('express');
const bodyParser = require("body-parser");
const app = express();
const port = 3000;

var admin = require("firebase-admin");

var serviceAccount = require("./urlshorten-690ae-firebase-adminsdk-w0usc-fc391b70eb.json");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});


const static=express.static("public");
const urldb = admin.firestore().collection("urldb");
const usersdb = admin.firestore().collection("usersdb");

app.use(static);
app.use(bodyParser.json());

// app.use((req,res,next)=>{
// res.send("We intercepted the req")
// })

app.get("/:short", (req, res) => {
console.log(req.params);
const short=req.params.short;

const doc=urldb.doc(short);

doc.get().then(response => {
const data = response.data();
// console.log(data);
if(data && data.url){
res.redirect(301, data.url);
} else {
res.redirect(301, "https://www.codewithharry.com/");
}
})

// res.send("We will redirect you to " + short)
});


app.post("/admin/urls/", (req, res) => {
const {email, password, short, url} = req.body;

usersdb.doc(email).get().then(response=>{
const user = response.data();
// console.log(user);

if(user && (user.email == email) && (user.password == password)){
const doc = urlsdb.doc(short);
doc.set({url});
res.send("Done")
} else {
res.send(403, "Not possible")
}
})

// res.send("Hello from another!");
});

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
Loading

0 comments on commit 1beae49

Please sign in to comment.