Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix duplicate user being created (#204)

Guilherme Oenning0eb1d68f873d
diff --git a/app/storage/postgres/user.go b/app/storage/postgres/user.go
index 2f57d9875..e518b65d2 100644
--- a/app/storage/postgres/user.go
+++ b/app/storage/postgres/user.go
@@ -2,6 +2,7 @@ package postgres
import (
"crypto/md5"
+ "strings"
"time"
"database/sql"
@@ -97,6 +98,7 @@ func (s *UserStorage) GetByProvider(tenantID int, provider string, uid string) (
// Register creates a new user based on given information
func (s *UserStorage) Register(user *models.User) error {
now := time.Now()
+ user.Email = strings.TrimSpace(user.Email)
if err := s.trx.QueryRow("INSERT INTO users (name, email, created_on, tenant_id, role) VALUES ($1, $2, $3, $4, $5) RETURNING id", user.Name, user.Email, now, user.Tenant.ID, user.Role).Scan(&user.ID); err != nil {
return err
}
diff --git a/app/storage/postgres/user_test.go b/app/storage/postgres/user_test.go
index 700281f1c..ae11e6e8d 100644
--- a/app/storage/postgres/user_test.go
+++ b/app/storage/postgres/user_test.go
@@ -124,6 +124,29 @@ func TestUserStorage_Register(t *testing.T) {
Expect(user.Email).To(Equal("[email protected]"))
}
+func TestUserStorage_Register_WhiteSpaceEmail(t *testing.T) {
+ SetupDatabaseTest(t)
+ defer TeardownDatabaseTest()
+
+ users := postgres.NewUserStorage(trx)
+ user := &models.User{
+ Name: "Rob Stark",
+ Email: " ",
+ Tenant: &models.Tenant{
+ ID: 1,
+ },
+ Role: models.RoleCollaborator,
+ }
+ err := users.Register(user)
+ Expect(err).To(BeNil())
+
+ user, err = users.GetByID(user.ID)
+ Expect(err).To(BeNil())
+ Expect(user.Role).To(Equal(models.RoleCollaborator))
+ Expect(user.Name).To(Equal("Rob Stark"))
+ Expect(user.Email).To(Equal(""))
+}
+
func TestUserStorage_Register_MultipleProviders(t *testing.T) {
SetupDatabaseTest(t)
defer TeardownDatabaseTest()
diff --git a/main.go b/main.go
index 9bcfb8cd5..5d1f145b3 100644
--- a/main.go
+++ b/main.go
@@ -13,7 +13,7 @@ import (
)
var buildtime string
-var version = "0.8.0"
+var version = "0.9.0-beta"
func main() {
fmt.Printf("Application is starting...\n")
diff --git a/migrations/201712061924_unique_email.up.sql b/migrations/201712061924_unique_email.up.sql
new file mode 100644
index 000000000..912a4f804
--- /dev/null
+++ b/migrations/201712061924_unique_email.up.sql
@@ -0,0 +1,4 @@
+CREATE UNIQUE INDEX user_email_unique_idx ON users (tenant_id, email) WHERE email != '';
+CREATE UNIQUE INDEX user_provider_unique_idx ON user_providers (user_id, provider);
+CREATE UNIQUE INDEX tenant_subdomain_unique_idx ON tenants (subdomain);
+CREATE UNIQUE INDEX tenant_cname_unique_idx ON tenants (cname) WHERE cname != '';
\ No newline at end of file
diff --git a/public/components/common/Button.tsx b/public/components/common/Button.tsx
index acddab3df..2b6122b7a 100644
--- a/public/components/common/Button.tsx
+++ b/public/components/common/Button.tsx
@@ -42,12 +42,16 @@ export class Button extends React.Component<ButtonProps, ButtonState> {
this.unmounted = true;
}
- private async click(e?: React.MouseEvent<HTMLButtonElement>) {
+ public async click(e?: React.MouseEvent<HTMLButtonElement>) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
+ if (this.state.clicked) {
+ return;
+ }
+
const event = new ButtonClickEvent();
this.setState({ clicked: true });
if (this.props.onClick) {
diff --git a/public/components/common/SignInControl.tsx b/public/components/common/SignInControl.tsx
index 4d2afd708..795ff55fd 100644
--- a/public/components/common/SignInControl.tsx
+++ b/public/components/common/SignInControl.tsx
@@ -93,7 +93,7 @@ export class SignInControl extends React.Component<SignInControlProps, SignInCon
{ this.props.signInByEmail && <div>
<p>Enter your e-mail address to sign in</p>
- <Form ref={(f) => { this.form = f!; }} onSubmit={() => this.signIn()}>
+ <Form ref={(f) => { this.form = f!; }}>
<div id="email-signin" className="ui small action fluid input">
<input onChange={(e) => this.setState({ email: e.currentTarget.value })} type="text" placeholder="[email protected]" className="small" />
<Button onClick={() => this.signIn()} className={`positive ${this.state.email === '' && 'disabled'}`}>
diff --git a/public/pages/site/CompleteSignInProfilePage.tsx b/public/pages/site/CompleteSignInProfilePage.tsx
index e416b6f03..a2a844c58 100644
--- a/public/pages/site/CompleteSignInProfilePage.tsx
+++ b/public/pages/site/CompleteSignInProfilePage.tsx
@@ -2,7 +2,7 @@ import * as React from 'react';
import { HomePage } from './HomePage';
import { showModal, getQueryString } from '@fider/utils/page';
-import { Form } from '@fider/components/common';
+import { Form, Button } from '@fider/components/common';
import { inject, injectables } from '@fider/di';
import { TenantService } from '@fider/services';
@@ -47,7 +47,7 @@ export class CompleteSignInProfilePage extends React.Component<{}, CompleteSignI
</div>
<div className="content">
<p>Because this is your first sign in, please input your display name.</p>
- <Form ref={(f) => { this.form = f!; }} onSubmit={() => this.submit()}>
+ <Form ref={(f) => { this.form = f!; }}>
<div className="ui small action fluid input">
<input
onChange={(e) => this.setState({ name: e.currentTarget.value })}
@@ -56,9 +56,9 @@ export class CompleteSignInProfilePage extends React.Component<{}, CompleteSignI
placeholder="Your display name"
className="small"
/>
- <button onClick={() => this.form.submit()} className={`ui small positive button ${this.state.name === '' && 'disabled'}`}>
+ <Button onClick={() => this.submit()} className={`small positive ${this.state.name === '' && 'disabled'}`}>
Submit
- </button>
+ </Button>
</div>
</Form>
</div>