Skip to content

Commit 50146b1

Browse files
committed
feat: Add Blocked page
Tell users why they can't access the site Signed-off-by: Clayton Burlison <git@clburlison.com>
1 parent 635f5f1 commit 50146b1

5 files changed

Lines changed: 121 additions & 3 deletions

File tree

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ app.use(async (req, res, next) => {
133133
(
134134
req.path === '/api/discord/login' ||
135135
req.path === '/login' ||
136+
req.path === '/blocked' ||
136137
(config.homePage && req.path === '/home')
137138
)
138139
) {

src/routes/discord.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@ router.get('/callback', catchAsyncErrors(async (req, res) => {
4343
req.session.username = `${user.username}#${user.discriminator}`;
4444
const perms = await DiscordClient.getPerms(user);
4545
req.session.perms = perms;
46+
const blocked = perms.blocked;
4647
const valid = perms.map !== false;
4748
req.session.valid = valid;
4849
req.session.save();
4950
if (valid) {
5051
console.log(user.id, 'Authenticated successfully.');
5152
await DiscordClient.sendMessage(config.discord.logChannelId, `${user.username}#${user.discriminator} (${user.id}) Authenticated successfully.`);
5253
res.redirect(`/?token=${response.data.access_token}`);
54+
} else if (blocked) {
55+
// User is in blocked Discord server(s)
56+
console.warn(user.id, 'Blocked due to', blocked);
57+
await DiscordClient.sendMessage(config.discord.logChannelId, `${user.username}#${user.discriminator} (${user.id}) Blocked due to ${blocked}.`);
58+
res.redirect('/blocked');
5359
} else {
5460
// Not in Discord server(s) and/or have required roles to view map
5561
console.warn(user.id, 'Not authorized to access map');

src/routes/ui.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ if (config.homePage) {
4040
});
4141
}
4242

43+
router.get('/blocked', (req, res) => {
44+
const data = {};
45+
data.discord_invite = config.discord.invite;
46+
if (req.session.username) {
47+
data.guild_name = req.session.perms.blocked;
48+
data.username = req.session.username;
49+
}
50+
res.render('blocked', data);
51+
});
52+
4353
// Location endpoints
4454
router.get('/@/:lat/:lon', async (req, res) => {
4555
res.setHeader('Content-Type', 'text/html');

src/services/discord.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class DiscordClient {
3737
async getGuilds() {
3838
const guilds = await oauth.getUserGuilds(this.accessToken);
3939
const guildIds = Array.from(guilds, x => BigInt(x.id).toString());
40-
return guildIds;
40+
return [guildIds, guilds];
4141
}
4242

4343
async getUserRoles(guildId, userId) {
@@ -74,7 +74,7 @@ class DiscordClient {
7474
}
7575

7676
async getPerms(user) {
77-
const perms = {
77+
var perms = {
7878
map: false,
7979
pokemon: false,
8080
raids: false,
@@ -94,7 +94,7 @@ class DiscordClient {
9494
weather: false,
9595
devices: false
9696
};
97-
const guilds = await this.getGuilds();
97+
const [guilds, guildsFull] = await this.getGuilds();
9898
if (config.discord.allowedUsers.includes(user.id)) {
9999
Object.keys(perms).forEach((key) => perms[key] = true);
100100
console.log(`User ${user.username}#${user.discriminator} (${user.id}) in allowed users list, skipping guild and role check.`);
@@ -108,6 +108,7 @@ class DiscordClient {
108108
if (guilds.includes(guildId)) {
109109
// If so, user is not granted access
110110
blocked = true;
111+
perms["blocked"] = guildsFull.find(x => x.id === guildId).name;
111112
break;
112113
}
113114
}

src/views/blocked.mustache

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1">
5+
<style>
6+
html,
7+
body {
8+
height: 85%;
9+
width: 100%;
10+
margin: 0px;
11+
background-color: #2c2f33;
12+
background-image: url('/img/discord.png');
13+
}
14+
15+
.background {
16+
background-image:url("/img/landing.png");
17+
background-position: center;
18+
background-repeat: no-repeat;
19+
background-size: cover;
20+
height: 95%;
21+
border: 2px solid #7289da;
22+
display: block;
23+
margin-left: auto;
24+
margin-right: auto;
25+
}
26+
27+
#transbox {
28+
background-color:rgba(255,255,255,0.8);
29+
padding:20px;
30+
}
31+
32+
.title {
33+
text-align:center;
34+
font-size: 2em;
35+
color: #3C5FA3;
36+
}
37+
38+
.container {
39+
width: 90%;
40+
margin: 0 auto;
41+
padding: 30px;
42+
}
43+
44+
.clearfix:after {
45+
visibility:hidden;
46+
display:block;
47+
content:"";
48+
clear:both;
49+
height:0;
50+
}
51+
52+
.description {
53+
text-align:center;
54+
opacity: 0.5;
55+
color: #000000;
56+
margin-left: 10px;
57+
margin-bottom: 10px;
58+
white-space: pre-wrap;
59+
}
60+
61+
.make-center {
62+
text-align: center;
63+
padding: 20px 0;
64+
}
65+
66+
.square_btn{
67+
display: inline-block;
68+
padding: 7px 20px;
69+
border-radius: 25px;
70+
text-decoration: none;
71+
color: #FFF;
72+
background-image: -webkit-linear-gradient(45deg, #7289da 0%, #4d6cdb 100%);
73+
background-image: linear-gradient(45deg, #7289da 0%, #4d6cdb 100%);
74+
transition: .4s;
75+
}
76+
77+
.square_btn:hover {
78+
background-image: -webkit-linear-gradient(45deg, #7289da 0%, #3659d8 100%);
79+
background-image: linear-gradient(45deg, #7289da 0%, #3659d8 100%);
80+
}
81+
</style>
82+
</head>
83+
<body>
84+
<div class="container clearfix background">
85+
<div id="transbox">
86+
<title>Access denied</title>
87+
<div class="title">Access denied!</div>
88+
<div class="description">{{username}} has been blocked for being a member of {{guild_name}}.
89+
Please join our discord for more info.</div>
90+
<div class="make-center">
91+
<a href="/login" class="square_btn">Login</a><a href="{{discord_invite}}" class="square_btn">Join Our Discord</a>
92+
</div>
93+
</div>
94+
</div>
95+
</body>
96+
</body>
97+
</html>
98+
99+
<!--created by anonymous All images are copyright and belong to their respective owners,
100+
this document is for educational purpose-->

0 commit comments

Comments
 (0)