thedevs-network / kutt

Free Modern URL Shortener.
https://kutt.it
MIT License
8.48k stars 1.11k forks source link

Restricted Registration per Domain #712

Open mamo2021 opened 1 year ago

mamo2021 commented 1 year ago

Is it possible or can you make it possible that registration is only allowed for users with certain email addresses (domains)? It is useful to restrict registration so that not everyone can use the service. However, within an organization everyone should be able to create a user account and use the service.

poeti8 commented 1 year ago

Not right now, you can temporarily enable signup, allow some users to sign up and then disable the signup option again.

kengher commented 8 months ago

Pretty simple but you'll have make slight edits to the code. I did it for my org and works like a charm.

1) Open .env file, add this line somewhere: `

add a domain var here to limit so don't have to make changes to code when it needs to be modified.

LIMIT_EMAIL_DOMAIN=youremaildomain.com `

2) In client/const/site-theme.ts add: export const LIMIT_EMAIL_DOMAIN = publicRuntimeConfig.LIMIT_EMAIL_DOMAIN; //get var from .env file

3) in client/pages/login.tsx:

import { LIMIT_EMAIL_DOMAIN } from "../consts/site-theme";
...
function onSubmit(type: "login" | "signup") {
    return async e => {
....
    const { email, password } = formState.values;
    const emailRegex = LIMIT_EMAIL_DOMAIN != "" ? new RegExp("^[A-Za-z][A-Za-z0-9._%+-]+@" + LIMIT_EMAIL_DOMAIN + "$", "g") : null;
...
    if (!email) {
        return setError("Email address must not be empty.");
    }

    if (LIMIT_EMAIL_DOMAIN != "" && !emailRegex.test(email)) { //if domain exists and doesn't match the regex then error out
        return setError("Cannot use the provided email address.");
    }
...

4) npm run build

This only works for one domain, but you can always code it to make it comma separated, etc.. for multiple domains.