Skip to content

Commit

Permalink
Fix registration when invite is not required (stashapp#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash authored and feederbox826 committed Nov 15, 2023
1 parent 285d5f6 commit 3ded044
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
57 changes: 39 additions & 18 deletions frontend/src/pages/registerUser/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { useNavigate } from "react-router-dom";
import { Button, Form, Row, Col } from "react-bootstrap";
import cx from "classnames";

import { ErrorMessage, LoadingIndicator } from "src/components/fragments";
import Title from "src/components/title";
import { useNewUser } from "src/graphql";
import { useNewUser, useConfig, ConfigQuery } from "src/graphql";
import AuthContext, { ContextType } from "src/AuthContext";
import * as yup from "yup";

Expand All @@ -19,12 +20,18 @@ const schema = yup.object({
});
type RegisterFormData = yup.Asserts<typeof schema>;

const Register: FC = () => {
interface Props {
config: ConfigQuery["getConfig"];
}

const Register: FC<Props> = ({ config }) => {
const navigate = useNavigate();
const [awaitingActivation, setAwaitingActivation] = useState(false);
const Auth = useContext<ContextType>(AuthContext);
const [submitError, setSubmitError] = useState<string | undefined>();

const inviteRequired = config.require_invite ?? true;

const {
register,
handleSubmit,
Expand Down Expand Up @@ -102,21 +109,25 @@ const Register: FC = () => {
</Row>
</Form.Group>

<Form.Group controlId="inviteKey" className="mt-2">
<Row>
<Col xs={4}>
<Form.Label>Invite Key:</Form.Label>
</Col>
<Col xs={8}>
<Form.Control
className={cx({ "is-invalid": errors?.inviteKey })}
type="text"
placeholder="Invite Key"
{...register("inviteKey")}
/>
</Col>
</Row>
</Form.Group>
{inviteRequired ? (
<Form.Group controlId="inviteKey" className="mt-2">
<Row>
<Col xs={4}>
<Form.Label>Invite Key:</Form.Label>
</Col>
<Col xs={8}>
<Form.Control
className={cx({ "is-invalid": errors?.inviteKey })}
type="text"
placeholder="Invite Key"
{...register("inviteKey")}
/>
</Col>
</Row>
</Form.Group>
) : (
<Form.Control type="hidden" value="-" {...register("inviteKey")} />
)}

{errorList.map((error) => (
<Row key={error} className="text-end text-danger">
Expand All @@ -137,4 +148,14 @@ const Register: FC = () => {
);
};

export default Register;
const ConfigLoader = () => {
const { data: config, loading } = useConfig();
if (loading) return <LoadingIndicator message="Loading config..." />;

if (!config)
return <ErrorMessage error="Unable to load server configuration" />;

return <Register config={config.getConfig} />;
};

export default ConfigLoader;
2 changes: 1 addition & 1 deletion graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Query {
version: Version! @hasRole(role: READ)

### Instance Config ###
getConfig: StashBoxConfig! @hasRole(role: READ)
getConfig: StashBoxConfig!
}

type Mutation {
Expand Down
30 changes: 3 additions & 27 deletions pkg/models/generated_exec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions pkg/user/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,25 @@ func ActivateNewUser(fac models.Repo, name, email, activationKey, password strin
return nil, ErrInvalidActivationKey
}

// check expiry
var invitedBy *uuid.UUID
if config.GetRequireInvite() {
i, err := iqb.Find(a.InviteKey.UUID)
if err != nil {
return nil, err
}

i, err := iqb.Find(a.InviteKey.UUID)
if err != nil {
return nil, err
}
if i == nil {
return nil, errors.New("cannot find invite key")
}

if i == nil {
return nil, errors.New("cannot find invite key")
invitedBy = &i.GeneratedBy
}

invitedBy := i.GeneratedBy

createInput := models.UserCreateInput{
Name: name,
Email: email,
Password: password,
InvitedByID: &invitedBy,
InvitedByID: invitedBy,
Roles: getDefaultUserRoles(),
}

Expand Down Expand Up @@ -220,8 +221,10 @@ func ActivateNewUser(fac models.Repo, name, email, activationKey, password strin
}

// delete the invite key
if err := iqb.Destroy(a.InviteKey.UUID); err != nil {
return nil, err
if !a.InviteKey.UUID.IsNil() {
if err := iqb.Destroy(a.InviteKey.UUID); err != nil {
return nil, err
}
}

return ret, nil
Expand Down

0 comments on commit 3ded044

Please sign in to comment.