-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (117 loc) · 3.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import express from 'express'
import dotenv from 'dotenv'
import bodyParser from "body-parser"
import mongoose from 'mongoose';
import {UrlShortner} from './Model/urlShortnerModel.js';
import { User } from './Model/userModel.js';
import { nanoid } from 'nanoid'
import CryptoJS from "crypto-js";
import jwt from "jsonwebtoken"
import cors from 'cors'
const app = express()
dotenv.config()
app.use(bodyParser.json())
app.use(cors())
mongoose.connect(process.env.DB_URL).then(() => {
console.log('🚀🚀 DB Connected')
});
const verifyUser = (req,res,next) => {
const {authorization} = req.headers
try {
if(!authorization) {
return res.json({
msg: "please send jwt token"
})
}
jwt.verify(authorization, process.env.JWT_SECRET)
const username = jwt.decode(authorization)
req.username = username
next()
} catch(err){
console.log(err)
return res.json({
msg: "UnAuthorized. Please login again"
})
}
}
app.post("/short", verifyUser, async (req,res) => {
const {longUrl} = req.body
const newShortUrlUrlShortner = new UrlShortner({
longUrl,
shortUrl: nanoid()
})
const newUrl = await newShortUrlUrlShortner.save()
const userData = await User.findOne({username: req.username.username})
userData.url.push(newUrl._id)
await userData.save()
return res.json({
id : newUrl._id,
shortUrl: newUrl.shortUrl,
longUrl: newUrl.longUrl
})
})
app.get("/geturl/:shortUrl", async (req,res) => {
const {shortUrl} = req.params
const findLongUrl = await UrlShortner.findOne({shortUrl})
if(findLongUrl){
return res.json({
findLongUrl
})
}
else{
return res.json({
msg: "No such short url found"
})
}
})
app.get("/getallurl",verifyUser, async (req,res) => {
const getAllUrl = await User.findOne({username: req.username.username}).populate("url")
return res.json({
getAllUrl : getAllUrl.url.sort((a,b) => b.createdAt - a.createdAt)
})
})
app.post("/signup", async (req,res) => {
const {username, password} = req.body
const encryptPass = CryptoJS.AES.encrypt(password, process.env.CRYPTO_SECRET).toString();
const newUser = new User({username, password: encryptPass, url:[]})
await newUser.save()
return res.json({
username
})
})
app.post("/login", async (req,res) => {
const {username, password} = req.body
if(!username || !password) {
return res.json({
msg: "Enter credentials"
})
}
const findUser = await User.findOne({username})
if(!findUser){
return res.json({
msg: "No such user with username is found"
})
}
const decryptPass = CryptoJS.AES.decrypt(findUser.password, process.env.CRYPTO_SECRET).toString(CryptoJS.enc.Utf8);
if(password !== decryptPass){
return res.json({
msg: "Incorrect password"
})
}
const jwtToken = jwt.sign({
username
}, process.env.JWT_SECRET)
res.cookie("jwtToken", jwtToken)
return res.json({
username,
jwtToken
})
})
app.get("/testing", (req,res) => {
return res.json({
msg: "Working route"
})
})
app.listen(process.env.PORT, () => {
console.log('🚀🚀 Server running on : ', process.env.PORT)
})