Skip to content

Commit

Permalink
Rename login cookie, add avatar & templates
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Nov 3, 2023
1 parent 8b5e3c5 commit aa1a852
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 42 deletions.
37 changes: 19 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ app.get("/", (req, res) => {
})

app.get("/servers", async (req, res) => {
if (!req.signedCookies.token) return res.status(401).send("Missing token cookie")
if (!req.signedCookies.auth) return res.status(401).send("Missing auth cookie")

const servers = await oauth.getUserServers(req.signedCookies.token, pool)
const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
console.log(servers)

const filtered = servers.map(server => ({
Expand All @@ -75,9 +75,9 @@ app.get("/servers", async (req, res) => {
})

app.get("/servers/:id/hooks", async (req, res) => {
if (!req.signedCookies.token) return res.status(401).send("Missing token cookie")
if (!req.signedCookies.auth) return res.status(401).send("Missing auth cookie")

const servers = await oauth.getUserServers(req.signedCookies.token, pool)
const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

Expand All @@ -101,9 +101,9 @@ app.get("/servers/:id/hooks", async (req, res) => {
})

app.post("/servers/:id/hooks", async (req, res) => {
if (!req.signedCookies.token) return res.status(401).send({success: false, error: "Missing token cookie"})
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const servers = await oauth.getUserServers(req.signedCookies.token, pool)
const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

Expand All @@ -123,9 +123,9 @@ app.post("/servers/:id/hooks", async (req, res) => {
})

app.post("/servers/:id/hooks/:hook", async (req, res) => {
if (!req.signedCookies.token) return res.status(401).send({success: false, error: "Missing token cookie"})
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const servers = await oauth.getUserServers(req.signedCookies.token, pool)
const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

Expand All @@ -143,12 +143,12 @@ app.post("/servers/:id/hooks/:hook", async (req, res) => {
})

app.delete("/servers/:id/hooks/:hook", async (req, res) => {
if (!req.signedCookies.token) return res.status(401).send({success: false, error: "Missing token cookie"})
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const [rows] = await pool.query("SELECT * FROM `user` WHERE `token` = ?", [req.signedCookies.token])
const [rows] = await pool.query("SELECT * FROM `user` WHERE `token` = ?", [req.signedCookies.auth])
if (rows.length == 0) return res.status(401).send({success: false, error: "Invalid token cookie"})

const servers = await oauth.getUserServers(req.signedCookies.token, pool)
const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

const [rows2] = await pool.query("SELECT * FROM `hook` WHERE `id` = ?", [req.params.hook])
Expand All @@ -162,9 +162,9 @@ app.delete("/servers/:id/hooks/:hook", async (req, res) => {
})

app.post("/servers/:id/hooks/:hook/regen", async (req, res) => {
if (!req.signedCookies.token) return res.status(401).send({success: false, error: "Missing token cookie"})
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const servers = await oauth.getUserServers(req.signedCookies.token, pool)
const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

Expand All @@ -185,7 +185,7 @@ app.get("/login", async (req, res) => {
const body = {
client_id: botId,
client_secret: botSecret,
redirect_uri: "https://" + domain + "/api/oauth",
redirect_uri: "http://localhost:3000/api/oauth", //"https://" + domain + "/api/oauth",
grant_type: "authorization_code",
code: req.query.code
}
Expand Down Expand Up @@ -214,7 +214,8 @@ app.get("/login", async (req, res) => {
console.log(user)

const token = oauth.generateToken()
res.cookie("token", token, {signed: true, secure: true, httpOnly: true, expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7), domain: "." + domain.split(".").slice(-2).join(".")})
res.cookie("auth", token, {signed: true, secure: true, httpOnly: true, expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 4), domain: "." + domain.split(".").slice(-2).join(".")})
res.cookie("avatar", "https://cdn.discordapp.com/avatars/" + user.id + "/" + user.avatar + ".png", {secure: true, expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 4), domain: "." + domain.split(".").slice(-2).join(".")})

pool.query(
"INSERT INTO `user` (`id`, `token`, `access`, `refresh`, `expires`) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `token` = ?, `access` = ?, `refresh` = ?, `expires` = ?",
Expand All @@ -224,12 +225,12 @@ app.get("/login", async (req, res) => {
res.send({token})
})
app.get("/logout", (req, res) => {
if (!req.signedCookies.token) return res.status(401).send("Missing token cookie")
if (!req.signedCookies.auth) return res.status(401).send("Missing auth cookie")

res.clearCookie("token", {domain: "." + domain.split(".").slice(-2).join(".")})
res.clearCookie("auth", {domain: "." + domain.split(".").slice(-2).join(".")})
res.send({success: true})

pool.query("DELETE FROM `user` WHERE `token` = ?", [req.signedCookies.token])
pool.query("DELETE FROM `user` WHERE `token` = ?", [req.signedCookies.auth])
})

// - Hooks -
Expand Down
4 changes: 2 additions & 2 deletions templates/gollum.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `gollum`",
title: "[{{ repository.name }}] **{{ count(pages) }}** wiki pages updated",
url: "{{ repository.html_url }}",
color: color("black")
color: color("cyan")
}]
}
]
15 changes: 12 additions & 3 deletions templates/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `member` (`added`)",
title: "[{{ repository.name }}] Added **{{ member.login }}** as member",
url: "{{ repository.html_url }}",
thumbnail: {
url: "{{ member.avatar_url }}"
},
color: color("black")
}]
},{
Expand All @@ -19,8 +22,11 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `member` (`edited`)",
title: "[{{ repository.name }}] Edited **{{ member.login }}**'s member permissions",
url: "{{ repository.html_url }}",
thumbnail: {
url: "{{ member.avatar_url }}"
},
color: color("cyan")
}]
},{
Expand All @@ -30,8 +36,11 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `member` (`removed`)",
title: "[{{ repository.name }}] Removed **{{ member.login }}** as member",
url: "{{ repository.html_url }}",
thumbnail: {
url: "{{ member.avatar_url }}"
},
color: color("black")
}]
}
Expand Down
12 changes: 6 additions & 6 deletions templates/membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `membership` (`added`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ team.name }}] Added **{{ member.login }}** as member",
url: "{{ team.html_url }}",
color: color("green")
}]
},{
action: "removed",
Expand All @@ -19,9 +19,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `membership` (`removed`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ team.name }}] Removed **{{ member.login }}** as member",
url: "{{ team.html_url }}",
color: color("red")
}]
}
]
14 changes: 7 additions & 7 deletions templates/pull_request_review.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] Pull Request review dismissed",
url: "{{ pull_request.review_comment_url }}",
color: color("red")
title: "[{{ repository.name }}] Review on Pull Request **#{{ pull_request.number }}** dismissed",
url: "{{ review.html_url }}",
color: color("magenta")
}]
},{
action: "edited",
Expand All @@ -19,8 +19,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] Pull Request review edited",
url: "{{ pull_request.review_comment_url }}",
title: "[{{ repository.name }}] Review on Pull Request **#{{ pull_request.number }}** edited",
url: "{{ review.html_url }}",
color: color("cyan")
}]
},{
Expand All @@ -30,8 +30,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `pull_request_review` (`submitted`)",
url: "{{ pull_request.review_comment_url }}",
title: "[{{ repository.name }}] Review on Pull Request **#{{ pull_request.number }}** submitted",
url: "{{ review.html_url }}",
color: color("green")
}]
}
Expand Down
8 changes: 4 additions & 4 deletions templates/workflow_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}] Workflow **{{ workflow.name }}** {{ workflow_job.conclusion }}",
title: "[{{ repository.name }}] Workflow **{{ workflow_job.name }}** {{ workflow_job.conclusion }}",
url: "{{ workflow_job.html_url }}",
color: color("black")
}]
Expand All @@ -19,7 +19,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}] Workflow **{{ workflow.name }}** {{ workflow_job.status }}",
title: "[{{ repository.name }}] Workflow **{{ workflow_job.name }}** {{ workflow_job.status }}",
url: "{{ workflow_job.html_url }}",
color: color("black")
}]
Expand All @@ -30,7 +30,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}] Workflow **{{ workflow.name }}** queued",
title: "[{{ repository.name }}] Workflow **{{ workflow_job.name }}** queued",
url: "{{ workflow_job.html_url }}",
color: color("gray")
}]
Expand All @@ -41,7 +41,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}] Workflow **{{ workflow.name }}** waiting",
title: "[{{ repository.name }}] Workflow **{{ workflow_job.name }}** waiting",
url: "{{ workflow_job.html_url }}",
color: color("gray")
}]
Expand Down
3 changes: 1 addition & 2 deletions templates/workflow_run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ workflow_run.head_repository.name }}:{{ workflow_run.head_branch }}] Workflow **{{ workflow.name }}** finished",
title: "[{{ workflow_run.head_repository.name }}:{{ workflow_run.head_branch }}] Workflow **{{ workflow.name }}** {{ workflow_run.conclusion }}",
url: "{{ workflow_run.html_url }}",
description: "{{ workflow_run.conclusion }}",
color: color("magenta")
}]
},{
Expand Down

0 comments on commit aa1a852

Please sign in to comment.