Skip to content

Commit bdf48f2

Browse files
committed
adds unregistered viewing config option & implements unregistered view of torrent page
1 parent 15d6c06 commit bdf48f2

6 files changed

Lines changed: 116 additions & 52 deletions

File tree

api/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ validateConfig(config).then(() => {
122122
keyGenerator: (req) => {
123123
if (
124124
req.headers["x-forwarded-for"] &&
125-
req.headers["x-sq-server-secret"] === process.env.SQ_SERVER_SECET
125+
req.headers["x-sq-server-secret"] === process.env.SQ_SERVER_SECRET
126126
)
127127
return req.headers["x-forwarded-for"].split(",")[0];
128128
return req.ip;

api/src/middleware/auth.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const auth = async (req, res, next) => {
2525
} catch (err) {
2626
res.status(500).send(err);
2727
}
28+
} else if (
29+
req.headers["x-sq-public-access"] === "true" &&
30+
req.headers["x-sq-server-secret"] === process.env.SQ_SERVER_SECRET
31+
) {
32+
next();
2833
} else {
2934
res.sendStatus(401);
3035
}

api/src/utils/validateConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const configSchema = yup
3838
}, {});
3939
return yup.object(entries).required();
4040
}),
41+
SQ_ALLOW_UNREGISTERED_VIEW: yup.boolean().required(),
4142
SQ_BASE_URL: yup.string().matches(httpRegex).required(),
4243
SQ_API_URL: yup.string().matches(httpRegex).required(),
4344
SQ_MONGO_URL: yup.string().matches(mongoRegex).required(),

client/pages/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export const getServerSideProps = withAuthServerSideProps(
188188
return { props: {} };
189189
}
190190
},
191+
false,
191192
true
192193
);
193194

client/pages/torrent/[infoHash].js

Lines changed: 86 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
126126
const [comments, setComments] = useState(torrent.comments);
127127
const [isFreeleech, setIsFreeleech] = useState(torrent.freeleech);
128128
const [hasGroup, setHasGroup] = useState(!!torrent.group);
129-
const [bookmarked, setBookmarked] = useState(torrent.fetchedBy.bookmarked);
129+
const [bookmarked, setBookmarked] = useState(torrent.fetchedBy?.bookmarked);
130130

