Skip to content

Commit

Permalink
新增邀请方式
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangkaili committed Feb 5, 2021
1 parent 89f0514 commit adc1d95
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 75 deletions.
115 changes: 115 additions & 0 deletions app/scenes/Login/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Props = {
isCreate: boolean,
onEmailSuccess: (email: string) => void,
onLdapSuccess: (username: string) => void,
onInvSuccess: (username: string) => void,
};

type State = {
Expand All @@ -24,6 +25,10 @@ type State = {
ldapId: string,
ldapPassword: string,
showLdapSignin: boolean,
accountId: string,
accountPwd: string,
invCode: string,
showInvSignin: boolean,
};

class Service extends React.Component<Props, State> {
Expand All @@ -34,6 +39,10 @@ class Service extends React.Component<Props, State> {
ldapId: "",
ldapPassword: "",
showLdapSignin: false,
accountId: "",
accountPwd: "",
invCode: "",
showInvSignin: false,
};

handleChangeEmail = (event: SyntheticInputEvent<HTMLInputElement>) => {
Expand All @@ -48,6 +57,18 @@ class Service extends React.Component<Props, State> {
this.setState({ ldapPassword: event.target.value });
};

handleChangeAccountId = (event: SyntheticInputEvent<HTMLInputElement>) => {
this.setState({accountId : event.target.value });
};

handleChangePwdForAccount = (event: SyntheticInputEvent<HTMLInputElement>) => {
this.setState({ accountPwd: event.target.value });
};

handleChangeCodeForAccount = (event: SyntheticInputEvent<HTMLInputElement>) => {
this.setState({ invCode: event.target.value });
};

handleSubmitEmail = async (event: SyntheticEvent<HTMLFormElement>) => {
event.preventDefault();

Expand Down Expand Up @@ -98,6 +119,34 @@ class Service extends React.Component<Props, State> {
}
};

handleSubmitInvitation = async (event: SyntheticEvent<HTMLFormElement>) => {
event.preventDefault();

if (this.state.showInvSignin && this.state.accountId) {
this.setState({ isSubmitting: true });

try {
// console.log("action:", event.currentTarget.action);
const response = await client.post(event.currentTarget.action, {
username: this.state.accountId,
password: this.state.accountPwd,
invCode: this.state.invCode,
});
if (response.redirect) {
window.location.href = response.redirect;
} else {
console.log("login success for invitation");
this.props.onInvSuccess(this.state.accountId);
}
} finally {
console.log("submit failure");
this.setState({ isSubmitting: false });
}
} else {
this.setState({ showInvSignin: true });
}
};

render() {
const { isCreate, id, name, authUrl } = this.props;

Expand Down Expand Up @@ -194,6 +243,72 @@ class Service extends React.Component<Props, State> {
);
}

if (id === "invitation") {
if (isCreate) {
return null;
}

console.log("id:", id);
console.log("name:", name);
console.log("authUrl:", authUrl);
console.log("invitation type");
return (
<Wrapper key="invitation">
<Form
method="POST"
action="/auth/invitation"
onSubmit={this.handleSubmitInvitation}
>
{this.state.showInvSignin ? (
<>
<InputLarge
type="text"
name="accountId"
placeholder="Please enter Account ID"
value={this.state.accountId}
onChange={this.handleChangeAccountId}
disabled={this.state.isSubmitting}
autoFocus
required
short
/>

<InputLarge
type="password"
name="password"
placeholder="Please enter password"
value={this.state.accountPwd}
onChange={this.handleChangePwdForAccount}
disabled={this.state.isSubmitting}
autoFocus
required
short
/>
<InputLarge
type="text"
name="invCode"
placeholder="Please enter invitation code"
value={this.state.invCode}
onChange={this.handleChangeCodeForAccount}
disabled={this.state.isSubmitting}
autoFocus
required
short
/>
<ButtonLarge type="submit" disabled={this.state.isSubmitting}>
Sign In →
</ButtonLarge>
</>
) : (
<ButtonLarge type="submit" icon={<EmailIcon />} fullwidth>
Continue with Invitation
</ButtonLarge>
)}
</Form>
</Wrapper>
);
}

const icon =
id === "slack" ? (
<Logo>
Expand Down
2 changes: 1 addition & 1 deletion app/utils/uploadFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const uploadFile = async (
formData.append("file", file);
}

let bucketDir = "wiki" + data.form.key;
let bucketDir = "finance-wiki" + data.form.key;
console.log("bucketDir: ", bucketDir);

try {
Expand Down
37 changes: 21 additions & 16 deletions server/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,33 @@ if (process.env.GOOGLE_CLIENT_ID) {
// });
// }

// services.push({
// id: "ldap",
// name: "LDAP",
// authUrl: "",
// });

services.push({
id: "ldap",
name: "LDAP",
authUrl: "",
},
// {
// id: "email",
// name: "Email",
// authUrl: "",
// }
);
id: "invitation",
name: "Invitation",
authUrl: "",
});

// services.push({
// id: "ldap",
// name: "LDAP",
// authUrl: "",
// },
// // {
// // id: "email",
// // name: "Email",
// // authUrl: "",
// // }
// );

function filterServices(team) {
let output = services;

// 邀请制
if (team) {
output = reject(output, (service) => service.id === "invitation");
}

if (team && !team.ldapId) {
output = reject(output, (service) => service.id === "ldap");
}
if (team && !team.googleId) {
Expand Down
2 changes: 2 additions & 0 deletions server/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import email from "./email";
import google from "./google";
import slack from "./slack";
import ldap from "./ldap";
import invitation from "./invitation";

const app = new Koa();
app.use(Cors());
Expand All @@ -22,6 +23,7 @@ router.use("/", slack.routes());
router.use("/", google.routes());
router.use("/", email.routes());
router.use("/", ldap.routes());
router.use("/", invitation.routes());

router.get("/redirect", auth(), async (ctx) => {
const user = ctx.state.user;
Expand Down
Loading

0 comments on commit adc1d95

Please sign in to comment.