Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
IITII committed Jan 29, 2024
2 parents 3db1e34 + 1efe8c9 commit 51b9363
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 16 deletions.
11 changes: 5 additions & 6 deletions client/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const LoginPage = () => {
const { isAuthenticated } = useStoreState((s) => s.auth);
const login = useStoreActions((s) => s.auth.login);
const [error, setError] = useState("");
const [verifying, setVerifying] = useState(false);
const [message, setMessage] = useState("");
const [loading, setLoading] = useState({ login: false, signup: false });
const [formState, { email, password, label }] = useFormState<{
email: string;
Expand Down Expand Up @@ -79,8 +79,8 @@ const LoginPage = () => {
if (type === "signup" && !DISALLOW_REGISTRATION) {
setLoading((s) => ({ ...s, signup: true }));
try {
await axios.post(APIv2.AuthSignup, { email, password });
setVerifying(true);
const res = await axios.post(APIv2.AuthSignup, { email, password });
setMessage(res.data.message);
} catch (error) {
setError(error.response.data.error);
}
Expand All @@ -97,10 +97,9 @@ const LoginPage = () => {
return (
<AppWrapper>
<ColCenterV maxWidth="100%" px={3} flex="0 0 auto" mt={4}>
{verifying ? (
{message ? (
<H2 textAlign="center" light>
A verification email has been sent to{" "}
<Email>{formState.values.email}</Email>.
{message}
</H2>
) : (
<LoginForm id="login-form" onSubmit={onSubmit("login")}>
Expand Down
2 changes: 1 addition & 1 deletion knexfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
user: env.DB_USER,
port: env.DB_PORT,
password: env.DB_PASSWORD,
ssl: env.DB_SSL,
ssl: env.DB_SSL === true ? { "rejectUnauthorized": false } : false,
},
migrations: {
tableName: "knex_migrations",
Expand Down
9 changes: 8 additions & 1 deletion server/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,16 @@ export const signup: Handler = async (req, res) => {
req.user
);

if (!process.env.MAIL_HOST) {
return res
.status(201)
.send({ message: "Your account has been created successfully." });
}
await mail.verification(user);

return res.status(201).send({ message: "Verification email has been sent." });
return res
.status(201)
.send({ message: `Verification email has been sent to ${user.email}.` });
};

export const token: Handler = async (req, res) => {
Expand Down
39 changes: 35 additions & 4 deletions server/handlers/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,37 @@ export const redirect = (app: ReturnType<typeof next>): Handler => async (
return res.redirect("/banned");
}

// 5. If wants to see link info, then redirect
// 5. Append query string when provided
if (req.query) {
const url = URL.parse(link.target, true);
const linkTargetParams = new URLSearchParams(url.search);

let added = 0;
Object.entries(req.query).forEach(([key, value]) => {
if (typeof value === "string") {
added++;
linkTargetParams.append(key, value);
}
});

if (added) {
url.search = linkTargetParams.toString();
link.target = URL.format(url);
}
}

// 6. If wants to see link info, then redirect
const doesRequestInfo = /.*\+$/gi.test(req.params.id);
if (doesRequestInfo && !link.password) {
return app.render(req, res, "/url-info", { target: link.target });
}

// 6. If link is protected, redirect to password page
// 7. If link is protected, redirect to password page
if (link.password) {
return res.redirect(`/protected/${link.uuid}`);
}

// 7. Create link visit
// 8. Create link visit
if (link.user_id && !isBot) {
queue.visit.add({
headers: req.headers,
Expand All @@ -310,7 +329,19 @@ export const redirect = (app: ReturnType<typeof next>): Handler => async (
});
}

// 8. Redirect to target
// 9. Create Google Analytics visit
if (env.GOOGLE_ANALYTICS_UNIVERSAL && !isBot) {
ua(env.GOOGLE_ANALYTICS_UNIVERSAL)
.pageview({
dp: `/${address}`,
ua: req.headers["user-agent"],
uip: req.realIP,
aip: 1
})
.send();
}

// 10. Redirect to target
return res.redirect(link.target);
};

Expand Down
4 changes: 2 additions & 2 deletions server/handlers/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const createLink = [
.custom(
value =>
urlRegex({ exact: true, strict: false }).test(value) ||
/^(?!https?)(\w+):\/\//.test(value)
/^(?!https?)(\w[\w-]+):\/\//.test(value)
)
.withMessage("URL is not valid.")
.custom(value => removeWww(URL.parse(value).host) !== env.DEFAULT_DOMAIN)
Expand Down Expand Up @@ -140,7 +140,7 @@ export const editLink = [
.custom(
value =>
urlRegex({ exact: true, strict: false }).test(value) ||
/^(?!https?)(\w+):\/\//.test(value)
/^(?!https?)(\w[\w-]+):\/\//.test(value)
)
.withMessage("URL is not valid.")
.custom(value => removeWww(URL.parse(value).host) !== env.DEFAULT_DOMAIN)
Expand Down
3 changes: 2 additions & 1 deletion server/queries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const add = async (params: Add, user?: User) => {
email: params.email,
password: params.password,
verification_token: uuid(),
verification_expires: addMinutes(new Date(), 60).toISOString()
verification_expires: addMinutes(new Date(), 60).toISOString(),
verified: process.env.MAIL_HOST ? false : true
};

if (user) {
Expand Down
2 changes: 1 addition & 1 deletion server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const generateId = async (domain_id: number = null) => {
};

export const addProtocol = (url: string): string => {
const hasProtocol = /^\w+:\/\//.test(url);
const hasProtocol = /^\w[\w-]+:\/\//.test(url);
return hasProtocol ? url : `http://${url}`;
};

Expand Down

0 comments on commit 51b9363

Please sign in to comment.