131131
const { addNotification } = useContext(NotificationContext);
132132
const { setLoading } = useContext(LoadingContext);
@@ -477,9 +477,20 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
477477
)}
478478
</Text>
479479
<Box display="flex" alignItems="center" ml={3}>
480-
<Button onClick={handleBookmark} variant="secondary" px="10px" mr={3}>
481-
{bookmarked ? <Bookmark size={18} /> : <BookmarkEmpty size={18} />}
482-
</Button>
480+
{!!userId && (
481+
<Button
482+
onClick={handleBookmark}
483+
variant="secondary"
484+
px="10px"
485+
mr={3}
486+
>
487+
{bookmarked ? (
488+
<Bookmark size={18} />
489+
) : (
490+
<BookmarkEmpty size={18} />
491+
)}
492+
</Button>
493+
)}
483494
{(userRole === "admin" || userId === torrent.uploadedBy._id) && (
484495
<>
485496
<Button
@@ -503,13 +514,19 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
503514
{isFreeleech ? "Unset" : "Set"} freeleech
504515
</Button>
505516
)}
506-
<Button
507-
as="a"
508-
href={`${SQ_API_URL}/torrent/download/${torrent.infoHash}/${uid}`}
509-
target="_blank"
510-
>
511-
Download .torrent
512-
</Button>
517+
{!!userId ? (
518+
<Button
519+
as="a"
520+
href={`${SQ_API_URL}/torrent/download/${torrent.infoHash}/${uid}`}
521+
target="_blank"
522+
>
523+
Download .torrent
524+
</Button>
525+
) : (
526+
<Link href="/login" passHref>
527+
<Button as="a">Log in to download</Button>
528+
</Link>
529+
)}
513530
</Box>
514531
</Box>
515532
<Info
@@ -629,29 +646,43 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
629646
display="flex"
630647
borderBottom="1px solid"
631648
borderColor="border"
632-
pb={5}
649+
pb={userId ? 5 : 0}
633650
mb={5}
634651
>
635-
<Button onClick={() => handleVote("up")} variant="noBackground" mr={2}>
636-
<Text icon={Like} iconColor={userVote === "up" ? "green" : undefined}>
637-
{votes.up || 0}
638-
</Text>
639-
</Button>
640-
<Button
641-
onClick={() => handleVote("down")}
642-
variant="noBackground"
643-
mr={2}
644-
>
645-
<Text
646-
icon={Dislike}
647-
iconColor={userVote === "down" ? "red" : undefined}
648-
>
649-
{votes.down || 0}
650-
</Text>
651-
</Button>
652-
<Button onClick={() => setShowReportModal(true)} variant="noBackground">
653-
Report
654-
</Button>
652+
{!!userId && (
653+
<>
654+
<Button
655+
onClick={() => handleVote("up")}
656+
variant="noBackground"
657+
mr={2}
658+
>
659+
<Text
660+
icon={Like}
661+
iconColor={userVote === "up" ? "green" : undefined}
662+
>
663+
{votes.up || 0}
664+
</Text>
665+
</Button>
666+
<Button
667+
onClick={() => handleVote("down")}
668+
variant="noBackground"
669+
mr={2}
670+
>
671+
<Text
672+
icon={Dislike}
673+
iconColor={userVote === "down" ? "red" : undefined}
674+
>
675+
{votes.down || 0}
676+
</Text>
677+
</Button>
678+
<Button
679+
onClick={() => setShowReportModal(true)}
680+
variant="noBackground"
681+
>
682+
Report
683+
</Button>
684+
</>
685+
)}
655686
</Box>
656687
<Box borderBottom="1px solid" borderColor="border" pb={5} mb={5}>
657688
<Box
@@ -674,11 +705,13 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
674705
Remove this torrent
675706
</Button>
676707
)}
677-
<Link href={`/upload?groupWith=${torrent.infoHash}`} passHref>
678-
<Button as="a" ml={3}>
679-
Add a torrent
680-
</Button>
681-
</Link>
708+
{!!userId && (
709+
<Link href={`/upload?groupWith=${torrent.infoHash}`} passHref>
710+
<Button as="a" ml={3}>
711+
Add a torrent
712+
</Button>
713+
</Link>
714+
)}
682715
</Box>
683716
</Box>
684717
{torrent.groupTorrents.length && hasGroup ? (
@@ -693,15 +726,16 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
693726
<Text as="h2" mb={4}>
694727
Comments
695728
</Text>
696-
<form onSubmit={handleComment}>
729+
<form onSubmit={userId ? handleComment : undefined}>
697730
<Input
698731
ref={commentInputRef}
699732
name="comment"
700733
label="Post a comment"
701734
rows="5"
735+
disabled={!userId}
702736
mb={4}
703737
/>
704-
<Button display="block" ml="auto">
738+
<Button disabled={!userId} display="block" ml="auto">
705739
Post
706740
</Button>
707741
</form>
@@ -780,15 +814,23 @@ const Torrent = ({ token, torrent = {}, userId, userRole, uid }) => {
780814
};
781815

782816
export const getServerSideProps = withAuthServerSideProps(
783-
async ({ token, userId, fetchHeaders, query: { infoHash } }) => {
784-
if (!token) return { props: {} };
817+
async ({
818+
token,
819+
userId,
820+
fetchHeaders,
821+
isPublicAccess,
822+
query: { infoHash },
823+
}) => {
824+
if (!token && !isPublicAccess) return { props: {} };
785825

786826
const {
787827
publicRuntimeConfig: { SQ_API_URL },
788828
serverRuntimeConfig: { SQ_JWT_SECRET },
789829
} = getConfig();
790830

791-
const { id, role } = jwt.verify(token, SQ_JWT_SECRET);
831+
const { id, role } = token
832+
? jwt.verify(token, SQ_JWT_SECRET)
833+
: { id: null, role: null };
792834

793835
try {
794836
const torrentRes = await fetch(`${SQ_API_URL}/torrent/info/${infoHash}`, {
@@ -810,7 +852,8 @@ export const getServerSideProps = withAuthServerSideProps(
810852
if (e === "banned") throw "banned";
811853
return { props: {} };
812854
}
813-
}
855+
},
856+
true
814857
);
815858

816859
export default Torrent;

client/utils/withAuth.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,53 @@ export const withAuth = (Component, noRedirect = false) => {
3030

3131
export const withAuthServerSideProps = (
3232
getServerSideProps,
33+
publicAccess = false,
3334
noRedirect = false
3435
) => {
3536
return async (ctx) => {
36-
const { token, userId } = getReqCookies(ctx.req);
37+
let { token, userId } = getReqCookies(ctx.req);
3738

38-
if (!token && !noRedirect)
39+
const {
40+
serverRuntimeConfig: { SQ_SERVER_SECRET },
41+
publicRuntimeConfig: { SQ_ALLOW_UNREGISTERED_VIEW },
42+
} = getConfig();
43+
44+
const isPublicAccess = publicAccess && SQ_ALLOW_UNREGISTERED_VIEW && !token;
45+
46+
if (!token && !noRedirect && !isPublicAccess)
3947
return {
4048
redirect: {
4149
permanent: false,
4250
destination: "/login",
4351
},
4452
};
4553

46-
if (!token && noRedirect) return { props: {} };
54+
if (!token && noRedirect && !isPublicAccess) return { props: {} };
4755

48-
try {
49-
const {
50-
serverRuntimeConfig: { SQ_SERVER_SECRET },
51-
} = getConfig();
56+
if (isPublicAccess) {
57+
token = null;
58+
userId = null;
59+
}
5260

61+
try {
5362
const fetchHeaders = {
5463
"Content-Type": "application/json",
55-
Authorization: `Bearer ${token}`,
5664
"X-Forwarded-For":
5765
ctx.req.headers["x-forwarded-for"] ?? ctx.req.socket.remoteAddress,
5866
"X-Sq-Server-Secret": SQ_SERVER_SECRET,
67+
"X-Sq-Public-Access": isPublicAccess,
5968
};
6069

70+
if (token) {
71+
fetchHeaders["Authorization"] = `Bearer ${token}`;
72+
}
73+
6174
const { props: ssProps } = await getServerSideProps({
6275
...ctx,
6376
token,
6477
userId,
6578
fetchHeaders,
79+
isPublicAccess,
6680
});
6781
return { props: { ...ssProps, token } };
6882
} catch (e) {

0 commit comments

Comments
 (0)